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;
- typedef long long ll;
- int n;
- int values[105], weights[105];
- int maximum_weight;
- ll dp[105][100005];
- ll 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];
- }
- ll 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;
- cin >> maximum_weight;
- for(int i = 0; i < n; i++) {
- cin >> weights[i];
- cin >> values[i];
- }
- 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