Problem:
Score : 700 points

Problem Statement

You are given N integers A_1, A_2, ..., A_N.

Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}.

Find the median of this list, S_{2^{N-1}}.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq 2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.

Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S = (1, 1, 2, 2, 3, 3, 4). Its median is S_4 = 2.

Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).

Solution in Pascal (FPC 2.6.2) (judge verdict: Runtime Error)

program V8122A;
 var
  f:Array[0..125001] of longword;
  a,b:Array[0..2001] of longint;
  i,j,n,p,s:longint;
  t:longword;
 begin
  readln(n);
  s:=0;
  for i:=1 to n do
   begin
    read(A[i]);
    b[i]:=a[i] mod 30;
    inc(s,a[i]);
   end;
  readln;
  t:=2<<30-1;
  //writeln(t);
  filldword(f,sizeof(f)>>2,0);
  f[1]:=1;
  for i:=1 to n do
   for j:=s div 30+1 downto a[i] div 30+1 do
    f[j]:=(f[j-a[i] div 30]<<(b[i])) and t or f[j];
  p:=0;
  for i:=1 to s div 30+1 do
   for j:=1 to 30 do
    if ((1<<(j-1)) and f[i]=1<<(j-1)) and ((i-1)*30+j-1<=s>>1) then p:=(i-1)*30+j-1;
  writeln(s-p);
 end.