-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoney Sums.cpp
115 lines (107 loc) · 2.84 KB
/
Money Sums.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(v) (v).begin(), (v).end()
#define fl fflush(stdout)
void solve() {
// beginner intuition
/*
int n; cin >> n;
vector<int> val(n);
for (int i = 0; i < n; i++) cin >> val[i];
set<int> ans;
sort(all(val));
ans.insert(val[0]);
for (int i = 1; i < n; i++) {
set<int>tmp;
tmp.insert(val[i]);
for (auto x : ans) {
tmp.insert(val[i] + x);
}
for (auto x : tmp) ans.insert(x);
tmp.clear();
}
cout << ans.size() << endl;;
for (auto x : ans) cout << x << " ";
*/
//Also solved using 0/1 knapsack
/*
int n; cin >> n;
vector<int> val(n);
for (int i = 0; i < n; i++) cin >> val[i];
vector<vector<bool>> dp(n + 1, vector<bool> (1e5 + 1, false));
int cnt = 0;
for (int i = 0; i <= n; i++) dp[i][0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 1e5; j++) {
if (j - val[i - 1] >= 0) {
if (dp[i - 1][j] == true || dp[i - 1][j - val[i - 1]] == true) {
dp[i][j] = true;
}
}
else {
dp[i][j] = dp[i - 1][j];
}
if (i == n) {
if (dp[n][j] == true) cnt++;
}
}
}
cout << cnt << endl;
for (int i = 1; i <= 1e5; i++) {
if (dp[n][i] == true) cout << i << " ";
}
*/
//Slightly more optimized way of doing it
int n, tot = 0; cin >> n;
vector<int> val(n);
for (int i = 0; i < n; i++) {
cin >> val[i];
tot += val[i];
}
vector<bool> dp(tot + 1, false);
dp[0] = true;
for (int i = 0; i < n; i++) {
for (int j = tot; j >= 0; j--) {
if (j - val[i] >= 0) {
if (dp[j] == true || dp[j - val[i]] == true)
dp[j] = true;
}
}
}
vector<int> ans;
for (int i = 1; i <= tot; i++) {
if (dp[i] == true) ans.pb(i);
}
cout << ans.size() << endl;
for (auto x : ans) cout << x << " ";
}
signed main() {
auto start = high_resolution_clock::now();
IOS;
int t = 1;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
// cin >> t;
int i = 1;
while (t--) {
// cout << "CASE #" << i << ": ";
solve();
cout << endl;
i++;
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cerr << "Time taken:" << duration.count() / 1000000.0 << " seconds" << "\n";
return 0;
}