Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<ctime>
- #include<map>
- #include<vector>
- #include<queue>
- using namespace std;
- int n;
- int values[2002], weights[2002];
- int maximum_weight;
- int dp[2002][3502];
- int rec(int i, int weight_left) {
- if(i == n or weight_left == 0) {
- return 0;
- }
- if(dp[i][weight_left] != -1) {
- return dp[i][weight_left];
- }
- int result = 0;
- result = max(result, rec(i + 1, weight_left));
- if(weight_left - weights[i] >= 0) {
- result = max(result, rec(i + 1, weight_left - weights[i]) + values[i]);
- }
- dp[i][weight_left] = result;
- return result;
- }
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> values[i];
- cin >> weights[i];
- }
- cin >> maximum_weight;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j <= maximum_weight; j++) {
- dp[i][j] = -1;
- }
- }
- cout << rec(0, maximum_weight) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement