
object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  solve
  def solve: Unit ={
    val n = readLine.trim.toInt
    if (n == 0) return
    val powers = readLine.trim.split(' ').map(_.toLong)
    val target = powers.sum
    println(
      powers.map{it ⇒ List(it, 0L)}.foldLeft(List(0L)){
        case (l, r) ⇒ l.flatMap(it ⇒ r.map(_ + it))
      }.map(it ⇒ math.abs(target - it - it)).min
    )

    solve
  }


  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
