-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatDPE.cpp
More file actions
39 lines (30 loc) · 844 Bytes
/
Copy pathatDPE.cpp
File metadata and controls
39 lines (30 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Contest : AtCoder Educational DP Contest
* Problem : E - Knapsack 2
* Link : https://atcoder.jp/contests/dp/tasks/dp_e
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
int main() {
fastio
ll n, W, total = 1;
cin >> n >> W;
vector<ll> w(n), v(n);
for (int i = 0; i < n; ++i) cin >> w[i] >> v[i], total += v[i];
vector<ll> dp(total, INT_MAX);
dp[0] = 0; // valor 0 não custa peso algum
for (int i = 0; i < n; ++i) {
for (int j = total; j >= v[i]; --j) {
// para cada valor j, guarda o menor peso para esse valor
dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
}
}
// pegar o maior valor para um peso <= W
ll ans = 0;
for (ll i = 0; i < total; ++i)
if (dp[i] <= W) ans = i;
cout << ans << '\n';
return 0;
}