-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path624.cpp
49 lines (40 loc) · 919 Bytes
/
624.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
/*
dynamic programming > subset sum
difficulty: easy
date: 24/Mar/2020
problem: print the subset of A with the max sum k, k <= K
by: @brpapa
*/
#include <iostream>
#include <cstring>
#define INF (1 << 30)
using namespace std;
int A[22], N, K;
int memo[22][22*22*22];
int dp(int n, int k) {
// n-ésimo elemento de A, soma acumulada k
if (k > K) return -INF;
int &ans = memo[n][k];
if (ans != -1) return ans;
if (n == N) return ans = k;
return ans = max(dp(n+1, A[n]+k), dp(n+1, k));
}
void recover() {
int k = 0;
for (int n = 0; n < N; n++) {
if (memo[n+1][k+A[n]] >= memo[n+1][k]) {
k += A[n];
cout << A[n] << " ";
}
}
cout << "sum:" << k << endl;
}
int main() {
while (cin >> K) {
cin >> N; for (int n = 0; n < N; n++) cin >> A[n];
memset(memo, -1, sizeof memo);
dp(0, 0);
recover();
}
return 0;
}