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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading;

namespace AtCoder
{
	public class Program
	{
		static void Main()
		{
			var cin = new Scanner();
			int n = cin.Int();
			var a = cin.ArrayInt(n);

			int max = n * a.Max() + 1;
			var dp = new Bitset(max);
			dp[0] = true;
			for (int i = 0; i < n; i++) {
				dp |= (dp << a[i]);
			}

			int sum = a.Sum();
			int target = (sum + 1) / 2;
			int ans = sum;
			for (int i = target; i <= sum; i++) {
				if (dp[i]) {
					ans = i;
					break;
				}
			}

			Console.WriteLine(ans);
		}
	}

	public class Bitset
	{
		public static int PopCount(ulong bits)
		{
			bits = ((bits & 0xaaaaaaaaaaaaaaaa) >> 1) + (bits & 0x5555555555555555);
			bits = ((bits & 0xcccccccccccccccc) >> 2) + (bits & 0x3333333333333333);
			bits = ((bits & 0xf0f0f0f0f0f0f0f0) >> 4) + (bits & 0x0f0f0f0f0f0f0f0f);
			bits = ((bits & 0xff00ff00ff00ff00) >> 8) + (bits & 0x00ff00ff00ff00ff);
			bits = ((bits & 0xffff0000ffff0000) >> 16) + (bits & 0x0000ffff0000ffff);
			bits = ((bits & 0xffffffff00000000) >> 32) + (bits & 0x00000000ffffffff);
			return (int)bits;
		}

		private readonly int length_;
		private readonly ulong[] bits_;

		public int Size { get; }
		public Bitset(int size, bool fillTrue = false)
		{
			Size = size;
			length_ = ((size - 1) >> 6) + 1;
			bits_ = new ulong[length_];
			if (fillTrue) {
				int y = size % 64;
				if (y == 0) {
					for (int i = 0; i < length_; i++) {
						bits_[i] = ulong.MaxValue;
					}
				} else {
					for (int i = 0; i < length_ - 1; i++) {
						bits_[i] = ulong.MaxValue;
					}

					bits_[length_ - 1] = (1UL << y) - 1;
				}
			}
		}
		public Bitset(int size, long initialValue)
		{
			Size = size;
			length_ = ((size - 1) >> 6) + 1;
			bits_ = new ulong[length_];
			bits_[0] = (ulong)initialValue;
		}

		public bool this[int i]
		{
			get { return ((bits_[i >> 6] >> i) & 1) != 0; }
			set { bits_[i >> 6] = (bits_[i >> 6] & (ulong.MaxValue ^ (1ul << i))) | ((ulong)(value ? 1 : 0) << i); }
		}

		public static Bitset operator &(Bitset lhs, Bitset rhs)
		{
			int min = Math.Min(lhs.length_, rhs.length_);

			var ret = new Bitset(Math.Max(lhs.Size, rhs.Size));
			for (int i = 0; i < min; i++) {
				ret.bits_[i] = lhs.bits_[i] & rhs.bits_[i];
			}

			return ret;
		}
		public static Bitset operator |(Bitset lhs, Bitset rhs)
		{
			int max = Math.Max(lhs.length_, rhs.length_);
			int min = Math.Min(lhs.length_, rhs.length_);
			var ret = new Bitset(Math.Max(lhs.Size, rhs.Size));

			for (int i = 0; i < min; i++) {
				ret.bits_[i] = lhs.bits_[i] | rhs.bits_[i];
			}

			if (lhs.length_ > rhs.length_) {
				for (int i = min; i < max; i++) {
					ret.bits_[i] = lhs.bits_[i];
				}
			} else {
				for (int i = min; i < max; i++) {
					ret.bits_[i] = rhs.bits_[i];
				}
			}

			return ret;
		}
		public static Bitset operator ^(Bitset lhs, Bitset rhs)
		{
			int max = Math.Max(lhs.length_, rhs.length_);
			int min = Math.Min(lhs.length_, rhs.length_);
			var ret = new Bitset(Math.Max(lhs.Size, rhs.Size));

			for (int i = 0; i < min; i++) {
				ret.bits_[i] = lhs.bits_[i] ^ rhs.bits_[i];
			}

			if (lhs.length_ > rhs.length_) {
				for (int i = min; i < max; i++) {
					ret.bits_[i] = lhs.bits_[i];
				}
			} else {
				for (int i = min; i < max; i++) {
					ret.bits_[i] = rhs.bits_[i];
				}
			}

			return ret;
		}
		public static Bitset operator <<(Bitset target, int shift)
		{
			var ret = new Bitset(target.Size);
			if (shift > target.length_ << 6) {
				return ret;
			}

			int minIndex = (shift + 63) >> 6;
			if (shift % 64 == 0) {
				for (int i = target.length_ - 1; i >= minIndex; i--) {
					ret.bits_[i] = target.bits_[i - minIndex];
				}
			} else {
				for (int i = target.length_ - 1; i >= minIndex; i--) {
					ret.bits_[i] = (target.bits_[i - minIndex + 1] << shift)
						| (target.bits_[i - minIndex] >> (64 - shift));
				}

				ret.bits_[minIndex - 1] = target.bits_[0] << shift;
			}

			return ret;
		}
		public static Bitset operator >>(Bitset target, int shift)
		{
			var ret = new Bitset(target.Size);
			if (shift > target.length_ << 6) {
				return ret;
			}

			int minIndex = (shift + 63) >> 6;
			if (shift % 64 == 0) {
				for (int i = 0; i + minIndex < ret.length_; i++) {
					ret.bits_[i] = target.bits_[i + minIndex];
				}
			} else {
				for (int i = 0; i + minIndex < ret.length_; i++) {
					ret.bits_[i] = (target.bits_[i + minIndex - 1] >> shift)
						| (target.bits_[i + minIndex] << (64 - shift));
				}

				ret.bits_[ret.length_ - minIndex] = target.bits_[ret.length_ - 1] >> shift;
			}

			return ret;
		}
		public static bool operator ==(Bitset lhs, Bitset rhs)
		{
			int max = Math.Max(lhs.length_, rhs.length_);
			int min = Math.Min(lhs.length_, rhs.length_);
			for (int i = 0; i < min; i++) {
				if (lhs.bits_[i] != rhs.bits_[i]) {
					return false;
				}
			}

			if (lhs.length_ > rhs.length_) {
				for (int i = min; i < max; i++) {
					if (lhs.bits_[i] != 0) {
						return false;
					}
				}
			} else {
				for (int i = min; i < max; i++) {
					if (rhs.bits_[i] != 0) {
						return false;
					}
				}
			}

			return true;
		}
		public static bool operator !=(Bitset lhs, Bitset rhs)
		{
			return !(lhs == rhs);
		}
		public override bool Equals(object obj)
		{
			return false;
		}

		public override int GetHashCode()
		{
			return HashCode.Combine(length_, bits_, Size);
		}

		public override string ToString()
		{
			string ret = "";
			for (int i = 0; i < length_ - 1; i++) {
				string temp = Convert.ToString((long)bits_[i], 2);
				temp = (64 > temp.Length ? new string('0', 64 - temp.Length) : "")
					+ temp;
				ret = temp + ret;
			}

			{
				string temp = Convert.ToString((long)bits_[length_ - 1], 2);
				temp = ((Size % 64) > temp.Length ? new string('0', (Size % 64) - temp.Length) : "")
					+ temp;
				ret = temp + ret;
			}

			return ret;
		}
	}

	public struct ModInt
	{
		public const long P = 1000000007;
		//public const long P = 998244353;

		public static ModInt Inverse(ModInt value) => Pow(value, P - 2);
		public static ModInt Pow(ModInt value, long k) => Pow(value.value_, k);
		public static ModInt Pow(long value, long k)
		{
			long ret = 1;
			for (k %= P - 1; k > 0; k >>= 1, value = value * value % P) {
				if ((k & 1) == 1) {
					ret = ret * value % P;
				}
			}
			return new ModInt(ret);
		}

		private long value_;

		public ModInt(long value)
			=> value_ = value;
		public ModInt(long value, bool mods)
		{
			if (mods) {
				value %= P;
				if (value < 0) {
					value += P;
				}
			}

			value_ = value;
		}

		public static ModInt operator +(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = (lhs.value_ + rhs.value_) % P;
			return lhs;
		}
		public static ModInt operator +(long lhs, ModInt rhs)
		{
			rhs.value_ = (lhs + rhs.value_) % P;
			return rhs;
		}
		public static ModInt operator +(ModInt lhs, long rhs)
		{
			lhs.value_ = (lhs.value_ + rhs) % P;
			return lhs;
		}

		public static ModInt operator -(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = (P + lhs.value_ - rhs.value_) % P;
			return lhs;
		}
		public static ModInt operator -(long lhs, ModInt rhs)
		{
			rhs.value_ = (P + lhs - rhs.value_) % P;
			return rhs;
		}
		public static ModInt operator -(ModInt lhs, long rhs)
		{
			lhs.value_ = (P + lhs.value_ - rhs) % P;
			return lhs;
		}

		public static ModInt operator *(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = lhs.value_ * rhs.value_ % P;
			return lhs;
		}
		public static ModInt operator *(long lhs, ModInt rhs)
		{
			rhs.value_ = lhs * rhs.value_ % P;
			return rhs;
		}
		public static ModInt operator *(ModInt lhs, long rhs)
		{
			lhs.value_ = lhs.value_ * rhs % P;
			return lhs;
		}

		public static ModInt operator /(ModInt lhs, ModInt rhs)
		{
			long exp = P - 2;
			while (exp > 0) {
				if (exp % 2 > 0) {
					lhs *= rhs;
				}

				rhs *= rhs;
				exp /= 2;
			}

			return lhs;
		}

		public static implicit operator ModInt(long n) => new ModInt(n, true);
		public long ToLong() => value_;
		public override string ToString() => value_.ToString();
	}

	public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
	{
		private readonly Func<TKey, TValue> initialzier_;
		public HashMap(Func<TKey, TValue> initialzier)
			: base()
		{
			initialzier_ = initialzier;
		}

		public HashMap(Func<TKey, TValue> initialzier, int capacity)
			: base(capacity)
		{
			initialzier_ = initialzier;
		}

		new public TValue this[TKey key]
		{
			get
			{
				if (ContainsKey(key) == false) {
					base[key] = initialzier_(key);
				}

				return base[key];
			}

			set { base[key] = value; }
		}
	}

	public static class Helper
	{
		public static void UpdateMin<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) > 0 ? value : target;

		public static void UpdateMin<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) > 0) {
				target = value;
				onUpdated(value);
			}
		}

		public static void UpdateMax<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) < 0 ? value : target;
		public static void UpdateMax<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) < 0) {
				target = value;
				onUpdated(value);
			}
		}

		public static T[] Array<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToArray();
		public static List<T> List<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToList();
		public static T[,] Array2<T>(int n, int m, T init)
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = init;
				}
			}

			return array;
		}

		public static T[,] Array2<T>(int n, int m, Func<int, int, T> initializer)
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = initializer(i, j);
				}
			}

			return array;
		}

		public static T[,,] Array3<T>(int n1, int n2, int n3, T init)
		{
			var array = new T[n1, n2, n3];
			for (int i1 = 0; i1 < n1; i1++) {
				for (int i2 = 0; i2 < n2; i2++) {
					for (int i3 = 0; i3 < n3; i3++) {
						array[i1, i2, i3] = init;
					}
				}
			}

			return array;
		}

		private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void DoAt4(int i, int j, int imax, int jmax, Action<int, int> action)
		{
			for (int dn = 0; dn < 4; dn++) {
				int d4i = i + delta4_[dn];
				int d4j = j + delta4_[dn + 1];
				if ((uint)d4i < (uint)imax && (uint)d4j < (uint)jmax) {
					action(d4i, d4j);
				}
			}
		}

		private static readonly int[] delta8_ = { 1, 0, -1, 0, 1, 1, -1, -1, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void DoAt8(int i, int j, int imax, int jmax, Action<int, int> action)
		{
			for (int dn = 0; dn < 8; dn++) {
				int d8i = i + delta8_[dn];
				int d8j = j + delta8_[dn + 1];
				if ((uint)d8i < (uint)imax && (uint)d8j < (uint)jmax) {
					action(d8i, d8j);
				}
			}
		}
	}

	public class Scanner
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private readonly Func<string> reader_;
		private string[] buf_;
		private int index_;

		public Scanner(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}

		public string NextLine() => reader_();
		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = reader_();
			while (st == "") {
				st = reader_();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}