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

#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::str::*;
#[allow(unused_imports)]
use std::mem::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::usize;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let mut line = String::new();
    stdin().read_line(&mut line).unwrap();
    line.trim().to_string().parse().ok().unwrap()
}

#[allow(dead_code)]
fn read_cols<T: FromStr>() -> Vec<T> {
    let mut line = String::new();
    stdin().read_line(&mut line).unwrap();

    line.trim()
        .split_whitespace()
        .map(|s| s.parse().ok().unwrap())
        .collect()
}

/// Returns the minimum partial sum of nums,
/// greater than or equal to bound.
fn calc_partial_sum_lower_bound(
    nums: &[i64],
    bound: i64,
    sums: &[i64],
    cache: &mut Vec<HashMap<i64, i64>>,
) -> Option<i64> {
    if bound <= 0 {
        return Some(0);
    }

    let (last, remains) = match nums.split_last() {
        Some(pair) => pair,
        None => return None,
    };

    let cache_key = remains.len();

    if sums[cache_key] < bound {
        // total sum is less than bound
        return None;
    }

    if let Some(&r) = cache[cache_key].get(&bound) {
        return Some(r);
    }

    let min_without_last = calc_partial_sum_lower_bound(remains, bound, sums, cache);
    let min_with_last = calc_partial_sum_lower_bound(remains, bound - last, sums, cache);

    if min_without_last.is_none() && min_with_last.is_none() {
        return None;
    }

    let ret = min(
        min_without_last.unwrap_or(i64::max_value()),
        min_with_last.map_or(i64::max_value(), |m| m + last),
    );
    cache[cache_key].insert(bound, ret);
    Some(ret)
}

fn main() {
    let n = read::<usize>();
    let nums = {
        let mut nums = read_cols::<i64>();
        nums.sort();
        nums
    };
    assert_eq!(n, nums.len());

    // Idea:
    //  When S=a_1+...+a_n and S_i (i=1, ...) is i-th partial sum
    //  in ascending order and S_0 = 0,
    //  S_{2^n-1-i} = S - S_i,
    //  and if S_i <= S/2, then S_{2^n-1-i} >= S/2.
    //  By this fact, minimum S_k where satisfies S_k >= S/2
    //  is the median of partial sums.
    let mut cache = Vec::new();
    cache.resize(n, HashMap::<i64, i64>::new());

    let sums = nums.iter()
        .scan(0, |sum, &num| {
            *sum += num;
            Some(*sum)
        })
        .collect::<Vec<_>>();

    let total_sum = sums.last().unwrap();
    let answer =
        calc_partial_sum_lower_bound(&nums, (total_sum + 1) / 2, &sums, &mut cache).unwrap();

    println!("{}", answer);
}