We are given a knapsack problem with N up to 100, wi up to 10^7, W up to 10^9, vi up to 100. The constraints hint that a typical DP by weight is impossible because weight capacity is huge. However, values are small (max 100 per item, and N=100 so total max value sum = 100*100 = 10,000). So we can do DP on value: find minimum weight needed to achieve a given total value. Then we can find the maximum value such that minimum weight <= W.
This is classic: 0-1 knapsack where we compute dp[v] = minimum total weight to achieve exactly value v. Since total value Vmax = sum of all vi <= 10,000, that's manageable. Initialize dp[0] = 0, dp[v>0] = INF (some large number > W). Then for each item (v_i, w_i), iterate v from Vmax down to v_i: dp[v] = min(dp[v], dp[v - v_i] + w_i). At the end, find max v such that dp[v] <= W.
Complexities: O(N * Vmax) ~ 100 * 10,000 = 1e6, fine.
We must careful about INF: Since W up to 1e9 and wi up to 1e7, INF could be something like 10^18 or 1e18 to avoid overflow.
Now note: There's a twist: The problem statement says "You want to find a subset of items to put such that: * The total value of the items is as large as possible. * The items have combined weight at most W". So it's the standard 0/1 knapsack.
Examples: first input yields 13, second yields 9.
Edge cases: N=0? But N>=1 per constraints.
Implement: read N, W; arrays v[N], w[N]; compute total_value = sum(v). Create dp array of size total_value+1, fill with INF. dp[0]=0. For each item: for val from total_value down to v_i: if dp[val - v_i] + w_i < dp[val], update.
Finally, for val from total_value down to 0: if dp[val] <= W: print val; break.
Make sure to use long long for weight. Since v_i are ints, but dp indices based on ints.
So solution is straightforward.
But wait: constraints: wi up to 10,000,000 and W up to 1e9, so sum of weights could be > 2^31, so long long needed.
Now we implement solve().
But note: The problem might be from AtCoder: typical DP problem "Knapsack 2" where N up to 100, values up to 100, weight large. Yes exactly.
Thus solution is DP on values.
Let's code.
We'll read input. Ensure use of fast I/O if needed. Use vector dp(total_value+1, INF). INF = 1e18.
Implementation steps:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int main() {
int N, W;
cin >> N >> W;
vector<int> v(N), w(N);
int sumV = 0;
for(int i=0; i<N; ++i) {
cin >> v[i] >> w[i];
sumV += v[i];
}
vector<ll> dp(sumV+1, INF);
dp[0] = 0;
for(int i=0; i<N; ++i) {
for(int val = sumV; val >= v[i]; --val) {
if(dp[val - v[i]] + w[i] < dp[val]) {
dp[val] = dp[val - v[i]] + w[i];
}
}
}
int ans = 0;
for(int val = sumV; val >= 0; --val) {
if(dp[val] <= W) {
ans = val;
break;
}
}
cout << ans << endl;
return 0;
}
That should work.
Double-check with examples:
Example 1:
N=4, W=5
items: (4,2), (5,2), (2,1), (8,3)
sumV = 4+5+2+8 = 19
Compute dp.
dp[0]=0
Item1 v=4,w=2:
- for val=19..4: dp[4] = min(INF, 0+2)=2; others remain INF.
Item2 v=5,w=2:
- val=19..5:
dp[5] = INF vs dp[0]+2=2 => dp[5]=2
dp[9] = INF vs dp[4]+2=2+2=4 => dp[9]=4
dp[?] also maybe: for val from 9 down, but we did correctly.
Item3 v=2,w=1:
- update: many combos.
Item4 v=8,w=3.
Eventually dp[13]? Let's check: Could we achieve value 13? Items: 5+8=13, weight=2+3=5 -> OK. So dp[13] should be 5. Then answer = max v with dp<=5 -> 13. Good.
Example2:
2 20
5 9
4 10
sumV=9
dp after processing: dp[5]=9, dp[4]=10, dp[9]=19 (since 5+4=9, weight 9+10=19)
Also dp[0]=0
Now find max v with dp<=20: v=9 has weight 19 <=20, so answer 9. Good.
Thus correct.
Potential issues: If total_value is large? max 10000, fine.
Memory: dp array size 10001 long longs, okay.
Thus solution is complete.
Now output exactly as required.
That's it. ```cpp
#include
#include
#include
using namespace std;
typedef long long ll;
const ll INF = 1e18; // larger than any possible total weight
int main() {
int N, W;
cin >> N >> W;
vector v(N), w(N);
int totalV = 0;
for (int i = 0; i < N; ++i) {
cin >> v[i] >> w[i];
totalV += v[i];
}
// dp[val] = minimum weight needed to achieve exactly value val
vector<ll> dp(totalV + 1, INF);
dp[0] = 0;
// 0/1 knapsack by values
for (int i = 0; i < N; ++i) {
for (int val = totalV; val >= v[i]; --val) {
if (dp[val - v[i]] + w[i] < dp[val]) {
dp[val] = dp[val - v[i]] + w[i];
}
}
}
// answer is the largest value whose minimal weight ≤ W
int ans = 0;
for (int val = totalV; val >= 0; --val) {
if (dp[val] <= W) {
ans = val;
break;
}
}
cout << ans << endl;
return 0;
}