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: Accepted)

//httips://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")
    };
}

//


//固定長
#[derive(Clone)]
struct BitSet {
    size: usize,
    a: Vec<usize>,
}

fn quot_rem(n: usize) -> (usize, usize) {
    let w = 8 * std::mem::size_of::<usize>();
    (n / w, n % w)
}

impl BitSet {
    fn new(size: usize) -> Self {
        let w = 8 * std::mem::size_of::<usize>();
        BitSet {
            size: size,
            a: vec![0; (size + w - 1) / w],
        }
    }
    fn set_at(&mut self, x: usize) {
        assert!(x < self.size);
        let (q, r) = quot_rem(x);
        self.a[q] |= 1 << r;
    }
    fn get_at(&self, x: usize) -> usize {
        assert!(x < self.size);
        let (q, r) = quot_rem(x);
        (self.a[q] >> r) & 1
    }
    fn fix(&mut self) {
        let (q, r) = quot_rem(self.size);
        if r != 0 {
            self.a[q] &= (1 << r) - 1;
        }
    }
    fn clear(&mut self) {
        let len = self.a.len();
        self.a.clear();
        self.a.resize(len, 0);
    }
}

use std::ops::*;

impl BitAndAssign for BitSet {
    fn bitand_assign(&mut self, rhs: Self) {
        assert!(self.size == rhs.size);
        for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) {
            *a &= *b;
        }
    }
}

impl BitAnd for BitSet {
    type Output = Self;
    fn bitand(mut self, rhs: Self) -> Self {
        self &= rhs;
        self
    }
}

impl BitOrAssign for BitSet {
    fn bitor_assign(&mut self, rhs: Self) {
        assert!(self.size == rhs.size);
        for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) {
            *a |= *b;
        }
    }
}

impl BitOr for BitSet {
    type Output = Self;
    fn bitor(mut self, rhs: Self) -> Self {
        self |= rhs;
        self
    }
}

impl ShlAssign<usize> for BitSet {
    fn shl_assign(&mut self, rhs: usize) {
        let (q, r) = quot_rem(rhs);
        if q >= self.a.len() {
            self.clear();
            return;
        }
        if r == 0 {
            let a = &mut self.a;
            for i in (q..a.len()).rev() {
                a[i] = a[i - q];
            }
        } else {
            let w = 8 * std::mem::size_of::<usize>();
            let a = &mut self.a;
            for i in ((q + 1)..a.len()).rev() {
                let x = a[i - q];
                let y = a[i - q - 1];
                a[i] = (x << r) | (y >> (w - r));
            }
            a[q] = a[0] << r;
        }
        for i in (0..q).rev() {
            self.a[i] = 0;
        }
        self.fix();
    }
}

impl Shl<usize> for BitSet {
    type Output = Self;
    fn shl(mut self, rhs: usize) -> Self {
        self <<= rhs;
        self
    }
}

fn run() {
    input! {
        n: usize,
        a: [usize; n],
    }
    let sum = a.iter().fold(0, |s, a| s + *a);
    let mut bit = BitSet::new(sum + 1);
    bit.set_at(0);
    for a in a {
        bit |= bit.clone() << a;
    }
    let mut ans = (sum + 1) / 2;
    while bit.get_at(ans) == 0 {
        ans += 1;
    }
    println!("{}", ans);
}

fn main() {
    run();
}