-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path624.cpp
More file actions
43 lines (42 loc) · 951 Bytes
/
624.cpp
File metadata and controls
43 lines (42 loc) · 951 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
#include <bits/stdc++.h>
using namespace std;
int m, k;
int a[25];
int dp[25][1500];
int ans[25][1500];
int call(int i, int n) {
if (i == m) return n;
if (dp[i][n] != -1) return dp[i][n];
int x = 0, y = 0;
if (n + a[i] <= k) x = call(i + 1, n + a[i]);
y = call(i + 1, n);
if (x >= y) {
ans[i][n] = 1;
dp[i][n] = x;
} else {
ans[i][n] = 2;
dp[i][n] = y;
}
return dp[i][n];
}
int main() {
int i, n, l, j;
while (scanf("%d %d", &k, &m) == 2) {
memset(ans, -1, sizeof(ans));
memset(dp, -1, sizeof(dp));
for (i = 0; i < m; i++) scanf("%d", &a[i]);
sort(a, a + m);
j = call(0, 0);
i = 0;
n = 0;
while (ans[i][n] != -1) {
if (ans[i][n] == 1) {
printf("%d ", a[i]);
n = n + a[i];
}
i++;
}
printf("sum:%d\n", j);
}
return 0;
}