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

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <ctime>
#include <complex>

using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef unsigned long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL) INF;

const double EPS = 1e-6;
const int MAX = 2005+1;
const int MAXX = MAX * MAX / 64 + 10;

LL DP[2][MAXX];
int A[MAX];

int main()
{
    int n;
    scanf("%d", &n);
    FOR(i, 0, n)
        scanf("%d", A + i);
    int sum = 0;
    FOR(i, 0, n)
        sum += A[i];
    DP[0][0] = 1;
    int lim = MAXX;
    int C = 0, N = 1;
    FOR(i, 0, n)
    {
        FOR(j, 0, lim)
        {
            DP[N][j] |= DP[C][j];
            int full = A[i] / 64;
            int part = A[i] % 64;
            int jj = j + full;
            LL res = DP[C][j] << part;
            LL res2 = DP[C][j] >> (64 - part);
            if (jj < lim)DP[N][jj] |= res;
            if (jj + 1 < lim)DP[N][jj+1] |= res2;
        }
        swap(C, N);
        FOR(i, 0, lim)
            DP[N][i] = 0;
    }
    
    FOR(i, (sum + 1) / 2, sum+1)
    if ((DP[C][i / 64] & (1ULL << (i % 64))) > 0)
    {
        cout << i << endl;
        return 0;
    }
}