fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val aa = readLine()!!.split(" ").map { it.toLong() }
    val len = aa.sum()
    var ll = LongArray(n){0}
    val half = len/2L

    ll[0] = aa[0]
    for (i in 1 until aa.size) {
        ll[i] = ll[i-1] + aa[i]
    }

    var min = len
    for (i in 0 until n-1) {
        val left = ll[i]
        val right = len - left

        val leftDiff = Math.abs(half-left)
        val rightDiff = Math.abs(half-right)
        val diff = leftDiff + rightDiff
        if (diff < min) min = diff
    }

    println(min)
}