-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-knapsack.cpp
42 lines (34 loc) · 951 Bytes
/
binary-knapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define endl "\n"
#define ll long long
ll mod = INT_MAX;
int profit[2001],cap[2001];
int dp[2001][2001];
int item, size;
int f(int in, int rem){
if(dp[in][rem]!=-1)return dp[in][rem];
if(in==size || rem==0)return 0;
int a=0,b=0;
if(cap[in]<=rem)a = profit[in]+f(in+1,rem-cap[in]);
b = f(in+1,rem);
return dp[in][rem] = max(a,b);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1; //cin >> t;
while(t--){
cin >> item >> size;
memset(dp, -1, sizeof(dp));
for(int i=0;i<item;i++)cin >> cap[i] >> profit[i];
cout << f(0,size) << endl;
}
}