import scala.io.StdIn

object Main extends App {
  val n = StdIn.readInt
  val a = StdIn.readLine.split(" ").map(_.toLong)
  val sum = a.sum
  val initial = ((a.head-(sum-a.head)).abs, a.head)

  val ans = a.tail.init./:(initial)((acc, i) => {
      val x = acc._2 + i
      val y = sum - x
      val xy = math.min((x-y).abs, acc._1)
      (xy, x)
    })._1

  println(ans)
}