import sys

def input():

    return sys.stdin.readline()[:-1]

inf=float("inf")

n=int(eval(input()))

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

suma=sum(a)

cnt=0

i=0

while(cnt<suma/2):

    cnt+=a[i]

    i+=1

cnt2=cnt-a[i-1]

half=float(suma)/2

# print(half)

nerai=min(abs(suma-cnt-half),abs(cnt-half),abs(cnt2-half),abs(suma-cnt2-half))

print((int(nerai*2)))
import bisect

import copy

import heapq

import math

import sys

from collections import *

from functools import lru_cache

from itertools import accumulate, combinations, permutations, product

def input():

    return sys.stdin.readline()[:-1]

def ruiseki(lst):

    return [0]+list(accumulate(lst))

sys.setrecursionlimit(500000)

mod=10007

al=[chr(ord('a') + i) for i in range(26)]

direction=[[1,0],[0,1],[-1,0],[0,-1]]



n=int(eval(input()))

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

lst=ruiseki(a)

asum=sum(a)

# print(asum)

# print(lst)

cnt=bisect.bisect_left(lst,asum/2)

# print(cnt)

if asum//2==lst[cnt]:

    print((0))

else:

    if asum%2==0:

        print((2*min(asum//2-lst[cnt-1],lst[cnt]-asum//2)))

    else:

        print((min((asum//2-lst[cnt-1])*2+1,(lst[cnt]-asum//2)*2-1)))
import sys


def input():
    return sys.stdin.readline()[:-1]


inf = float("inf")
n = int(eval(input()))
a = list(map(int, input().split()))
suma = sum(a)
cnt = 0
i = 0
while cnt < suma / 2:
    cnt += a[i]
    i += 1
cnt2 = cnt - a[i - 1]
half = float(suma) / 2
# print(half)
nerai = min(
    abs(suma - cnt - half), abs(cnt - half), abs(cnt2 - half), abs(suma - cnt2 - half)
)
print((int(nerai * 2)))

import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product


def input():
    return sys.stdin.readline()[:-1]


def ruiseki(lst):
    return [0] + list(accumulate(lst))


sys.setrecursionlimit(500000)
mod = 10007
al = [chr(ord("a") + i) for i in range(26)]
direction = [[1, 0], [0, 1], [-1, 0], [0, -1]]
n = int(eval(input()))
a = list(map(int, input().split()))
lst = ruiseki(a)
asum = sum(a)
# print(asum)
# print(lst)
cnt = bisect.bisect_left(lst, asum / 2)
# print(cnt)
if asum // 2 == lst[cnt]:
    print((0))
else:
    if asum % 2 == 0:
        print((2 * min(asum // 2 - lst[cnt - 1], lst[cnt] - asum // 2)))
    else:
        print((min((asum // 2 - lst[cnt - 1]) * 2 + 1, (lst[cnt] - asum // 2) * 2 - 1)))

+import bisect +import copy +import heapq +import math +from collections import * +from functools import lru_cache +from itertools import accumulate, combinations, permutations, product -inf = float("inf") +def ruiseki(lst): +    return [0] + list(accumulate(lst)) + + +sys.setrecursionlimit(500000) +mod = 10007 +al = [chr(ord("a") + i) for i in range(26)] +direction = [[1, 0], [0, 1], [-1, 0], [0, -1]] -suma = sum(a) -cnt = 0 -i = 0 -while cnt < suma / 2: -    cnt += a[i] -    i += 1 -cnt2 = cnt - a[i - 1] -half = float(suma) / 2 -# print(half) -nerai = min( -    abs(suma - cnt - half), abs(cnt - half), abs(cnt2 - half), abs(suma - cnt2 - half) -) -print((int(nerai * 2))) +lst = ruiseki(a) +asum = sum(a) +# print(asum) +# print(lst) +cnt = bisect.bisect_left(lst, asum / 2) +# print(cnt) +if asum // 2 == lst[cnt]: +    print((0)) +else: +    if asum % 2 == 0: +        print((2 * min(asum // 2 - lst[cnt - 1], lst[cnt] - asum // 2))) +    else: +        print((min((asum // 2 - lst[cnt - 1]) * 2 + 1, (lst[cnt] - asum // 2) * 2 - 1)))