-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00574 Sum It Up.cpp
60 lines (54 loc) · 1.37 KB
/
00574 Sum It Up.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
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pb push_back
using namespace std;
vector <ll> in, resp;
set <vector <ll>> solutions;
vector <vector<ll>> table;
bool cmp(ll a, ll b) {
return b < a;
}
void back_track(ll k, ll t, ll n) {
if(k == n) {
ll soma = 0;
for(ll i = 0; i < resp.size(); i++) soma += resp[i];
if(soma != t) return;
sort(resp.begin(), resp.end(), cmp);
solutions.insert(resp);
} else {
resp.pb(in[k]);
back_track(k + 1, t, n);
resp.pop_back();
back_track(k + 1, t, n);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie();
ll t, n;
while(cin >> t >> n) {
if(n == 0) break;
cout << "Sums of " << t << ":" << endl;
in.clear(), in.resize(n);
solutions.clear(), table.clear();
for(ll i = 0; i < n; i++) {
cin >> in[i];
}
back_track(0, t, n);
if(solutions.size() == 0) {
cout << "NONE" << endl;
continue;
}
for(auto vec : solutions) {
table.pb(vec);
}
for(ll i = table.size() -1; i >= 0; i--) {
for(ll j = 0; j < table[i].size(); j++) {
if(j == 0) cout << table[i][j];
else cout << "+" << table[i][j];
}
cout << endl;
}
}
}