-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva562.cpp
More file actions
58 lines (51 loc) · 1.22 KB
/
uva562.cpp
File metadata and controls
58 lines (51 loc) · 1.22 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
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
bool visited[50010];
vector<int> coins;
vector<int> allpossible;
vector<int> tmp;
int n, m, v, sumofall;
int newv, curv;
double halfv;
bool isnear(const int a, const int b) {
return abs(sumofall - 2*a) < abs(sumofall - 2*b);
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
coins.clear();
allpossible.clear();
sumofall = 0;
memset(visited, false, sizeof(visited));
scanf("%d", &m);
for(int j = 0; j < m; ++j) {
scanf("%d", &v);
coins.push_back(v);
sumofall += v;
}
halfv = sumofall/2.0;
visited[sumofall] = true;
allpossible.push_back(sumofall);
for(int j = 0; j < m; ++j) {
tmp.clear();
curv = coins[j];
for(int k = 0; k < allpossible.size(); ++k) {
newv = allpossible[k] - curv;
if(!visited[newv]) {
visited[newv] = true;
tmp.push_back(newv);
}
}
for(int l = 0; l < tmp.size(); ++l) {
allpossible.push_back(tmp[l]);
}
}
sort(allpossible.begin(), allpossible.end(), isnear);
printf("%d\n", abs(sumofall - 2*allpossible[0]));
}
return 0;
}