1.
/**
* FWP,
AusgewŠhlte Probleme aus dem ACM Programming Contest, SS10
*
Problem: acm_11851_celebrity_split
* Link:
*
*
@author Martin Lambeck
*
@version 1.0, 02.10.2010
*
*
Method :
*
Status : Accepted
*
Runtime: 3.868
*/
package
acm_11851_celebrity_split;
import
java.util.HashMap;
import
java.util.Scanner;
public class
Main
{
static
Scanner sc = new Scanner(System.in);
static
int best = 0;
static
int[] nums;
static
final int MAXN = 4900000;
//static
boolean[] diff = new boolean[MAXN * 12 + 1];
static
HashMap<Integer, Integer> diff = new HashMap<Integer,
Integer>(600000, 1.0f);
static
boolean fill = false;
static
int end = 0;
public
static void main(String... args)
{
while (testcase())
;
}
public
static boolean testcase()
{
int n = sc.nextInt();
if (n == 0)
return false;
nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = sc.nextInt();
int total = 0;
for (int i : nums)
total += i;
best = 0;
diff.clear();
diff.put(0, 0);
fill = true;
end = n/2;
find(0, 0, 0);
fill = false;
end = n;
find(n/2, 0, 0);
System.out.println(total - 2*best);
return true;
}
private
static void find(int p, int jack, int jill)
{
int df = (jack >= jill ? jack - jill : jill -
jack);
if (fill)
{
if ((df == 0) && (jack
> best))
best =
jack;
int min = (jack <= jill ? jack
: jill);
Integer n = diff.get(df);
if ((n == null) || (min > n))
diff.put(df,
min);
} else if (diff.containsKey(df))
{
int max = (jack >= jill ? jack
: jill) + diff.get(df);
if (max > best)
best = max;
}
if (p >= end)
return;
find(p+1, jack+nums[p], jill);
find(p+1, jack, jill+nums[p]);
find(p+1, jack, jill);
}
}