Problem:
 Score: 200 points    Problem Statement  Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i -th section from the left has a length of A_i millimeters.  Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:   Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).  Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.   Find the minimum amount of money needed before cutting the bar into two parts with the same length.      Constraints   2 \leq N \leq 200000  1 \leq A_i \leq 2020202020  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  A_3  ...  A_N       Output  Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.        Sample Input 1 3
2 4 3      Sample Output 1 3  The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:   Shrink the second section from the left. The lengths of the sections are now [2, 3, 3] .  Shrink the first section from the left. The lengths of the sections are now [1, 3, 3] .  Shrink the second section from the left. The lengths of the sections are now [1, 2, 3] , and we can cut the bar at the second notch from the left into two parts of length 3 each.        Sample Input 2 12
100 104 102 105 103 103 101 105 104 102 104 101      Sample Output 2 0

Solution (Python):
import sys
from bisect import bisect

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n=int(input())
    aa=list(map(int, input().split()))
    cs=[aa[0]]
    for i in range(1,n):
        cs.append(cs[i-1]+aa[i])
    
    mid=cs[-1]/2
    mi=bisect(cs,mid)
    ans=min(mid-cs[mi-1],cs[mi]-mid)
    print(int(ans*2))

main()

Solution (Python):
import numpy as np

N = int(input())
A = np.fromstring(input(), dtype=np.int64, sep=' ')
b = np.cumsum(A)
c = b[N - 1] - b
d = np.abs(b - c)
ans = np.min(d)
print(ans)

Solution (Python):
N = int(input())

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

full_sum = sum(A_list)
min_haba = full_sum
tmp_sum = 0
for i in range(N):
    tmp_sum += A_list[i]
    min_haba = min(abs((full_sum - tmp_sum)- tmp_sum),min_haba)
    

print(min_haba)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
all = sum(a)
cum = 0
ans = 10**11
for i in range(n):
  cum += a[i]
  if abs(all - cum*2) <= ans:
    ans =abs(all - cum*2)
print(ans)

Solution (Python):
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
sum = np.sum(A)
cumsum = np.cumsum(A)
diff = float("inf")
for n in range(N):
    left = cumsum[n]
    right = sum-left
    if abs(left-right) < diff:
        diff = abs(left-right)
print(diff)

Solution (Python):
import bisect
N = int(input())
A = list(map(int, input().split()))
B = [0]*N
B[0] = A[0]

for i in range(1,N):
  B[i] = B[i-1]+A[i]

p = B[-1]/2
q = bisect.bisect_left(B,p)

if q>=1:
  ans = min(abs(B[q-1]-p),abs(B[q]-p))*2
else:
  ans = abs(B[q]-p)*2
print(int(ans))

Solution (Python):
n = int(input())
As = list(map(int,input().split()))
yen = 0
length = sum(As)
l = 0
for a in As:
  l += a
  if l == length/2:
    break
  if l > length/2:
    c = l - length/2
    diff = min(abs(c),abs(b))
    yen += int(diff*2)
    break
  b = length/2 - l
print(yen)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
doubledCumSum = 0
total = sum(a)
lower = upper = 0
for v in a:
    doubledCumSum += 2 * v
    if doubledCumSum <= total:
        lower = doubledCumSum 
    else:
        upper = doubledCumSum
        break
ans = min(abs(total - lower), abs(total - upper))
print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
S=sum(A)
x=0
ans=S
for a in A:
    x+=a
    y=abs(S-2*x)
    ans=min(ans,y)
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
Sa = sum(A)
h = Sa/2
S = 0

for a in A:
    if S+a > h:
        if abs(h-S) > abs(h-S-a):
            S += a
        break
    S += a

print(abs((Sa-S)-S))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

b=[]
sum=0
for i in range(n):
  sum += a[i]
  b.append(sum)

j=0
while b[j] <= (sum//2):
  j += 1

ans1=abs(sum - 2*b[j])
ans2=10**10
if j!=0:
  ans2=abs(sum -2*b[j-1])
  
ans = min(ans1,ans2)
print(ans)

Solution (Python):
import bisect
N = int(input())
A = list(map(int,input().split()))
cums = [0]
for a in A:
    cums.append(cums[-1] + a)

half = cums[-1]//2

i = bisect.bisect_left(cums, half)
if cums[i] == half:
    print(0)
    exit()
if cums[-1]%2 and cums[i] == half+1:
    print(0)
    exit()

a,b = cums[i], cums[-1]-cums[i]
ans = abs(a-b)

if i>0:
    a,b = cums[i-1], cums[-1]-cums[i-1]
    ans = min(ans, abs(a-b))

print(ans)

Solution (Python):

import sys
import itertools

def main():
    n, *A = map(int, sys.stdin.read().split())

    S = sum(A)
    l = 0
    for i in range(n):
        l += A[i]
        if l >= S // 2:
            if l == S / 2:
                ans = 0
                break
            else:
                if l == S // 2 - 1:
                    ans = 1
                    break
                else:
                    if l - A[i] >= S - l:
                        ans = A[i] + (S - l) - (l - A[i])
                    else:
                        ans = l - (S - l)
                    break

    print(ans)





if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

length = 0
L = sum(A)

for i in range(N):
    length += A[i]
    if length >= L / 2:
        cut = i
        break    

sum_left1 = length
sum_right1 = L - length

sum_left2 = length - A[cut]
sum_right2 = sum_right1 + A[cut]

ans = min(abs(sum_left1 - sum_right1),abs(sum_left2-sum_right2))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)

subsum = 0
for itr, val in enumerate(A):
  subsum += val
  if subsum >= sumA / 2:
    p = itr
    ax = val
    break

ans = min(subsum * 2 -sumA, sumA - (subsum - ax) * 2)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s = 0
t = sum(a)
for i in range(n):
  s += a[i]
  if s - t / 2 > 0:
    break

s1 = s - a[i]    
print(min((t - s1 -s1), (s- (t - s) )))

Solution (Python):

def main():
    n = int(input())
    A = list(map(int, input().split()))
    ave = sum(A) / 2
    total = 0
    
    b = 0
    a = 0
    
    for i in range(n):
        total += A[i]
        if total >= ave:
            b = sum(A[:i+1])
            a = sum(A[i+1:])
            b2 = sum(A[:i])
            a2 = sum(A[i:])
            
            break
    print(min(max(a,b)-min(a,b),max(a2,b2)-min(a2,b2)))
    


if __name__ == '__main__':
    main()

Solution (Python):
import math
n=int(input())
a=list(map(int,input().split()))
ans=9999999999
hidari=sum(a)
migi=0
for i in range(n):
    hidari-=a[i]
    migi+=a[i]
    sa=math.fabs(hidari-migi)
    if sa<ans:
        ans=sa
print(int(ans))

Solution (Python):
import bisect
N = int(input())
A = list(map(int, input().split()))

s = [0]*N
s[0] = A[0]
for i in range(1, N):
    s[i] = s[i-1] + A[i]

id = bisect.bisect_left(s, s[-1] // 2)
if s[id] == s[-1] // 2:
    if s[-1] % 2 == 0:
        print(0)
    else:
        print(1)
else:
    k = 0
    if s[-1] % 2 != 0:
        k = 1
    ans = abs(s[id]*2 - s[-1])
    if id > 0:
        ans = min(ans, abs(s[id-1]*2 - s[-1]))
    print(ans)

Solution (Python):

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

b = 0
idx = 0
L = sum(A)
while (b<L/2):
    b += A[idx]
    idx += 1
a = b - A[max(0,idx-1)]
ans = min(L-2*a,2*b-L)
print(ans)

Solution (Python):
import sys
import math
import bisect
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]



def A():
    X, Y = LI()
    ans = 0
    if X == 1:
        ans += 300000
    elif X == 2:
        ans += 200000
    elif X == 3:
        ans += 100000

    if Y == 1:
        ans += 300000
    elif Y == 2:
        ans += 200000
    elif Y == 3:
        ans += 100000

    if X == 1 and Y == 1:
        ans += 400000

    print(ans)
    return



def B():
    N = I()
    A = LI()
    A_sum = sum(A)
    cnt = 0
    ans = sum(A)
    for i in range(N):
        cnt += A[i]
        sa = abs(A_sum - cnt - cnt)
        ans = min(ans, sa)
    print(ans)
    return



def C():
    return



def D():
    return



def E():
    return



def F():
    return



def resolve():
    B()
    return



if __name__ == "__main__":
    B()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
B = [0]
for i in range(N):
    B.append(B[i]+A[i])
ans = float('inf')
for i in range(1, N):
    ans = min(ans, abs(s-(2*B[i])))
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

mid = sum(a)/2


x=[0]*(n)
for i in range(n-1):
  x[i+1]=x[i]+a[i]

x=x[1:]

dis = [abs(x[i]-mid) for i in range(n-1)]
ret = int(min(dis)/0.5)

print(ret)

Solution (Python):
N = int(input())

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

cand = []
left = 0
right = sum(A)
for i in range(N-1):
    left += A[i]
    right -= A[i]
    diff = abs(left - right)
    cand.append(diff)
print(min(cand))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_a = sum(a)
p = 0
for i in range(n):
    count += a[i]
    if count > sum_a//2:
        p = i
        break
if p == 0:
    ans = abs(sum_a - count*2)
else:
    ans = min(abs(sum_a - count*2), abs(sum_a - (count-a[p])*2))
print(ans)

Solution (Python):
n=int(input())
hoge=list(map(int,input().split()))
kei=sum(hoge)
now=0
ans=-1
for i in range(n):
  now+=hoge[i]
  if(abs(now-kei/2)<=ans or ans==-1):
    ans=abs(now-kei/2)
    
print(int(ans*2))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
tot = sum(A)
B = [0 for _ in range(N-1)]
cnt = A[0]
for i in range(1,N):
    B[i-1] = abs(tot-2*cnt)
    cnt += A[i]
print(min(B))

Solution (Python):
def main():
    n = input()
    aa = map(int, raw_input().split())
    length = sum(aa)
    s = 0
    for i, a in enumerate(aa):
        s += a
        if s >= length / 2.0:
            break
    if s == length / 2.0:
        print 0
        return
    print min(s * 2 - length, length  - (s - aa[i]) * 2)


main()

Solution (Python):
import bisect as bi
import numpy
n = int(input())
li = list(map(int,input().split()))

li = list(map(lambda x:2*x,li))
acc = list(numpy.cumsum(li))
m = acc[n-1]//2
ml = acc[bi.bisect_left(acc,m)-1]
mr = acc[bi.bisect_left(acc,m)]


print(min(m-ml,mr-m))

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

tot_len = sum(A)
cum = 0
for i in range(N-1):
    cum += A[i]
    if cum >= tot_len/2:
        break
first = sum(A[:i+1])
last = sum(A[i+1:])
diff = abs(first - last)
if i>=1:
    first2 = sum(A[:i])
    last2 = sum(A[i:])
    diff2 = abs(first2 - last2)
    print(min(diff,diff2))
else:
    print(abs(first - last))

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

co = sum(a)
su = 0
for i in range(n):
    su += a[i]
    if su >=co/2:
        k,l = i-1,i
        break
print(min(abs(2*su-co),abs(co-2*(su-a[l]))))

Solution (Python):
N = int(input())
A_list = [int(e) for e in input().split()]

total = sum(A_list)
half = total/2
right_side = 0

for i in range(N):
  if right_side + A_list[i] <= half:
    right_side += A_list[i]
  else:
    left_side = total - right_side
    next_length = A_list[i]
    break




if abs(right_side - left_side)<abs(right_side+next_length-left_side+next_length):
  print(abs(right_side - left_side))
else:
  print(abs(right_side+next_length-left_side+next_length))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
A = sum(a)
x = A - 2*a[0]
ll = [x]

for i in range(1,n-1):
    x = x-2*a[i]
    ll.append(x)

for i in range(len(ll)):
    ll[i] = abs(ll[i])
    
print(min(ll))

Solution (Python):
import sys,itertools
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
l = list(map(int, input().split()))
t = 0

for i in range(N):
    k = l[i]
    t += k

t_2 = t/2
pre = 0
A = 0
for i in range(N):
    A += l[i]
    d = A-t_2
    if d>0:
        if d>abs(pre):
            A -= l[i]
        break
    pre = d
print(abs(t-2*A))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

left = [0] * N
right = [0] * N

left[0] = A[0]
for i in range(1, N):
        left[i] += left[i-1] + A[i]

right[-1] = A[-1]
for i in reversed(range(0, N-1)):
        right[i] += right[i+1] + A[i]



ans = 2020202050
for i in range(N-1):
        ans = min(ans, abs(left[i] - right[i+1]))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

tot = sum(A)
rui = [0]*len(A)
cur = 0
for i in range(0,len(A)):
  cur += A[i]
  rui[i] = cur

ans = 0
for i in range(0,len(A)):
  if rui[i] < tot//2 and rui[i+1] > tot//2:
    ans = abs(rui[i] - (tot - rui[i]))
    ans2 = abs(2*rui[i+1]-tot)
    ans = min([ans, ans2])

print(ans)

Solution (Python):
import sys
n = int(input())
a = list(map(int, input().split()))

s = sum(a) / 2
i = 0
c = 0
while (True):
    c += a[i]
    if c > s:
        d = c - a[i]
        break
    i += 1
    

x = c - s
y = s - d
ans = min(x, y) * 2
print(int(ans))

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
a = [int(item) for item in input().split()]
cumsum = [0] * (n + 1)
total_len = sum(a)
for i in range(n):
    cumsum[i+1] = cumsum[i] + a[i]

ans = 10**15 
for i in range(n+1):
    ans = min(ans, abs(cumsum[i] - (cumsum[n] - cumsum[i])))

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))

s = sum(A)
idx = 0
s1 = 0
half = s//2
for i, a in enumerate(A):
    s1 += a
    if half < s1:
        idx = i
        break

s2 = sum(A[i+1:])
ans1 = abs(s1-s2)
s3 = sum(A[i:])
ans2 = abs(s1-s3-A[i])
print(min([ans1, ans2]))

Solution (Python):

import bisect
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = True



def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    n = II()
    a = LMIIS()

    s = sum(a)

    m = s
    x = 0
    for i in range(n):
        x += a[i]
        m = min(m, abs(s/2 - x))
    ans = int(m * 2)


    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
d_A = sum(A)

d1 = 0
d2 = d_A
delta = 1000000000000
for i in range(0,N):
    d1 += A[i]
    d2 -= A[i]
    if abs(d1 - d2) <= delta:
        delta = abs(d1-d2)
print(delta)

Solution (Python):
def solve():
    n = int(input())
    a = tuple(map(int, input().split()))

    ret = float('inf')

    left = 0
    right = sum(a)
    for a_ in a:
        left += a_
        right -= a_
        ret = min(ret, abs(left - right))

    return ret


print(solve())

Solution (Python):


import sys
import math
import collections

N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))

total = sum(As)
current = 0
half = None
for i in range(len(As)):
    if current + As[i] >= (total / 2):
        if abs(current - total / 2) <= abs((current + As[i]) - total / 2):
            half = current
        else:
            half = current + As[i]
        break
    current += As[i]
print(abs((total - half) - half))
exit(0)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

L = 0
R = 0
l = 0
r = N - 1
for i in range(N):
    if L < R:
        L += A[l]
        l += 1
    else:
        R += A[r]
        r -= 1

print(abs(L-R))

Solution (Python):
import numpy as np
n = int(input())
a = [int(i) for i in input().split(" ")]
s = [a[0]]
for i in a[1:]:
    s.append(s[-1]+i)
s = np.array(s)
ar = np.argmin(np.abs(s-s[-1]/2))
if s[ar]==s[-1]/2:
    print(0)
else:
    te=s[ar]-s[-1]/2
    print(int(abs(te)//0.5))

Solution (Python):
import sys
import heapq
from collections import defaultdict
import itertools
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
L = sum(A)
length = 0
mid = None
for i in range(N):
    length += A[i]
    if length >= L / 2:
        mid = i
        break


d = min(length - L / 2, L / 2 - (length - A[mid]))
print(int(d * 2))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
previous = 0
following = sum(A)
ans = float("inf")
for a in A:
    previous += a
    following -= a
    tmp_ans = abs(previous - following)
    if ans > tmp_ans:
        ans = tmp_ans
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=float(sum(a))
c=b/2
d=0
e=0
while d<c:
  d=d+a[e]
  e=e+1
print(int(min(d-c,c-d+a[e-1])*2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = sum(a)

r = 0
l = 0
k = 0
for i in range(len(a)):
    k = i
    if l+a[i] > s//2:break
    l += a[i]
r = s - a[k] - l

print(min(max(l+a[k],r)-min(l+a[k],r),max(l,r+a[k])-min(l,r+a[k])))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()
total = sum(A)
A = list(accumulate(A))
r = bisect_left(A, total / 2)
l = r - 1
if r == n - 1:
    print(abs((total - A[l]) - A[l]))
else:
    print(min(abs((total - A[r]) - A[r]), abs((total - A[l]) - A[l])))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
S=sum(A)

m=0
for i in range(N):
  if m+A[i]<=S//2:
    m+=A[i]
  else:
    ind=i
    break
  
if m==S/2:
  print(0)
  exit()
  
n=S-m-A[ind]

if n<m:
  n+=A[ind]
else:
  m+=A[ind]
  
print(abs(m-S//2)+abs(n-S//2))

Solution (Python):
n = int(input())
a_array = [int(x) for x in input().split()]
ruiseki = [0]
now = 0
sum_a = sum(a_array)
half = sum_a//2
diff = float("inf")
for a in a_array:
    ruiseki.append(ruiseki[-1] + a)
    left = ruiseki[-1]
    right = sum_a - left
    diff = min(diff, abs(half-left) + abs(half-right))
print(diff)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ret = float('inf')
s = sum(a)
curSum = 0
for cut in range(len(a)):
    ret = min(ret, abs(curSum - s + curSum))
    curSum += a[cut]
print(ret)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
li=[]
x=0
for i in range(n):
  x+=a[i]
  li.append(abs(s-2*x))

print(min(li))

Solution (Python):
from itertools import accumulate
N = int(input())
A = list(map(int, input().split()))


A = list(accumulate(A))
ans = float('inf')
for a in A[:-1]:
    ans = min(ans, abs((A[-1] - a) - a))


print(ans)

Solution (Python):
n=int(input())
ls=list(map(int,input().split(" ")))

left=0
right=sum(ls)
diff=right-left
prev_diff=10**100
for i in ls:
    left+=i
    right-=i
    diff=abs(right-left)
    if diff>=prev_diff:
        break
    prev_diff=diff
print(prev_diff)

Solution (Python):
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        A = list(map(int, input().split()))

        sum_all = sum(A)
        min_diff = sum_all
        left = 0
        right = sum_all
        for i in range(N):
            left += A[i]
            right -= A[i]
            tmp_diff = abs(right - left)
            min_diff = min(min_diff, tmp_diff)

        print(min_diff)



if __name__ == '__main__':
    AtCoder().main()

Solution (Python):
n = int(input())
al = list(map(int,input().split()))

import itertools
al_ac = list(itertools.accumulate(al))

ans = float("inf")

for a in al_ac:
    if ans > abs( (al_ac[-1] - a) -a) :
        ans = abs( (al_ac[-1] - a) -a)

print(ans)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
H = sum(A)/2
s = 0
for i in range(N):
    t = s + A[i]
    if t >= H:
        break
    else:
        s = t
print(int(min(t-H,H-s)*2))

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time

def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()

input=lambda: sys.stdin.readline().rstrip()

show_flg=False


ans=0
n=I()
a=LI()

tot=sum(a)
acc=0
for i in range(n):
    acc+=a[i]
    if acc>=tot/2:
        k=i
        break
if tot==acc*2:
    print(0)
    exit()
else:
    l=min(abs(2*acc-tot),abs(2*(acc-a[k])-tot))
    ans=l

show(k,tot,acc,acc-a[k])
print(ans)

Solution (Python):
n = int(input())
an = list(map(int, input().split()))
sm = sum(an)
left = 0
mini = sm
for a in an:
  left += a
  dev = abs(sm-2*left)
  mini = min(dev, mini)
print(mini)

Solution (Python):
def main():  
    n = int(input())
    a = list(map(int,input().split()))
    asum = sum(a)
    asum2 = asum // 2
    x = 0
    tmp = 0
    for i in range(len(a)):
        tmp += a[i]
        if tmp >= asum2:
            x = i
            break
    spa1 = sum(a[0:x])
    spa2 = sum(a[0:x+1])
    ans1 = asum - 2*spa1
    ans2 = 2*spa2 - asum
    print(min([ans1,ans2]))
    
if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

d=[0]*n
d[0]=sum(a)
for i in range(n-1):
    d[i+1]=d[i]-2*a[i]
d=[abs(x) for x in d]
print(min(d))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

L=sum(A)

LS=A[0]
for i in range(1,N):
    LS+=A[i]
    if LS>L/2:
        L1=LS-A[i]
        L2=L-L1
        L3=LS
        L4=L-LS
        ans=min(abs(L1-L2),abs(L3-L4))
        print(ans)
        exit()

Solution (Python):
n=int(input())
l=tuple(map(int,input().split()))
s=sum(l)
m=0
p=0
while 2*m<s:
  m+=l[p]
  p+=1
a=2*m-s
b=s-2*(m-l[p-1])
print(min(a,b))

Solution (Python):
import sys
n=int(input())
a=list(map(int,input().split()))
s=[0]
for i in range(n):
  s.append(s[i]+a[i])
h=sum(a)/2
for i in range(n+1):
  if s[i]==h:
    print(0)
    sys.exit()
  if s[i]>h:
    x=i
    break
ans=min(int((s[x]-h)*2),int((h-s[x-1])*2))
print(ans)

Solution (Python):
n = int(input())
a = list(map(int ,input().split()))
b = [0]*n
b[0] = a[0]
for i in range(1,n):
  b[i] = b[i-1]+a[i]
s = sum(a)
ans = s
for i in range(n):
  ans = min(ans, abs(s-b[i] - b[i]))
  
print(ans)

Solution (Python):
n= int(input())
a = list(map(int,input().split()))
ans = float("inf")
flag_1 = 0
flag_2 = sum(a)
for i in range(n):
  flag_1 += a[i]
  flag_2 -= a[i]
  ans = min(ans,abs(flag_1-flag_2))
print(ans)

Solution (Python):
N = int(input())
A = list(map(float, input().split()))
mid = sum(A)/2
tmp = 0
for i in range(N):
    tmp+=A[i]
    if tmp>mid:
        if tmp-mid>mid-(tmp-A[i]):
            print(int(sum(A[i:])-sum(A[:i])))
        else:
            print(int(tmp-sum(A[i+1:])))
        break

Solution (Python):
from numpy import cumsum

N = int(input())
L = list(map(int, input().split()))

all = sum(L)
cum = list(cumsum(L))

ans = float('inf')
for i in range(N-1):
    tmp = abs(cum[i]-(all-cum[i]))
    ans = min(ans, tmp)
print(ans)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))

l=0
r=sum(A)
ans=float('inf')
for i in range(n):
  l+=A[i]
  r-=A[i]
  ans=min(ans,abs(l-r))
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

goukei = sum(a)
nagasa = goukei
if nagasa%2 == 1:
  nagasa = nagasa//2 + 1
else:
  nagasa = nagasa//2

leftA = 0
leftB = 0
rightA = 0
rightB = 0
for aa in a:
  leftB = leftA
  leftA += aa
  if leftA > nagasa:
    break

rightA = goukei - leftA
rightB = goukei - leftB
print(min(abs(leftA-rightA), abs(leftB-rightB)))

Solution (Python):
N = int(input())
An = list(map(int,input().split(' ')))

An_half_sum = sum(An)/2
i = 0
sum_count =0
for i in range(N):
    sum_count += An[i]
    if An_half_sum <= sum_count:
        break
min_num = min(sum_count - An_half_sum, An_half_sum - sum_count + An[i])
print(int(min_num*2))

Solution (Python):
n=int(input())
a=[int(i)for i in input().split()]
d,s=0,sum(a)
i=s
for p in a:
    d+=p
    f=abs(s-2*d)
    if f<i:i=f
print(int(i))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

length = sum(A)
left = 0
left1 = 0
left2 = 0

for i in range(N):
    left += A[i]
    if left >= length//2:
        left1 = left
        left2 = left - A[i]
        break

right1 = length - left1
right2 = length - left2

print(min(abs(left1 - right1), abs(left2 - right2)))

Solution (Python):

import sys
N=input()
A=map(int, sys.stdin.readline().split())

L=[]
for i,a in enumerate(A):
    if i==0:
        L.append(a)
    else:
        L.append(L[-1]+a)

ans=float("inf")
for x in L:
    y=L[-1]-x
    ans=min(ans,abs(x-y))

print ans

Solution (Python):
def main():
    n=int(input())
    a=list(map(int,input().split()))
    tmp1=0
    tmp2=0
    low=0
    high=n-1
    while low!=high:
        if tmp1<tmp2:
            tmp1+=a[low]
            low+=1
        else:
            tmp2+=a[high]
            high-=1
    if tmp1<tmp2:
        print(abs(tmp1+a[low]-tmp2))
    else:
        print(abs(tmp2+a[low]-tmp1))

    
if __name__=='__main__':
    main()

Solution (Python):
n = int(input())
a_array = list(map(int, input().split()))
a_sum = sum(a_array)
left = 0
for i in range(n):
    left += a_array[i]
    if left > a_sum / 2:
        break
print(min(abs(a_sum - 2 * left), abs(a_sum - 2 * left + 2 * a_array[i])))

Solution (Python):
import numpy as np
import bisect

N = int(input())
A = np.array(list(map(int, input().split())))
s = sum(A)
cum = A.cumsum()
index = bisect.bisect_left(cum, s / 2)
if index == 0:
    answer = abs(s - 2 * A[0])
else:
    answer = min(abs(s - 2 * cum[index-1]), abs(s - 2 * cum[index]))
print(answer)

Solution (Python):

import sys, math, itertools, heapq, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans =0
count = 0

n=int(input())
A=list(map(int,input().split()))
SA=sum(A)
SA2=SA/2
mindis=SA
for ai in A:
    count+=ai
    mindis=min(mindis,abs(count*2-SA))
print(mindis)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]

b=[0]*n
b[0]=a[0]
for i in range(1,n):
    b[i]=a[i]+b[i-1]
suma=sum(a)

import bisect
ncut = bisect.bisect(b,suma//2)

ans1=abs( sum(a[:ncut])-sum(a[ncut:]) )
if ncut<n:
    ans2=abs( sum(a[:ncut+1])-sum(a[ncut+1:]) )
else:
    ans2=10**12
print(min(ans1,ans2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
left = 0
right = sum(A)
diff = abs(left - right)
for i in range(N):
    left += A[i]
    right -= A[i]
    if abs(left - right) < diff:
        diff = abs(left - right)
print(diff)

Solution (Python):
import collections as c
ip = lambda : map(int, input().split())

N = int(input())
a = c.deque([int(i) for i in input().split()])
l,r = 0,0
while len(a) > 0:
    if l>=r:
        r += a.pop()
    else:
        l += a.popleft()
print(abs(l-r))

Solution (Python):
from sys import stdin
N = int(input())
A= [int(x) for x in stdin.readline().rstrip().split()]

mindif = 2020202020*200000
right = sum(A)
left = 0

for i in range(N):
    right -= A[i]
    left += A[i]
    if mindif > abs(right-left):
        mindif =  abs(right-left)

print(mindif)

Solution (Python):
N = int(input())
b = [int(x) for x in input().split()]
d = sum(b)
ans = d
for i in range(N):
  if d < 0:
    break
  d -= b[i] * 2
  ans = min(ans, abs(d))
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

l = sum(a)

sum_1 = 0
split = 0
for i, x in enumerate(a):
    sum_1 += x
    if sum_1 > (l / 2):
        split = i
        break

sum_2 = sum_1 - a[split]

ans = min(2 * sum_1 - l, l - 2 * sum_2)
print(ans)

Solution (Python):
import math
a=int(input())
b=list(map(int,input().split()))
c=sum(b)
d=0
e=float("inf")
f=0
for i in b:
  d+=i
  if abs((c/2)-d)<e:
    e=abs((c/2)-d)
    f=d
print(abs(c-f*2))

Solution (Python):
n=int(input())
A=[int(i) for i in input().split()]

tar=sum(A)/2

now=0
pre=0
for i in range(n):
    now+=A[i]
    if pre<=tar and tar<=now:
        
        break
    pre=now

if tar-pre>now-tar:
    print(abs(now-(sum(A)-now)))
else:
    print(abs(pre-(sum(A)-pre)))