-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1943.cpp
More file actions
65 lines (46 loc) · 823 Bytes
/
1943.cpp
File metadata and controls
65 lines (46 loc) · 823 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
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int cache[105][100010];
vector<pair<int, int>> v;
int sum;
int search(int idx, int me)
{
if(sum <= me * 2)
{
if(sum == me * 2)
return 1;
return 0;
}
if(idx == v.size())
return 0;
int &ret = cache[idx][me];
if(ret != -1)
return ret;
ret = search(idx + 1, me);
for(int i = 1; i <= v[idx].second; ++i)
{
ret = max(ret, search(idx + 1, me + v[idx].first * i));
}
return ret;
}
int main(void)
{
for(int i = 0; i < 3; ++i)
{
int n;
cin >> n;
sum = 0;
memset(cache, -1, sizeof(cache));
for(int j = 0; j < n; ++j)
{
int cost, count;
cin >> cost >> count;
sum += (cost * count);
v.push_back({cost, count});
}
cout << search(0, 0) << "\n";
v.clear();
}
}