def main():

    import sys

    from itertools import accumulate



    n = int(eval(input()))

    a = list(map(int, sys.stdin.readline().split()))



    b = list(accumulate(a))

    diff = b[-1]

    total = b[-1]

    for x in b:

        diff = min(diff, abs(total - 2 * x))

    print(diff)



if __name__ == '__main__':

     main()

def main():

    import sys

    import bisect

    from itertools import accumulate



    n = int(eval(input()))

    a = list(map(int, sys.stdin.readline().split()))



    b = list(accumulate(a))

    l = b[-1]

    i = bisect.bisect(b, l // 2)

    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))

    print(ans)





if __name__ == '__main__':

    main()

def main():
    import sys
    from itertools import accumulate

    n = int(eval(input()))
    a = list(map(int, sys.stdin.readline().split()))
    b = list(accumulate(a))
    diff = b[-1]
    total = b[-1]
    for x in b:
        diff = min(diff, abs(total - 2 * x))
    print(diff)


if __name__ == "__main__":
    main()

def main():
    import sys
    import bisect
    from itertools import accumulate

    n = int(eval(input()))
    a = list(map(int, sys.stdin.readline().split()))
    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect(b, l // 2)
    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))
    print(ans)


if __name__ == "__main__":
    main()

+    import bisect -    diff = b[-1] -    total = b[-1] -    for x in b: -        diff = min(diff, abs(total - 2 * x)) -    print(diff) +    l = b[-1] +    i = bisect.bisect(b, l // 2) +    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i])) +    print(ans)