### subroutines

def rec(k, sum)
  return sum if k >= $n

  max_sum = rec(k + 1, sum)

  sumk = sum + $as[k]
  if sumk <= $cap
    sum0 = rec(k + 1, sumk)
    max_sum = sum0 if max_sum < sum0
  end

  max_sum
end

### main

loop do
  $n = gets.to_i
  break if $n == 0

  $as = gets.split.map(&:to_i)
  #p $as

  sum = $as.inject(:+)
  $cap = sum / 2
  #p $cap

  max_pow = rec(0, 0)
  puts sum - max_pow * 2
end