#!/usr/bin/env python3

n, *a = list(map(int, open(0).read().split()))

s = sum(a)

r = [a[0]]

for i in range(1, n):

    r.append(r[i - 1] + a[i])

print((min(abs(2 * i - s) for i in r)))

#!/usr/bin/env python3

n, *a = list(map(int, open(0).read().split()))

r = [a[0]]

for i in range(1, n):

    r.append(r[i - 1] + a[i])

print((min(abs(2 * i - r[-1]) for i in r)))

#!/usr/bin/env python3
n, *a = list(map(int, open(0).read().split()))
s = sum(a)
r = [a[0]]
for i in range(1, n):
    r.append(r[i - 1] + a[i])
print((min(abs(2 * i - s) for i in r)))

#!/usr/bin/env python3
n, *a = list(map(int, open(0).read().split()))
r = [a[0]]
for i in range(1, n):
    r.append(r[i - 1] + a[i])
print((min(abs(2 * i - r[-1]) for i in r)))

-s = sum(a) -print((min(abs(2 * i - s) for i in r))) +print((min(abs(2 * i - r[-1]) for i in r)))