fun main(args: Array<String>) {
    ddcc2020qualb()
}

fun ddcc2020qualb() {
    val n = readLine()!!.toInt()
    val aList = readLine()!!.split(' ').map { it.toLong() }.toLongArray()

    val sum = aList.sum()
    val halved = sum / 2 + if (sum % 2 == 0L) 0 else 1

    var currSum = 0L
    var i = 0
    while (currSum < halved && i < aList.lastIndex) currSum += aList[i++]

    val answer = Math.min(
        Math.abs(aList.slice(0..i - 1).sum() - aList.slice(i..aList.lastIndex).sum()),
        Math.abs(aList.slice(0..i - 2).sum() - aList.slice(i - 1..aList.lastIndex).sum())
    )

    println(answer)
}
