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: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define sc3(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define pri(x) printf("%d\n", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define imprime(v) for(int X=0;X<v.size();X++) printf("%d ", v[X]); printf("\n");
#define endl "\n"
const int INF= 0x3f3f3f3f;
const long double pi= acos(-1);
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< vector< int > > vvi;
const int MOD=1e9+7;
const ll LINF=0x3f3f3f3f3f3f3f3f;

bitset<4000005> can;
int n, a[2005];
int toadd[4000004];
int id;

bool ok(int i) { return can[i]; }

int main() {
  sc(n);
  int sum = 0;
  int max_value = 0;
  for(int i = 0; i < n; i++) {
    sc(a[i]);
    sum += a[i];
    int new_max = max_value;
    id = 0;
    for(int j = 0; j <= max_value; j++) {
      if(can[j] and !can[a[i] + j]) toadd[id++] = a[i] + j;
    }
    max_value = max(toadd[id - 1], max_value);
    if(!can[a[i]]) toadd[id++] = a[i];
    max_value = max(toadd[id - 1], max_value);
    for(int j = 0; j < id; j++) can[toadd[j]] = 1;

  }
  int target = (sum + 1) / 2;
  for(int i = target; i <= sum; i++) {
    if(ok(i)) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}