-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE - Knapsack_2.cpp
More file actions
56 lines (48 loc) · 1.03 KB
/
Copy pathE - Knapsack_2.cpp
File metadata and controls
56 lines (48 loc) · 1.03 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* author: _Berlin_
* created: 07.04.2024 01:05:39 PM
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef BERLIN
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
#define endl '\n'
#define ll long long
const int N = 1e2 + 9;
const ll inf = 1e11;
int n, capacity, weight[N], value[N];
ll dp[N][N * 1000];
ll knapsack(int i, int total_value) {
if(i == n + 1) {
if(total_value == 0) {
return 0;
}
return inf;
}
ll &ans = dp[i][total_value];
if(ans != -1) return ans;
ans = inf;
ans = knapsack(i + 1, total_value);
ans = min(ans, weight[i] + knapsack(i + 1, total_value - value[i]));
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> capacity;
for(int i = 1; i <= n; i++){
cin >> weight[i] >> value[i];
}
int ans = 0;
memset(dp, -1, sizeof dp);
for(int total_value = 1; total_value <= n * 1000; total_value++){
if(knapsack(1, total_value) <= capacity) {
ans = max(ans, total_value);
}
}
cout << ans << endl;
return 0;
}