-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneySums.cpp
More file actions
58 lines (48 loc) · 1.11 KB
/
MoneySums.cpp
File metadata and controls
58 lines (48 loc) · 1.11 KB
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
//Money Sums - https://cses.fi/problemset/task/1745
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int N = 1e5 + 2;
void solve() {
int n;
cin >> n;
vector<int> coins(n);
for (int i = 0; i < n; i++) {
cin >> coins[i];
}
vector<vector<bool>> dp(N, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
dp[0][i] = true;
}
dp[coins[0]][0] = true;
for (int i = 1; i < N; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i][j - 1];
if (i - coins[j] < 0) continue;
dp[i][j] = dp[i][j] | (dp[i - coins[j]][j - 1]);
}
}
vector<int> ans;
for (int i = 1; i < N; i++) {
if (dp[i][n - 1]) {
ans.push_back(i);
}
}
int m = (int) ans.size();
cout << m << endl;
for (int i = 0; i < m; i++) {
cout << ans[i] << " \n"[i == m - 1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}