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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef unsigned long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define B 60
int a[2018];
lint dp[2][(4000010)/B+100];
vector<int> v;
int main()
{
	int n,sum=0,out=1001001001;cin>>n;
	rep(i,n){
		cin>>a[i];
		sum+=a[i];
	}
	memset(dp,0LL,sizeof(dp));
	dp[0][0]=1;
	rep(i,n){
		int now=i%2,ne=1-now;
		int s=a[i]/B,mo=a[i]%B;
		memset(dp[ne],0LL,sizeof(dp[ne]));
		rep(j,(4000010)/B+5){
			if(dp[now][j]<1LL) continue;
			//cout<<i<<' '<<j<<' '<<dp[now][j]<<endl;
			dp[ne][j]|=dp[now][j];
			//cout<<((1LL<<(B-mo))-1)<<' '<<(dp[now][j]&((1LL<<(B-mo))-1))<<endl;
			//dp[ne][j+s]|=((dp[now][j]&((1LL<<(B-mo))-1))<<mo);
			dp[ne][j+s]|=(dp[now][j]<<mo);
			//dp[ne][j+s+1]|=(dp[now][j]/(1LL<<(B-mo)));
			dp[ne][j+s+1]|=(dp[now][j]>>(B-mo));
		}
	}
	rep(j,(4000010)/B+5){
		int now=n%2;
		if(dp[now][j]<1LL) continue;
		//cout<<n<<' '<<j<<' '<<dp[now][j]<<endl;
	}
	rep(i,4000001){
		if((dp[n%2][i/B]&(1LL<<(i%B))) && i>=sum/2){
			out=min(out,i);
		}
	}
	assert(out<1001001001);
	cout<<out<<endl;
}