C - 予算内での買い物 / Shopping Within Budget Editorial by admin Editorial for printing
Qwen3-Coder-480BOverview
This is a problem where Takahashi keeps part of his money for living expenses and maximizes the total satisfaction by selecting products within the remaining budget.
Analysis
This problem reduces to the classic 0-1 Knapsack Problem.
Each product has two choices: “select” or “not select,” and we need to maximize the total satisfaction (value) without exceeding the budget (capacity).
Issues with Naive Approaches
For example, greedy approaches such as selecting in order of “highest satisfaction” or “highest cost performance (satisfaction/price)” do not guarantee an optimal solution for the knapsack problem.
Also, if we try all possible selections through brute force, there are \(2^N\) combinations, which is impractical given the constraint that \(N\) can be up to \(2000\) (it would result in TLE).
Solution
By using Dynamic Programming (DP), we can efficiently find the optimal solution.
We update a DP table dp[w] defined as “the maximum satisfaction obtainable with a budget of at most \(w\) yen.”
Algorithm
This problem can be solved with the standard DP algorithm for the 0-1 Knapsack Problem.
DP Definition
dp[w]: The maximum satisfaction obtainable with a budget of at most \(w\) yen
Update Rule
For each product \((c, v)\), we iterate over budgets \(w\) that are at least \(c\), going from larger to smaller values, and update as follows: $\( dp[w] = \max(dp[w],\ dp[w - c] + v) \)$
By performing this update from back to front, we prevent selecting the same product multiple times (handling the constraint that each item can be chosen at most once).
Procedure
- Calculate the budget:
budget = S - T - For each product, only consider those whose price is within the budget
- Initialize the DP table:
dp[0..budget] = 0 - Update the DP table for each candidate product
- The final answer is
max(dp)
Example
For example,
N=3, S=10, T=2 → budget=8
Products:
1: C=3, V=5
2: C=4, V=7
3: C=5, V=9
All products are eligible candidates. As we update the DP, selecting products 2 and 3 would have a total cost of \(4+5=9\) which exceeds the budget, but selecting products 1 and 2 gives \(3+4=7 \leq 8\) with satisfaction \(5+7=12\), and so on — comparing these to find the optimal solution.
Complexity
- Time complexity: \(O(N \cdot \text{budget})\)
- Space complexity: \(O(\text{budget})\)
Here, \(\text{budget} = S - T \leq 10^5\), so this runs sufficiently fast.
Implementation Notes
Products whose price exceeds the budget can be excluded beforehand to avoid unnecessary computation
During DP updates, loop “from back to front” to prevent selecting the same product multiple times
Output
max(dp)at the end (note that the answer is not necessarilydp[budget])Source Code
import sys
import heapq
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx]); idx += 1
S = int(data[idx]); idx += 1
T = int(data[idx]); idx += 1
budget = S - T
items = []
for _ in range(N):
c = int(data[idx]); idx += 1
v = int(data[idx]); idx += 1
if c <= budget:
items.append((c, v))
# 満足度を降順にソートして、効率の良いものから選ぶようにする(性質を利用)
# 実際にはDPで解くのが適切
# これは 0-1 ナップサック問題に帰着される
# DPテーブル: dp[i][w] = i番目まで見て、重さw以内での最大価値
# ただしメモリ節約のため、1次元で処理する
# 制約上、budget <= 10^5 なので、DPが可能
dp = [0] * (budget + 1)
for c, v in items:
# 後ろから更新しないと、同じアイテムを複数回使ってしまう可能性がある
for j in range(budget, c - 1, -1):
if dp[j] < dp[j - c] + v:
dp[j] = dp[j - c] + v
print(max(dp))
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
posted:
last update: