USACO Stamps

Code

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
/*
ID: wcr19961
PROG: stamps
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 2000010;
int dp[maxn];

int main() {
freopen("stamps.in", "r", stdin);
freopen("stamps.out", "w", stdout);
int k, n, x;
scanf("%d%d", &k, &n);
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
for (int j = x; j < maxn; ++j) {
if (dp[j - x] < k) {
dp[j] = min(dp[j], dp[j - x] + 1);
}
}
}
int ans = 0;
while (dp[ans + 1] != inf) {
++ans;
}
printf("%d\n", ans);
return 0;
}