Problem 651
Tallest Billboard
Hard · 30 pts · Dynamic Programming
Problem
You want to build a billboard on two steel supports of equal height. You have rods of given lengths and can weld rods together; each rod is used for at most one support or not at all. Find the largest possible height of the two supports, or 0 if they cannot be made equal and non-empty. Track a map from the difference between the two supports to the largest height of the taller one; each rod can go to the taller side, the shorter side, or be left out.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the largest possible support height, or 0.
Constraints
1 <= N <= 20; 1 <= A[i] <= 1000; the sum of all rods is at most 5000
Example input
4 1 2 3 6
Example output
6
Explanation
With rods 1 2 3 6, the subsets {1, 2, 3} and {6} both have length 6. No pair of disjoint subsets reaches a larger equal sum, so the answer is 6.