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)

#[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_iterative(nums: &[usize], bound: usize, sums: &[usize]) -> usize {
    if bound <= 0 {
        return 0;
    }

    let mut dp = Vec::new();
    dp.resize(nums[0] + 1, false);
    dp[0] = true;
    dp[nums[0]] = true;

    for (i, &num) in nums.iter().enumerate().skip(1) {
        let mut next_dp = Vec::new();
        next_dp.resize(min(sums[i], bound) + 1, false);
        for j in 0..min(sums[i], bound) + 1 {
            let without_i = if j < dp.len() { dp[j] } else { false };

            let with_i = if nums[i] <= j && j - num < dp.len() {
                dp[j - num]
            } else {
                false
            };

            next_dp[j] = without_i || with_i;
        }
        dp = next_dp;
    }

    for i in (0..bound + 1).rev() {
        if dp[i] {
            return sums[nums.len() - 1] - i;
        }
    }

    0
}

fn main() {
    let n = read::<usize>();
    let nums = {
        let mut nums = read_cols::<usize>();
        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 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);
    let answer = calc_iterative(&nums, total_sum / 2, &sums);

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