Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <cstring>
- #include <map>
- using namespace std;
- typedef long long ll;
- const int maxn = 105;
- const ll INF = 1e16;
- int n;
- ll W;
- int weights[maxn], values[maxn];
- ll dp[maxn][100005];
- ll rec(int at, int value_left) {
- if(value_left == 0) {
- return 0;
- }
- if(at >= n) {
- return INF;
- }
- if(dp[at][value_left] != -1) {
- return dp[at][value_left];
- }
- ll res = INF;
- res = min(res, rec(at + 1, value_left));
- if(value_left - values[at] >= 0) {
- res = min(res, rec(at + 1, value_left - values[at]) + weights[at]);
- }
- dp[at][value_left] = res;
- return res;
- }
- int main()
- {
- memset(dp, -1, sizeof dp);
- cin >> n >> W;
- int sum = 0;
- for(int i = 0; i < n; i++) {
- cin >> weights[i] >> values[i];
- sum += values[i];
- }
- for(int i = sum; i >= 0; i--) {
- if(rec(0, i) <= W) {
- cout << i << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment