import sys



input = sys.stdin.readline





def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))



    sum_A = sum(A)

    center = sum_A / 2

    total = 0

    near_center = 0

    for a in A:

        total += a

        if abs(total - center) < abs(near_center - center):

            near_center = total



    ans = abs((sum_A - near_center) - near_center)

    print(ans)





if __name__ == "__main__":

    main()

import sys

from itertools import accumulate



input = sys.stdin.readline





def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))



    cumsum_A = list(accumulate(A))

    sum_A = sum(A)



    ans = float("inf")

    for i in range(N - 1):

        cost = abs((sum_A - cumsum_A[i]) - cumsum_A[i])

        if cost < ans:

            ans = cost



    print(ans)





if __name__ == "__main__":

    main()

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    sum_A = sum(A)
    center = sum_A / 2
    total = 0
    near_center = 0
    for a in A:
        total += a
        if abs(total - center) < abs(near_center - center):
            near_center = total
    ans = abs((sum_A - near_center) - near_center)
    print(ans)


if __name__ == "__main__":
    main()

import sys
from itertools import accumulate

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    cumsum_A = list(accumulate(A))
    sum_A = sum(A)
    ans = float("inf")
    for i in range(N - 1):
        cost = abs((sum_A - cumsum_A[i]) - cumsum_A[i])
        if cost < ans:
            ans = cost
    print(ans)


if __name__ == "__main__":
    main()

+from itertools import accumulate +    cumsum_A = list(accumulate(A)) -    center = sum_A / 2 -    total = 0 -    near_center = 0 -    for a in A: -        total += a -        if abs(total - center) < abs(near_center - center): -            near_center = total -    ans = abs((sum_A - near_center) - near_center) +    ans = float("inf") +    for i in range(N - 1): +        cost = abs((sum_A - cumsum_A[i]) - cumsum_A[i]) +        if cost < ans: +            ans = cost