Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int cost[2002], weight[2002];
- int dp[2002][3502];
- int rek(int at, int weight_left) {
- if(at == n or weight_left == 0) {
- return 0;
- }
- if(dp[at][weight_left] != -1) {
- return dp[at][weight_left];
- }
- int maks = -2e9;
- maks = max(maks, rek(at + 1, weight_left)); // sto ako ne go zememe ovoj element
- if(weight_left - weight[at] >= 0) {
- maks = max(maks, rek(at + 1, weight_left - weight[at]) + cost[at]); // sto ako go zememe ovoj element
- }
- dp[at][weight_left] = maks;
- return maks;
- }
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> cost[i] >> weight[i];
- }
- int max_weight;
- cin >> max_weight;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j <= max_weight; j++) {
- dp[i][j] = -1;
- }
- }
- cout << rek(0, max_weight) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement