Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int cost[100][100];
- int knapsack(int n, int W, int wm[], int vm[])
- {
- for(int i=0; i<=W; i++)
- cost[0][i] = 0;
- for(int i=0; i<=n; i++)
- cost[i][0] = 0;
- for(int i=1; i<=n; i++)
- {
- for(int j=1; j<=W; j++)
- {
- if(wm[i] > j)
- cost[i][j] = cost[i-1][j];
- else
- {
- if (vm[i]+cost[i-1][j-wm[i]] > cost[i-1][j])
- cost[i][j] = vm[i] + cost[i-1][j-wm[i]];
- else
- cost[i][j] = cost[i-1][j];
- }
- }
- }
- return cost[n][W];
- }
- void items(int n, int W, int wm[])
- {
- int i = n;
- int j = W;
- while (i > 0 && j > 0)
- {
- if(cost[i][j] != cost[i-1][j])
- {
- printf("%d\n",i);
- j-=wm[i];
- i--;
- }
- else
- i--;
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- int wm[n+2],vm[n+2];
- wm[0]=0, vm[0]=0;
- for(int i=1;i<=n;i++)
- cin >> wm[i] >> vm[i];
- cout << knapsack(n, 50, wm, vm) << endl;
- items(n, 50, wm);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment