Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int cost[105], weight[105];
- long long dp[101][100001];
- long long 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];
- }
- long long maks = -1e15;
- 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;
- int max_weight;
- cin >> max_weight;
- for(int i = 0; i < n; i++) {
- cin >> weight[i] >> cost[i];
- }
- 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