公式
A - Unusual-Constraint Knapsack 解説 by evima
Let \(R\) denote the maximum total weight of items that can be chosen.
Initially, \(R = W\).
We decide whether to choose item \(i\) in the order \(i = N, N-1, \dots, 1\).
When \(R < W_i\), item \(i\) cannot be chosen.
When \(W_i \leq R\), if we do not choose item \(i\), then by the constraint we have \(\sum_{j=1}^{i-1} W_j \leq R\), so we can choose all of items \(1, 2, \dots, i-1\).
If we choose item \(i\), then \(R\) is updated to \(R - W_i\).
We compare the total value obtainable in each of the two cases and adopt the larger one.
By doing this recursively, the answer can be obtained in \(O(N)\).
投稿日時:
最終更新: