-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-624.cpp
More file actions
47 lines (37 loc) · 736 Bytes
/
UVA-624.cpp
File metadata and controls
47 lines (37 loc) · 736 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
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll total, n;
vector<ll> setl;
ll maskresp, nearestTotal;
void verif(int atual, int mask, int p) {
if (atual > total)
return;
if (atual > nearestTotal) {
nearestTotal = atual;
maskresp = mask;
}
if (p >= n)
return;
verif(atual + setl[p], mask | (1 << p), p + 1);
verif(atual, mask, p + 1);
}
int main() {
while (cin >> total >> n) {
setl.clear();
for (int i = 0; i < n; i++) {
ll temp;
cin >> temp;
setl.push_back(temp);
}
maskresp = -1;
nearestTotal = -1;
verif(0, 0, 0);
for (int i = 0; i < n; i++) {
if ((maskresp >> i) % 2)
printf("%lld ", setl[i]);
}
printf("sum:%lld\n", nearestTotal);
}
return 0;
}