-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarmer.cpp
67 lines (56 loc) · 1.04 KB
/
farmer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* International Olympiad of Informatics 2004, Day 2 Q2
* Ananya Kumar, 2012
* NUS High School
*/
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXQ 150005
bool memo[MAXQ] = {false};
vector<int> strip;
int main ()
{
FILE *in = fopen("farmer.in","r");
FILE * out = fopen("farmer.out","w");
int Q, M, K;
fscanf(in,"%d %d %d",&Q,&M,&K);
int i, j, field, curstrip;
memo[0] = true;
int sumtree = 0;
int ans;
for ( i = 0; i < M; i++ )
{
fscanf(in,"%d",&field);
for ( j = min(Q-field,sumtree); j >= max(0,Q-150*(M-i)); j-- ) memo[j+field] |= memo[j];
sumtree += field;
if ( memo[Q] )
{
fprintf(out,"%d\n",Q);
return 0;
}
}
if ( sumtree > Q )
{
fprintf(out,"%d\n",Q-1);
return 0;
}
for ( i = 0; i < K; i++ )
{
fscanf(in,"%d",&curstrip);
strip.push_back(curstrip);
}
ans = Q;
sort(strip.begin(),strip.end());
for ( i = strip.size()-1; i >= 0; i-- )
{
sumtree += strip[i];
ans--;
if ( sumtree >= Q )
{
fprintf(out,"%d\n",ans);
return 0;
}
}
}