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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumInt(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a
  }
  return sum
}

type BitSet []uint64
func NewBitSet(N int) *BitSet {
  B := BitSet(make([]uint64, N / 64 + 1))
  return &B
}
func (B *BitSet) ShOr(N int) {
  n, k := N / 64, N % 64
  for i := len((*B)) - 1; 0 <= i; i-- {
    x := i - n
    if x < 0 { break }
    (*B)[i] |= (*B)[x] << uint(k)
    if 0 < k && 1 < x { (*B)[i] |= (*B)[x - 1] >> uint(64 - k) }
  }
}
func (B *BitSet) Get(N int) uint64 {
  n, k := N / 64, N % 64
  return ((*B)[n] >> uint(k)) % 2
}

func Solve() {
  var N int
  NextInt(&N)
  var A []int
  NextIntVec(&A)
  S := SumInt(A...)
  B := NewBitSet(S)
  (*B)[0] = 1
  for _, a := range A {
    B.ShOr(a)
  }
  for i := S - S / 2; i <= S; i++ {
    if B.Get(i) == 1 {
      Write(i)
      break
    }
  }
  return
}

func main() {
  Solve()
  Output()
}