-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP562.cpp
42 lines (36 loc) · 832 Bytes
/
P562.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
#include <iostream>
#include <algorithm>
#define MAX_SUM 50000
int maxUsage(int m, int *a, int max) {
int *b = new int[max+1];
for(int i = 0; i < max+1; ++i)
b[i] = 0;
for(int coinI = 0; coinI < m; ++coinI) {
int coin = a[coinI];
for(int i = max; i >= coin; --i) {
int v = coin + b[i-coin];
if(v > b[i]) {
b[i] = v;
}
}
}
int ret = b[max];
delete[] b;
return ret;
}
int main() {
int n, m, a[100];
std::cin >> n;
for(int cas = 0; cas < n; ++cas) {
std::cin >> m;
int sum = 0;
for(int i = 0; i < m; ++i) {
std::cin >> a[i];
sum += a[i];
}
std::sort(a, a+m);
int maxHalf = maxUsage(m, a, sum/2);
//std::cerr << "Sum: " << sum << ", half usage: " << maxHalf << std::endl;
std::cout << (sum-2*maxHalf) << std::endl;
}
}