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 Rust (1.15.1) (judge verdict: Wrong Answer)

mod bitset {
    const W: usize = 64;
    #[derive(Clone)]
    pub struct BitSet {
        size: usize,
        a: Vec<usize>,
    }
    fn qr(x: usize) -> (usize, usize) {
        (x / W, x % W)
    }
    impl BitSet {
        pub fn new(n: usize) -> Self {
            BitSet {
                size: n,
                a: vec![0; (n + W - 1) / W],
            }
        }
        pub fn get_size(&self) -> usize {
            self.size
        }
        pub fn set_at(&mut self, x: usize) {
            debug_assert!(x < self.size);
            let (q, r) = qr(x);
            self.a[q] |= 1 << r;
        }
        pub fn get_at(&self, x: usize) -> bool {
            debug_assert!(x < self.size);
            let (q, r) = qr(x);
            ((self.a[q] >> r) & 1) == 1
        }
    }
    use std::ops::{Shl, BitOr};
    impl BitOr for BitSet {
        type Output = Self;
        fn bitor(self, rhs: BitSet) -> BitSet {
            let (mut x, y) = if self.size > rhs.size {(self, rhs)} else {(rhs, self)};
            for (t, f) in x.a.iter_mut().zip(y.a.iter()) {
                *t |= *f;
            }
            x
        }
    }
    impl Shl<usize> for BitSet {
        type Output = Self;
        fn shl(self, rhs: usize) -> BitSet {
            let n = self.size;
            let m = n + rhs;
            let mut next = BitSet::new(m);
            let (q, r) = qr(rhs);
            if r == 0 {
                for (t, f) in next.a[q..].iter_mut().zip(self.a.iter()) {
                    *t = *f;
                }
                return next;
            }
            next.a[q] |= self.a[0] << r;
            let mut prev = self.a[0];
            for (t, f) in next.a[(q + 1)..].iter_mut().zip(self.a[1..].iter()) {
                *t = (*f << r) | (prev >> (W - r));
                prev = *f;
            }
            next
        }
    }
}

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// ここまで

fn main(){
    input! {
        n: usize,
        a: [usize; n],
    }
    let mut b = bitset::BitSet::new(1);
    b.set_at(0);
    for a in a {
        b = b.clone() | (b << a);
    }
    let mut ans = (b.get_size() - 1 + 1) / 2;
    while !b.get_at(ans) {
        ans += 1;
    }
    println!("{}", ans);
}