Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn = 2005;
- const int max_weight = 3550;
- const int INF = 1e9;
- int price[maxn], weights[maxn];
- int n, W;
- int dp[maxn][max_weight];
- int rec(int at, int weight_left) {
- if(weight_left == 0) {
- return 0;
- }
- if(at == -1) {
- return 0;
- }
- if(dp[at][weight_left] != -1) {
- return dp[at][weight_left];
- }
- int result = -INF;
- result = max(result, rec(at - 1, weight_left));
- if(weight_left - weights[at] >= 0) {
- result = max(result, rec(at - 1, weight_left - weights[at]) + price[at]);
- }
- return dp[at][weight_left] = result;
- }
- int main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> price[i] >> weights[i];
- }
- cin >> W;
- for(int i = 0; i < maxn; i++) {
- for(int j = 0; j < max_weight; j++) {
- dp[i][j] = -1;
- }
- }
- cout << rec(n - 1, W) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement