Official

C - 予算内での買い物 / Shopping Within Budget Editorial by admin Editorial for printing

Claude 4.6 Opus (Thinking)

Overview

This is a classic 0-1 knapsack problem where we select items within a budget to maximize satisfaction.

Analysis

Problem Setup

Takahashi has \(S\) yen, but needs to keep \(T\) yen for living expenses, so the amount available for shopping is \(W = S - T\) yen. Since each item can be bought at most once, the problem reduces to selecting items within a budget of \(W\) yen to maximize the total satisfaction.

Naive Approach

For each of the \(N\) items, there are 2 choices: “buy” or “don’t buy”, so an exhaustive search would need to examine \(2^N\) combinations. Since \(N\) can be up to \(2000\), \(2^{2000}\) is an astronomical number, making exhaustive search completely infeasible.

How to Solve It

This is a well-known dynamic programming (DP) problem called the 0-1 knapsack problem. By performing DP with “available budget” as the state, we can solve it efficiently.

Algorithm

We use dynamic programming (1-dimensional DP).

DP Definition

\(dp[j]\) = the maximum satisfaction achievable when the total cost is at most \(j\) yen

The initial state is \(dp[j] = 0\) (the state where nothing is bought).

Transition

For each item \((C_i, V_i)\), we iterate over the budget \(j\) from large to small and perform the following update:

\[dp[j] = \max(dp[j],\ dp[j - C_i] + V_i) \quad (j \geq C_i)\]

  • \(dp[j]\): the satisfaction when item \(i\) is not bought
  • \(dp[j - C_i] + V_i\): the satisfaction when item \(i\) is bought (spending \(C_i\) yen and adding \(V_i\) to the optimal solution for the remaining \(j - C_i\) yen)

Why Iterate in Reverse Order?

By updating \(j\) from large to small, we guarantee that each item is selected at most once. If we updated from small to large, the same item could end up being selected multiple times.

Concrete Example

For example, with \(W = 5\) and two items \((C=3, V=10)\) and \((C=2, V=8)\):

  1. Initial state: \(dp = [0, 0, 0, 0, 0, 0]\)
  2. After processing item 1 \((3, 10)\): \(dp = [0, 0, 0, 10, 10, 10]\)
  3. After processing item 2 \((2, 8)\): \(dp = [0, 0, 8, 10, 10, 18]\)

The answer is \(dp[5] = 18\) (buying both items for a total of 5 yen, with satisfaction 18).

Complexity

  • Time complexity: \(O(N \times W)\) (where \(W = S - T\))
    • Since \(N \leq 2000\) and \(W \leq 10^5\), this is at most about \(2 \times 10^8\) operations, but since the internal processing consists only of simple comparisons and additions, it barely fits within the time limit even in Python.
  • Space complexity: \(O(W)\) (only a 1-dimensional DP array)

Implementation Notes

  • Compute the budget \(W = S - T\) at the beginning.

  • Initialize the DP array of length \(W + 1\) with all values set to \(0\).

  • For each item, loop \(j\) from \(W\) down to \(C_i\) in reverse order to correctly implement the 0-1 knapsack (each item at most once).

  • The final answer is \(dp[W]\) (the maximum satisfaction within a budget of \(W\) yen).

    Source Code

N, S, T = map(int, input().split())
budget = S - T
items = []
for _ in range(N):
    c, v = map(int, input().split())
    items.append((c, v))

# 0-1 knapsack
dp = [0] * (budget + 1)
for c, v in items:
    for j in range(budget, c - 1, -1):
        dp[j] = max(dp[j], dp[j - c] + v)

print(dp[budget])

This editorial was generated by claude4.6opus-thinking.

posted:
last update: