-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva562.cpp
73 lines (48 loc) · 958 Bytes
/
uva562.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
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
int n;
vector<int> coins;
int memo[25050][110];
int knapsack(int W, int coin_index)
{
if(memo[W][coin_index] != -1)
return memo[W][coin_index];
if(coin_index == n || W == 0)
return 0;
if(coins[coin_index] > W)
return knapsack(W, coin_index + 1);
return memo[W][coin_index] = max(coins[coin_index] + knapsack(W -
coins[coin_index], coin_index + 1), knapsack(W, coin_index + 1));
}
int main()
{
int T; cin >> T;
while(T--)
{
cin >> n;
if(n == 0)
cout << "0" << endl;
else
{
coins.clear();
coins.resize(n);
int sum = 0;
for(int i = 0; i < n; i++)
{
cin >> coins[i];
sum += coins[i];
}
memset(memo, -1, sizeof(memo));
int rez = knapsack(sum/2, 0);
cout << sum - 2 * rez << endl;
}
}
return 0;
}