import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val n = sc.nextInt()
  val a = (1..n).map { sc.nextLong() }

  val aLen = a.sum()
  val revA = a.reversed()

  var countF = 0L
  var indexF = 0
  for (i in 0 until n) {
    countF += a[i]
    if (countF > aLen / 2) {
      indexF = i
      break
    }
  }

  var countB = 0L
  var indexB = 0
  for (i in 0 until n) {
    countB += revA[i]
    if (countB > aLen / 2) {
      indexB = i
      break
    }
  }

  var sumF = 0L
  for (i in 0 until indexF) {
    sumF += a[i]
  }
  var sumB = 0L
  for (i in 0 until indexB) {
    sumB += revA[i]
  }

  if (indexF + indexB == n && sumF == sumB) {
    println(0)
    return
  }

  if (countF < countB) {
    var last = (countF + a[indexF]) - countB
    println(last)
  } else {
    var last = (countB + revA[indexB]) - countF
    println(last)
  }

}
