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):
from collections import deque

n = int(input())
a = deque(map(int, input().split()))

left = a.popleft()
right = a.pop()

while a:
    if left < right:
        left += a.popleft()
    else:
        right += a.pop()

ans = abs(left - right)
print(ans)

Solution (Python):
import numpy
n=int(input())
l=list(map(int,input().split()))
res=numpy.cumsum(l)
ans=float("inf")
su=res[-1]
for i in res:
  ans=min(ans,abs(2*i-su))
print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
AS = sum(A)
H = AS/2

wa = 0
for i in range(N):
    wa+=A[i]
    if wa>H:
        num = i
        R = wa
        L = wa-A[i]
        break

d = min(abs(H-R),abs(H-L))

print(int(d*2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cntL = 0
s = sum(a) // 2
cntR = 0
for i in range(len(a)):
    if cntL + a[i] < s:
        cntL += a[i]
    else:
        cntR = cntL
        cntL += a[i]
        break

print(min(abs((sum(a) - cntL) - cntL),abs(sum(a) - cntR) - cntR))

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
s=sum(a)
i=0
while s>0:
  s-=2*a[i]
  i+=1
d=min(abs(s),s+2*a[i-1])
print(d)

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

Solution (Python):
from itertools import accumulate, count
from bisect import bisect_right

input()
a = [0] + list(accumulate(map(int, input().split())))
i = bisect_right(a, a[-1] // 2)
print(min(2 * a[i] - a[-1], a[-1] - 2 * a[i - 1]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
half=sum(a)/2
count=0
mid=0
tag=0
for i in range(len(a)):
  count+=a[i]
  if count>=half:
    mid=a[i]
    tag=i
    break
right=sum(a[:(tag+1)])-half
left=mid-right
alpha=min(right,left)
if alpha==0:
  print(0)
else:
  print(int(alpha*2))

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

length = 0
lengths = []
for a in aaa:
  length += a
  lengths.append(length)
  
diffs = []
for l in lengths:
  diff = abs(l - (length - l))
  diffs.append(diff)
    
print(min(diffs))

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

a = sum(A)
b = a//2

s = 0

for i in range(N):
  s+=A[i]
  if b<=s:
    s_h = s - A[i]
    break

t = a - s
t_h = a - s_h
if s_h>=t_h:
  ans_h = s_h-t_h
  
else:
  ans_h = t_h-s_h
if s>=t:
  ans = s-t
  ans = min(ans,ans_h)
  print(ans)
  
else:
  ans = t-s
  ans = min(ans,ans_h)
  print(ans)

Solution (Python):
n = int(input())
a = list(int(x) for x in input().split())
key = sum(a)

value = a[0]
for i in range(1, n):
    judge = value + a[i]
    if judge <= key/2:
        value = judge
    else:
        ans = min([abs(key-value*2), abs(key-judge*2)])
        break
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
all_sum=sum(A)
tmp=0
tmp1=0
for i in A:
  if all_sum//2>tmp:
    tmp+=i
    tmp1=i
  else:
    break
if all_sum-tmp<(tmp-tmp1):
  print(abs(tmp1+all_sum-tmp-(tmp-tmp1)))
else:
  print(abs(tmp-abs(all_sum-tmp)))

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

L1 = [0]
for i in range(N):
    L1.append(L1[i] + A[i])

L2 = [0]
for i in range(N):
    L2.append(L2[i] + A[-i-1])

x = 100000000000
for i in range(N):
    y = abs(L1[i] - L2[-i-1])
    x = min(x, y)

print(x)

Solution (Python):
import copy
from bisect import bisect_left
N=int(input())
la=list(map(int,input().split()))
ll=copy.copy(la)

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


inl=bisect_left(ll,ll[-1]/2)
ds=ll[-1]/2-ll[inl-1]
db=ll[inl]-ll[-1]/2
if ds<=db:
    print(ll[-1]-ll[inl-1]*2)
else:
    print(ll[inl]*2-ll[-1])

Solution (Python):
num=int(input())
mm=[int(s) for s in input().split()]
total=sum(mm)


count=0
for j,i in enumerate(mm):
  count+=i
  if count>=total//2:
    flag=j
    break

q=sum(mm[flag+1:])

p=count-q


mm.reverse()

count=0
for j,i in enumerate(mm):
  count+=i
  if count>=total//2:
    flag=j
    break

q=sum(mm[flag+1:])
p2=count-q

if p>=p2:
  print(p2)
else:
  print(p)

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

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

import numpy as np

A=np.array(A)

from bisect import bisect_left
s = bisect_left(A.cumsum(), A.sum()/2)

if A.sum()/2 in A.cumsum():
    print(0)
else:
    print(int(min(abs(A.sum()/2 - A[0:s].sum()) * 2, abs(A.sum()/2 - A[0:s+1].sum()) * 2)))

Solution (Python):
n=int(input())
S=list(map(int, input().split()))
Sum=sum(S)

wa=0
count=0
for i in range(n):
    if wa<Sum/2:
        wa=wa+S[i]
        count=count+1
x=int(abs(Sum-2*wa))
wa=wa-S[count-1]
        
y=int(abs(Sum-2*wa))
print(min(x,y))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
 
s = sum(a)
 
left = 0
right = s
res = s
for i in range(n-1):
    left += a[i]
    right -= a[i]
    res = min(res,abs(left-right))
    if res == 0:
        break
    
print(res)

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
a = list(map(int, stdin.readline().rstrip().split()))

lf = 0
lb = 0
front = 0
back = n-1
for i in range(n):
    if lf <= lb:
        lf += a[front]
        front += 1
    else:
        lb += a[back]
        back += -1
if lf <= lb:
    h = lb-lf
else:
    h = lf-lb
print(h)

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

lr = 0
ll = sum(a)
index = 0

while lr + a[index] < ll - a[index]:
    lr = lr + a[index]
    ll = ll - a[index]
    index += 1

diff = min(ll - lr, lr + 2 * a[index] - ll)
print(diff)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = sum(a)
l = 0
r = 0
x = 0
ans = 0
for i in a:
  l += i
  r = b - l
  if l==r:
    break;
  elif l>r:
    ans = min(l-r,y-x)
    break;
  x = l
  y = r
print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
b=sum(a)
c=0
d=2020202020
for i in a:
    c+=i
    d=min(d,abs(c-b/2))
print(int(d*2))

Solution (Python):
import bisect
n=int(input())
a=list(map(int,input().split()))
s=[0]
for i in range(n):
  s.append(s[i]+a[i])
x=s[n]//2
idex = bisect.bisect_left(s,x)
ans=min(abs(s[n]-2*s[idex-1]),abs(s[n]-2*s[idex]))

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np
import bisect
N = int(input())
A = np.array(readline().split(),np.int64)
S = sum(A)

AA = np.cumsum(A)
half = S // 2
std = bisect.bisect_left(AA,half)


if S % 2 == 0 and S // 2 in AA:
  print(0)
  exit()






a = AA[std-1]
b = AA[-1] - a
aa = AA[std]
bb = AA[-1] - aa
print(min(abs(a - b),abs(aa - bb)))

Solution (Python):
def main():
    n = int(input())
    *a, = map(int, input().split())
    cs = [a[0]]*n
    for i in range(1, n):
        cs[i] = a[i]+cs[i-1]
    o = float("inf")
    for i in range(n):
        l, r = cs[i], cs[-1]-cs[i]
        tmp = max(l, r) - min(l, r)
        o = tmp if tmp<o else o
    print(o)

if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect
from itertools import accumulate
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = tuple(map(int, input().split()))

    a = tuple(accumulate(A))
    n = sum(A) // 2
    i = bisect(a, n)
    case1 = abs(sum(A[:i]) - sum(A[i:]))
    case2 = abs(sum(A[:i+1]) - sum(A[i+1:]))
    print(min(case1, case2))


main()

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = float("inf")
    su = sum(a)
    s = 0
    for i in range(n):
        s += a[i]
        su -= a[i]
        ans = min(ans, abs(s - su))
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate

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

pref = list(accumulate(a))

tab = [0] * n
for i in range(n):
	tab[i] = abs(pref[i] - (pref[-1] - pref[i])) 
print(min(tab))

Solution (Python):
n = int(input())
a_lis = list(map(int, input().split()))
all_sum = sum(a_lis)
half_sum = all_sum // 2
tmp_sum = 0
i = 0
while tmp_sum < half_sum:
    tmp_sum += a_lis[i]
    i += 1    
sep = i - 1
if sep == n:
    print(a_lis[-1] - sum(a_lis[:-1]))
else:
    print(min(tmp_sum - (all_sum - tmp_sum), all_sum - (tmp_sum - a_lis[sep])- (tmp_sum - a_lis[sep])))

Solution (Python):
import numpy as np

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

A = np.array(A)
L = np.sum(A)

l=0
ans = -1

for i in range(N):
    l += A[i]
    if l == L/2:
        ans = 0
        break
    elif l > L/2:
        Am = A[i]
        break

if ans < 0:
    L2 = L - l
    L1 = l - Am
    ans = int(min(L1+Am-L2, L2+Am-L1))

print(ans)

Solution (Python):
N = int(input())
s = raw_input().split()
A = [0]*N

for i in range(N):
    A[i] = int(s[i])



h = 0
t = N-1
lh = 0
lt = 0

for i in range(N):
    if (lh > lt):
        lt += A[t]
        t -= 1
    else:
        lh += A[h]
        h += 1
    

print abs(lh-lt)

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

sum=sum(a)
k=0
for i in range(x-1):
  if sum/2-k-a[i]<=a[i+1]:
    print(int(2*min(sum/2-k-a[i],k+a[i]+a[i+1]-sum/2)))
    sys.exit()
  else:
    k+=a[i]

Solution (Python):
from math import *
n=int(input())
a=list(map(int,input().split()))
ans=100000000000000000000000000000
sum=0
c=0
b=[]
cnt=[]
for x in a:
    sum+=x
    if(x==1):
        c+=1
    b.append(sum)
    cnt.append(c)
for i in range(0,n):
    one=b[i]
    two=sum-b[i]
    res=(one+two)//2
    diff1=abs(one-res)
    diff2=abs(two-res)
    ans=min(ans,diff1+diff2)

print(ans)

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

over=False
all_su=sum(nums)
su=0
for i in range(N):
  nsu=su+nums[i]
  if nsu>all_su/2:
    break
  else:
    su=nsu
if all_su/2-su > nsu-all_su/2:
  print(2*nsu-all_su)
else:
  print(all_su-2*su)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
chk=sum(a)
k=chk//2
k1=k+1
k2=k+2
k3=k-1
k4=k-2
x=y=z=0
for i,j in enumerate(a):
    z=i
    x+=j
    if x>=k:
        break
if x==k and chk%2==0:
    print(0)
else:
    ans=min(abs(x-k)+abs((chk-x)-k),abs(x-k1)+abs((chk-x)-k1),abs(x-k2)+abs((chk-x)-k2),abs(x-k3)+abs((chk-x)-k3),abs(x-k4)+abs((chk-x)-k4))
    x=y=z=0
    for i,j in enumerate(a):
        z=i
        if x+j>=k:
            break
        x+=j
    ans2=min(abs(x-k)+abs((chk-x)-k),abs(x-k1)+abs((chk-x)-k1),abs(x-k2)+abs((chk-x)-k2),abs(x-k3)+abs((chk-x)-k3),abs(x-k4)+abs((chk-x)-k4))
    print(min(ans,ans2))

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

h = sum(a)/2
ans1 = 0
ans2 = 0
ls = [a[0]]

for i in range(1,n):
    ls.append(ls[i-1]+a[i])
        
        
for i in range(len(ls)):
    if ls[i] > h:
        ans1 = abs(ls[i]-sum(a[i+1:]))
        ans2 = abs(ls[i-1]-sum(a[i:]))
        print(min(ans1,ans2))
        break

Solution (Python):
import itertools
n = int(input())
a = list(map(int, input().split()))
a = [0] + a
lacc = list(itertools.accumulate(a))
a.reverse()
racc = list(itertools.accumulate(a))
racc = [0] + racc[:-1] 
racc.reverse()
ans = racc[0]
for i in range(n+1):
    ans = min(ans, abs(racc[i] - lacc[i]))
print(ans)

Solution (Python):
def solve():
    N=int(input())
    AL=[int(x) for x in input().split()]
    total=sum(AL)
    target=total/2

    now=0
    for i,a in enumerate(AL):
        now+=a
        if now>target:
            ans1=(now-target)*2
            ans2=(target-now+a)*2
            return int(min(ans1,ans2))
print(solve())

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

sum_a_list = sum(A_list)
middle_num = sum_a_list/2

temp_sum = 0

middle_block = -1

for j in range(len(A_list)):
    temp_sum += A_list[j]
    if temp_sum == middle_num:
        print(0)
        exit(0)
    if temp_sum > middle_num:
        middle_block = j
        break


diff_go_right = abs(sum(A_list[:middle_block]) - sum(A_list[middle_block:]))
diff_go_left = abs(sum(A_list[:middle_block+1]) - sum(A_list[middle_block+1:]))

print(min(diff_go_right, diff_go_left))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
M = sum(A)/2
M
a = 0
i = 0
while a < M:
    a += A[i]
    i += 1
    
print(int(min(a-M,M-a+A[i-1])*2))

Solution (Python):
from itertools import accumulate, count

input()
a = [0] + list(accumulate(map(int, input().split())))
i = next(i for i in count() if 2 * a[i] > a[-1])
print(min(2 * a[i] - a[-1], a[-1] - 2 * a[i - 1]))

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

A_len = sum(A)

tyuuou = float(A_len) / 2


index = 0
s = 0
flag = 0 
f = 0
nokori = 0
for i in range(N-1):
    s += A[i]
    if s >= tyuuou:
        index = i
        nokori = A_len - s
        if s-A[i] > nokori:
            index -= 1
            s -= A[i]
            nokori += A[i]
        f = 1
        break
if f==0:
    index = N-2
    nokori = A_len - s

print(abs(nokori-s))

Solution (Python):
import numpy as np

inf = float("inf")

n = int(input())
a = np.array(input().split()).astype(float)
b = np.zeros(n).astype(float)
b[0] = a[0]
for i in range(1, n):
    b[i] = b[i - 1] + a[i]
sum = b[len(b) - 1]
mini = inf
split = 0
for i in range(n - 1):
    if mini > abs(b[i] - (sum - b[i])):
        split = b[i]
        mini = abs(b[i] - (sum - b[i]))
if abs(split - (sum - split)) * 2 % 2 == 1:
    print(int(abs(split - (sum - split)) * 2))
else:
    print(int(abs(split - (sum - split))))

Solution (Python):
import statistics
N = int(input())
A = list(map(int, input().split()))
tmp1 = [0] * N
tmp2 = [0] * N
tmp1[0] = A[0]
tmp2[-1] = A[-1]
for i in range(1, N):
    tmp1[i] = abs(tmp1[i - 1] + A[i])
    tmp2[-i - 1] = abs(tmp2[-i] + A[-i - 1])
ans = 10 ** 10
for i in range(N - 1):
    ans = min(ans, abs(tmp1[i] - tmp2[i + 1]))
print(ans)

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

result = [[a[0], sum(a[1:])]]
for i in range(1, n):
    result.append([result[-1][0]+a[i], result[-1][1]-a[i]])
h = []
for i in range(len(result)):
    h.append(abs(result[i][0]-result[i][1]))
print(min(h))

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

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

cnt=0
sum_r=0
sum_l=sum(a)
sa=[]
for i in range(n):
    sum_r+=a[i]
    sum_l-=a[i]
    sa.append(abs(sum_r-sum_l))
print(min(sa))

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

leftSum = 0
rightSum = 0
K = N
L = 0

for i in range(0,N):
    if(leftSum < rightSum):
        leftSum = leftSum + Ai[L]
        L = L+1
    else:
        rightSum = rightSum + Ai[K-1]
        K = K-1
print(abs(leftSum-rightSum))

Solution (Python):

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

S=sum(A)
T=0

for i in range(N):
    SS=S-A[i]
    TT=T+A[i]
    if SS-TT<=0:
        if abs(SS-TT)<abs(S-T):
            ans=abs(SS-TT)
            
            
        else:
            ans=abs(S-T)
            
        break
    T+=A[i]
    S-=A[i]

print(ans)

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

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

l = 0
r = N - 1

sum_l = A[l]
sum_r = A[r]

while r - l > 1:
    
    if sum_l < sum_r:
        l += 1
        sum_l += A[l]
    else:
        r -= 1
        sum_r += A[r]
        
print(abs(sum_l-sum_r))

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

sumA = sum(A)/2

i = 0
j = 0

while i < sumA:
    i = i+A[j]
    j += 1

ans1 = abs(sum(A[0:j]) - sum(A[j:N]))
ans2 = abs(sum(A[0:j-1]) - sum(A[j-1:N]))
ans3 = abs(sum(A[0:j+1]) - sum(A[j+1:N]))
print(min(ans1,ans2,ans3))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
i=ans1=0
while True:
    if ans1<x/2 and i<n:
        ans1+=a[i]
        i+=1
    else:
        break
if i==n:
    print(2*a[i-1]-x)
else:
    i-=1
    ans2=x-ans1
    ans1-=a[i]
    print(a[i]-abs(ans1-ans2))

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

total_length = 0
for i in range(n):
    total_length += a[i]

dis = 10000000000000000000000000000000000000000
sum_length = 0
for i in range(n):
    sum_length += a[i]
    if abs(total_length/2 - sum_length) < dis:
        dis = abs(total_length/2 - sum_length)
    else:
        sum_length -= a[i]
        break
print(abs(total_length - 2 * sum_length))

Solution (Python):


def solve():
    N = int(input())
    A = list(map(int, input().split()))
    L = 0
    R = sum(A)
    for a in A:
        if L+a <= R-a:
            L += a
            R -= a
        elif L == R:
            c = 0
            break
        else:
            c = a
            R -= a
            break

    res = min(L+c-R, R+c-L)
    return str(res)

if __name__ == '__main__':
    print(solve())

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    M=sum(A)/2
    l=0
    for i in range(N):
        o=l
        l+=A[i]
        if l>=M:
            break
    if (l-M) > (M-o):
        l=o
    print(int(abs(l-M)*2))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
INF = float('inf')

N = int(input())
Alist = inpl()

i = 0
j = 1
x = 0
y = 0
while i + j != N+1 :
    if y >= x:
        x += Alist[i]
        i += 1
    elif y < x:
        y += Alist[-j]
        j += 1

print(abs(x-y))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
l = 0
for i, ai in enumerate(a):
    l += ai
    if l >= total // 2:
        break
c1 = l - (total - l)
c2 = total - (l - ai) - (l - ai)
print(min(c1, c2))

Solution (Python):
N = int(input())
A  = list(map(int,input().split()))
now = 0
sumA = sum(A)
for i in range(N):
    now += A[i]
    if now == sumA/2:
        print(0)
        break
    elif now > sumA/2:
        test1 = now-sum(A[i+1:])
        test2 = sum(A[i:])-(now-A[i])
        print(int(min([test1,test2])))    
        break

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
suml=[0]*n
suml[0]=a[0]
for i in range(1,n):
  suml[i]=suml[i-1]+a[i]
total=suml[-1]
ans=10**20
for i in range(n):
  if ans>abs(total-2*suml[i]):
    ans=abs(total-2*suml[i])
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
SUM=sum(a)
hlf=SUM//2
cnt=0
res=float('inf')
for i in range(n):
    cnt+=a[i]
    rst=SUM-cnt
    if cnt==hlf:
        print(0)
        exit()
    res=min(abs(rst-cnt),res)
print(res)

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

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    total = sum(a)
    count = 0
    length = 0
    while True:
        length += a[count]
        if length >= total // 2:
            break
        count += 1

    ans = abs(total - length * 2)
    if abs(total - (length - a[count]) * 2) < ans:
        ans = abs(total - (length - a[count]) * 2)

    print(ans)

Solution (Python):
def main():
  from builtins import abs
  N = int(input())
  A = list(map(int, input().split()))

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

  l = sum(A)
  print(min(abs(p + p - l) for p in a))
main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
S=0
ans=2020202022
for i in range(n):
  S+=a[i]
  if abs((x-S)-S) < ans:
    ans=abs((x-S)-S)
print(ans)

Solution (Python):
import math

n = int(input())

a = [int(x) for x in input().split()]

res = 0

for x in a :
    res += x

ans = 0

i = 0

while ans < res//2 :
    ans += a[i]
    i += 1

i -= 1

rmn = res - ans
print(min(ans - rmn, rmn + 2*a[i] - ans))

Solution (Python):


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

summ = sum(A)
average = summ / 2

sum_left = 0
j = 0
while sum_left < average:
    sum_left += A[j]
    j += 1
    
sum_right = sum(A[j:])

d = abs(sum_right - sum_left)

sum_l = sum_left - A[j-1]
sum_r = sum_right + A[j-1]

d2 = abs(sum_r - sum_l)

if d2 < d:
    print(d2)
else:
    print(d)

Solution (Python):

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

sums =sum(A)

l = 0
mlen = sums
for i in A:
    l += i
    r = sums - l
    

    lr =l-r
    if lr <0:lr*=-1

    
    if mlen < lr:
        break
    else:
        mlen = r-l
        if mlen <0:mlen*=-1



print(mlen)

Solution (Python):
input()
xs=list(map(int,input().split()))
sx=sum(xs)
mmx,mnx=sx,0
rs = set()
for x in xs:
    mnx, mmx = mnx + x, mmx - x
    rs.add(abs(mmx - mnx))
print(min(rs))

Solution (Python):

n = int(input())

mydata = list(map(int,input().split()))
res = [0] * len(mydata)

res[0] = mydata[0]
for i in range(0,len(mydata)-1):
    res[i+1] = res[i] + mydata[i+1]


ans = 2020202020**100
for i in range(n-1):
    before_sum = res[i]
    after_sum = res[n-1]-res[i]
    tmp = abs(before_sum-after_sum)
    ans = min(ans,tmp)
print(ans)

Solution (Python):
n = int(input())
B = [int(x) for x in input().split(" ")]

total = sum(B)
mid = total/2

behind = 0
for i in range(n):
    if behind + B[i] > mid:
        targetIndex = i
        break
    else:
        behind += B[i]


right = mid - behind
left = behind + B[i] - mid

if right == left:
    print(int(right//0.5))
elif right > left:
    print(int(left//0.5))
elif right < left:
    print(int(right//0.5))

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

L = 0
R = 0
l = r = 0
while(l + r != N):
    if l==0 and r==0:
        L += A[l]
        l += 1
        R += A[-1 * (r + 1)]
        r += 1
    
    else:
        if L >= R:
            R += A[-1 * (r + 1)]
            r += 1

        else:
            L += A[l]
            l += 1



print(abs(L - R))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
S=[0 for _ in range(n+1)]
for i in range(n):
  S[i+1]=S[i]+A[i]
print(min(map(lambda x:abs(S[n]-2*x),S)))

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

prefix = [0]

for i in a:
    prefix.append(prefix[-1] + i)

mini = float('inf')

for i in range(0, n):
    mini = min(abs(prefix[i] - (prefix[-1] - prefix[i])), mini)

print(mini)

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

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

res = float("inf")
for i in range(1, len(PA) - 1):
    if PA[i] == PA[-1] - PA[i]:
        res = 0
    res = min(res, abs(PA[-1] - PA[i] - PA[i]))

print(res)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
L, R, sumL_R = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if sumL_R < 0:
        L += 1
        sumL_R += A[L]
    else:
        R -= 1
        sumL_R -= A[R]
print(abs(sumL_R))

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

Solution (Python):
N=int(input())
import numpy as np
a=np.array(input().split(),dtype=int)
asum=np.cumsum(a)
half=asum.max()//2
isodd=(asum.max()%2==1)
import bisect
p=bisect.bisect_left(asum,half)
if abs(half-asum[p])>abs(half-asum[p-1]):
  p=p-1
longer=asum[p]
shorter=asum.max()-asum[p]
print(abs(longer-shorter))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(readline())
A = np.array(read().split(),np.int64)

Acum = A.cumsum()

L = Acum[:-1]
R = Acum[-1] - L

answer = np.abs(L-R).min()
print(answer)

Solution (Python):
N=int(input())
A=[a for a in list(map(int,input().split()))]

SUM=sum(A)
MEAN=SUM / 2

idx=-1
pre=0
temp=0
while (temp <= SUM):
    if (MEAN <= temp):
        break
    idx+=1
    pre=temp
    temp+=A[idx]

print(int(min(abs(MEAN-pre),abs(MEAN-temp))/0.5))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
S=[0] 
for i in range(N):
    S.append(S[-1]+A[i])
L=S[-1] 

Sh=0 
Dh=L 
for j in range(N+1):
    if Dh>abs(S[j]-L/2):
        Dh=abs(S[j]-L/2)
        Sh=j
if Dh==0:
    print(0)
else:
    print(int(Dh*2))

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

length = sum(a)
if length %2 ==0:
    check = 0
    half = length//2
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    if check - half > half - (check-a[idx]):
        check -=a[idx]
    
    ans = abs((half-check)*2)
else:
    check = 0
    half = length//2
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    check -=a[idx]
    afte = sum(a[idx:])
    ans = half-check+afte-half
    
    check +=a[idx]
    afte = sum(a[idx+1:])
    ans = min(ans,abs(check-half)+abs(afte-half))
    
print(ans)

Solution (Python):
from itertools import accumulate

INF = float("inf")
IINF = 10 ** 18
MOD = 998244353

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

    lengths = list(accumulate([0] + A))

    
    
    half = lengths[-1] // 2

    

    if N == 2:
        print(abs(A[0] - A[1]))
    else:
        a1 = 0
        b1 = 0
        a2 = 0
        b2 = 0
        for i in range(1, N):
            if lengths[i] >= half:
                a1 = lengths[i]
                b1 = lengths[N] - lengths[i]
                a2 = lengths[i-1]
                b2 = lengths[N] - lengths[i-1]
                break

        
        
        print(min(abs(b2 - a2), abs(b1 - a1)))


if __name__ == '__main__':
    main()

Solution (Python):



n = int(input())

As = list(map(int, input().split(" ")))
sA = sum(As)

half_sA = sA // 2

cur_val = 0
for i, a in enumerate(As):
    cur_val += a
    if half_sA <= cur_val:
        over_i = i 
        break

cur_val2 = cur_val
cur_val1 = cur_val - a


diff1 = half_sA - cur_val1
diff2 = cur_val2 - half_sA

val_a = cur_val1
val_b = sA - val_a

val_c = cur_val2
val_d = sA - val_c

out = min( abs(val_b-val_a), abs(val_c-val_d) )






print(out)

Solution (Python):
import numpy as np
N=int(input().strip())
a=list(map(int,input().strip().split()))

half=sum(a)/2
sum_=0
min_=np.inf
for i in a:
    sum_+=i
    dist=abs(sum_-half)
    if dist<min_:
        min_=dist
ans=int(min_//0.5)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
L = 0
AS = []
for i in A:
  L += i
  AS.append(L)
R = L
for i in range(N-1):
  X = AS[i]
  Y = L - X
  R = min(R,abs(X - Y))
print(R)

Solution (Python):

import math

n = int(input())

a = [int(x) for x in input().split()]

res = 0

for x in a :
    res += x

ans = 0

i = 0

while ans < res//2 :
    ans += a[i]
    i += 1

i -= 1

rmn = res - ans
print(min(ans - rmn, rmn + 2*a[i] - ans))

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
long = sum(L)
ans = long
sum1 = 0
sum2 = long
for i in range(N):
	sum1 += L[i]
	sum2 -= L[i]
	ans = min(abs(sum1-sum2), ans);
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
p=q=sum(a)
for i in range(0,n):
  p-=2*a[i]
  q=min(abs(p),q)
print(q)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
l = sum(a)
b = list()
t = 0
ans = 10**10
for i in a:
    t += i
    b.append(t)
for j in b:
    x = abs(l-(2*j))
    if x < ans:
        ans = x

print(ans)

Solution (Python):
from bisect import bisect_left as bl

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

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

half = a[-1] / 2
index = bl(a, half)

print(int(min(half - a[index - 1], a[index] - half) * 2))

Solution (Python):
import numpy as np

def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

n = int(input())
ali = list(map(int, input().split()))
naka = sum(ali)/2

zoukali = []
total = 0
for a in ali:
    total += a
    zoukali.append(total)

medi_ind = getNearestValue(zoukali, naka)

mae = sum(ali[:medi_ind+1])
ushiro = sum(ali[medi_ind+1:])


print(abs(mae-ushiro))

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

cumA = [0] * N
tmp = 0
for i in range(N):
    tmp += A[i]
    cumA[i] = tmp

from bisect import bisect_left

index = bisect_left(cumA, cumA[-1] / 2)

if index == 0:
    left = 0
else:
    left = cumA[index - 1]
right = cumA[index]
ans = min(right * 2 - cumA[-1], cumA[-1] - left * 2)


print(ans)

Solution (Python):

si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n = ni()
a = nl()
a_sum = []
s = sum(a)
ans = 10**10
ss = 0
for i in range(n):
    ss += a[i] 
    a_sum.append(ss)

for i in range(n):
    start = a_sum[i]
    end = s - start
    t = abs(start - end)
    ans = min(t,ans)

print(ans)