-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbagpack.cpp
More file actions
54 lines (40 loc) · 769 Bytes
/
bagpack.cpp
File metadata and controls
54 lines (40 loc) · 769 Bytes
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
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
vector<pair<int, int>> list;
long long cache[105][100005];
int max(int n1, int n2)
{
if(n1 > n2)
return n1;
return n2;
}
long long search(int pick, long long weight)
{
long long &ret = cache[pick + 1][weight];
if(weight < 0 || pick >= list.size())
{
if(weight < 0)
return ret = 0;
}
if(ret != -1)
return ret;
ret = 0;
for(int i = pick + 1; i < list.size(); ++i)
ret = max(ret, search(i, weight - list[i].first));
return ret = ret + list[pick].second;
}
int main(void)
{
int n, k;
cin >> n >> k;
memset(cache, -1, sizeof(cache));
for(int i = 0; i < n; ++i)
{
int w, v;
cin >> w >> v;
list.push_back(make_pair(w,v));
}
cout << search(-1, k);
}