-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path562.cpp
More file actions
54 lines (42 loc) · 958 Bytes
/
Copy path562.cpp
File metadata and controls
54 lines (42 loc) · 958 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
48
49
50
51
52
53
54
/*
Author: Andreea Musat
Date: 15 sept 2017
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=503
*/
#include <bits/stdc++.h>
using namespace std;
int dp[128][(1 << 16)];
/* Top down knapsack for half of the total sum */
int knapsack(const vector<int>& arr, int n, int w)
{
if (n == 0 || w == 0)
return 0;
if (dp[n][w] != 0)
return dp[n][w];
int& ans = dp[n][w];
if (arr[n - 1] > w)
return ans = knapsack(arr, n - 1, w);
return ans = max(knapsack(arr, n - 1, w),
knapsack(arr, n - 1, w - arr[n - 1]) + arr[n - 1]);
}
int main()
{
int n, m, total_sum;
vector<int> arr; cin >> n;
while (n--)
{
cin >> m;
if (arr.size() > 0)
arr.clear();
arr.resize(m);
total_sum = 0;
memset(dp, 0, sizeof dp);
for (int i = 0; i < m; i++)
{
cin >> arr[i];
total_sum += arr[i];
}
cout << total_sum - 2 * knapsack(arr, m, total_sum / 2) << "\n";
}
return 0;
}