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):
def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    cumsumA = [0] * (N + 1)
    cumsumB = [0] * (N + 1)
    for i in range(N):
        cumsumA[i + 1] = cumsumA[i] + A[i]
        cumsumB[N - i - 1] = cumsumB[N - i] + A[N - i - 1]
    minA = 10**18
    for i in range(N + 1):
        minA = min(minA, abs(cumsumA[i] - cumsumB[i]))
    print(minA)


resolve()

Solution (Python):
n = int(input())
d = list(map(int, input().split()))
ans = float('inf')
fh = 0
lh = sum(d)
for i in range(0,n):
  fh += d[i]
  lh -= d[i]
  ans = min(abs(fh - lh), ans)
print(ans)

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

N = int(input())
A = list(map(int, input().split()))
L = [0]
for i in range(N):
    L.append(L[i]+A[i])
TotalLen = L[-1]
Center = TotalLen/2

temp = TotalLen
left = 0
right = 0
for i in range(N + 1):
    if abs(L[i] - Center) < temp:
        temp = abs(L[i] - Center)
        left = L[i]
        right = TotalLen - left
print(abs(left - right))

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

cum = [0]*(N+1)
ans = 10**12
total = sum(a)
for i in range(N):
  cum[i+1] = cum[i] + a[i]
  ans = min(ans,abs(cum[i+1]-(total-cum[i+1])))
print(ans)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N = k()
A = l()
S = sum(A)
minx = S
sum = 0

for x in A:
    sum += x
    minx = min(minx, abs(sum-(S-sum)))

print(minx)

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    diff = 10 ** 10

    partA = 0
    partB = sum(A)

    for i in range(N):
        diff = min(diff, abs(partA-partB))
        partA += A[i]
        partB -= A[i]
    print(diff)
    return
resolve()

Solution (Python):
import numpy as np

N=int(input())
A=np.array(list(map(int, input().split())))
S=sum(A)-A[0]
X=A[0]
ans=abs(S-X)
for i in range(1,N-1):
  X+=A[i]
  S-=A[i]
  ans=min(ans,abs(S-X))
print(ans)

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

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

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

S = sum(A)
min_S = S
sum_B = 0
for i in range(N):
    sum_B += A[i]
    min_S = min(min_S, abs(sum_B - (S - sum_B)))

print(min_S)

Solution (Python):
import numpy as np

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

cum_A = np.cumsum(A)
ans = 10 ** 18
for i in range(N):
    left = cum_A[i]
    right = cum_A[-1] - cum_A[i]
    ans = min(ans, abs(left - right))
print(ans)

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

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

cost = None
for i in range(N):
    left = cum_A[i]
    right = cum_A[-1] - cum_A[i]
    if cost is None:
        cost = abs(left - right)
    else:
        cost = min(abs(left - right), cost)

print(cost)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
import itertools
ACU=itertools.accumulate(A)
B=list(ACU)
MAX=B[-1]
tmp=10**10
for i in B:
  tmp=min(tmp,abs(2*i-MAX))
print(tmp)

Solution (Python):
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(input())
a=list(map(int,input().split()))
lst=ruiseki(a)
asum=sum(a)


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

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))

Solution (Python):
from itertools import accumulate

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

ans = a[-1]
for i in a:
    ans = min(ans,abs(2*i-a[-1]))
print(ans)

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

half = sum(lis)
left, right, dif = 0, half, half

for i in range(N):
    dif_before = dif
    left += lis[i]
    right = half - left
    dif = abs(right - left)
    if dif >= dif_before:
        break

print(dif_before)

Solution (Python):
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()
sum_a = sum(a)
v = sum(a)/2
ans = float("inf")
t = 0
for i in range(len(a)):
	t+=a[i]
	ans = min(ans,abs(sum_a-2*t))
print(ans)

Solution (Python):
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

v = sum(a)/2
t = 0
for i in range(len(a)):
	t+=a[i]
	if t>v:
		break
z1 = sum(a[:i])
k1 = sum(a[i:])
z2 = sum(a[:i+1])
k2 = sum(a[i+1:])
if abs(z1-k1)<abs(z2-k2):
	z = z1
	k = k1
else:
	z = z2
	k = k2

print(abs(z-k))

Solution (Python):
import numpy as np

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

a = np.array(a)
s = a.cumsum()
ans = abs(2*s - sum(a))


print(min(ans))

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

x = n//2
if sum(a)/2 == sum(a[:(n//2)]):
  print(0)
elif sum(a)/2 >= sum(a[:(n//2)]):
  while sum(a)/2 >= sum(a[:x]):
    x += 1  
  print(min(abs(2*sum(a[:x])-sum(a)), abs(2*sum(a[:x-1])-sum(a))))
else:
  while sum(a)/2 <= sum(a[:x]):
    x -= 1
  print(min(abs(2*sum(a[:x])-sum(a)), abs(2*sum(a[:x+1])-sum(a))))

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
al=sum(A)
cent=al/2
now=0
for a in A:
  if abs(cent-al+now)<abs(cent-al+now+a):
    print(abs(al-2*now))
    break
  now+=a

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s = [0]
for i in range(n):
    s.append(a[i]+s[i])
m = 100000000000000
for i in range(len(s)):
	m = min(m, abs((s[i] - s[0]) - (s[len(s) - 1] - s[i])))
print(m)

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
L = 0
R = sum(A)
ans = sum(A)
for n in range(N):
    L += A[n]
    R -= A[n]
    if ans > abs(L-R):
        ans = abs(L-R)
print(ans)

Solution (Python):
from itertools import accumulate


n = int(input())
a = list(map(int, input().split()))
a_acc = list(accumulate(a))
ans = 2020202020
for i in range(n):
    ans = min(abs(a_acc[-1] - a_acc[i] - a_acc[i]), ans)
print(ans)

Solution (Python):
n = int(input())
arr = [int(a) for a in input().split()]
s = sum(arr)
m = s // 2

tmp = 0
for a in arr:
    if tmp + a >= m:
        ans = min(s-tmp*2, (tmp+a)*2-s)
        break
    tmp += a

print(ans)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    s = sum(a)
    minx = s
    sum_a = 0

    for i in range(n):
        sum_a += a[i]
        minx = min(minx, abs(sum_a - (s - sum_a)))

    print(minx)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
ruiseki = [0]+list(itertools.accumulate(A))
i = bisect.bisect_left(ruiseki,sum(A)/2)
print(min(abs(sum(A[:i])-sum(A[i:])),abs(sum(A[:i-1])-sum(A[i-1:]))))

Solution (Python):
N = int(input())
sticks = list(map(int, input().split()))
sticksSum = sum(sticks)
diffMin = sum(sticks)
plusSticks = 0
for i in range(len(sticks)):
    plusSticks += sticks[i]
    diffMin = min(diffMin, abs(2*plusSticks-sticksSum))
print(diffMin)

Solution (Python):
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n = I()
    a = Intl()
    m = sum(a)
    s = list(it.accumulate(a))
    ans = []
    for i in range(n):
        ans.append(abs(s[i]-(m-s[i])))
    print(min(ans))

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

ans = float("INF")
for i in range(1, N+1):
  lsum = S[i]
  rsum = S[N]-S[i]
  ans = min(ans, abs(rsum-lsum))
  
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
diff = float("INF")
idx = -1
m = sum(A)/2
for i in range(N):
  S[i+1] = S[i] + A[i]
  if abs(m - S[i+1]) < diff:
    diff = abs(m - S[i+1])
    idx = i+1
lsum = S[idx]
rsum = S[N]-S[idx]
print(abs(lsum-rsum))

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

total = sum(a)
cum_sum = list(accumulate(a))

ans = float("INF")
for former in cum_sum:
    ans = min(ans, abs(former - (total - former)))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
c = 0
d = 10000000000000000
a = 0
for i in range(N):
  c += 2*A[i]
  if abs(c - s) < d:
    a = i
    d = abs(c - s)
a1 = sum(A[:(a+1)])
a2 = sum(A[(a+1):])
ans = abs(a1-a2)
print(ans)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
cnt=0
i=0
s=sum(a)
while cnt<s//2:
    temp=cnt
    cnt+=a[i]
    i+=1
    
print(min(abs(sum(a)-temp*2),abs(sum(a)-cnt*2)))

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

s = sum(A)
r = 10**10
n = 0
for i in range(N-1):
    n += A[i]
    r = min(abs(s-n*2), r)

print(r)

Solution (Python):


















import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())












n = int(input())
A = readInts()

total = sum(A)
mi_ = float('inf')
left = 0
for i in range(n):
    left += A[i]
    total -= A[i]
    mi_ = min(mi_, abs(left - total))
print(mi_)

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

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

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

from itertools import accumulate

a_csum = list(accumulate(a))

ans = 10**10

for i in range(n - 1):
    ans = min(ans, abs(a_csum[i] - (a_csum[-1] - a_csum[i])))

print(ans)

Solution (Python):
import sys


if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    pref = [a[0]]
    cost = 10**18
    for  i in range(1,n):
        pref.append(pref[i-1] + a[i])
    for i in range(n):
        d1 = pref[i]
        d2 = pref[-1] - pref[i]
        cost = min(cost,abs(d1-d2))
    print(cost)

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=lmp()
x=0
y=sum(a)
ans=abs(x-y)
for i in range(n):
    x+=a[i]
    y-=a[i]
    ans=min(ans,abs(x-y))
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
HALF = sum(As)/2

from itertools import accumulate
from bisect import bisect_left
cum = list(accumulate(As))
idx = bisect_left(cum, HALF)
L = abs(sum(As[:idx])-sum(As[idx:]))
R = abs(sum(As[:idx+1])-sum(As[idx+1:]))
print(min(L, R))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
l = A[0]
r = sum(A[1:])
x = abs(l-r)
for a in A[1:]:
    l += a
    r -= a
    if x > abs(l-r): x = abs(l-r)
print(x)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
SUM = sum(A)
half = SUM//2
acc = 0;dif = SUM
for i in range(N):
  acc += A[i]
  dif = min(dif,abs(SUM-2*acc))

print(dif)

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from itertools import accumulate
    acc = [a for a in accumulate([0] + A)]
    ans = acc[-1]
    for i in range(1, N+1):
        a = acc[i]
        b = acc[N] - acc[i]
        v = abs(a-b)
        ans = min(ans, v)
    print(ans)


if __name__ == '__main__':
    main()

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

Sum = sum(a) // 2

cand = 0
for i in range(n):
    temp = cand + a[i]
    if abs(cand - Sum) < abs(temp - Sum):break
    cand = temp
cost = abs(Sum - cand) + abs(Sum - (sum(a) - cand))
print(cost)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
temp=sum(a)
ans=0
t=9999999999999999
tsum=0
ta=0
for i in range(n):
  tsum=tsum+a[i]
  if abs(tsum-temp/2)<t:
    t=abs(tsum-temp/2)
    ta=tsum
print(abs(int(ta)*2-temp))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
A = LIST()

ans = INF
A_sum = sum(A)
tmp = 0
for x in A:
	tmp += x
	ans = min(ans, abs(tmp-(A_sum-tmp)))

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
sm=sum(a)
x=[a[0]]
y=[sm-a[0]]
for i in range(1,n):
  x.append(x[i-1]+a[i])
  y.append(sm-x[i])
min=9999999999
for (p,q) in zip(x,y):
  sa=abs(p-q)
  if sa<min:
    min=sa

print(min)

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

l = sum(A)
ans = float("inf")
t = 0

for i in range(N-1):
	t += A[i]
	ans = min(ans,abs(l-2*t))
	

print(ans)

Solution (Python):
N,A=open(0)
*A,=map(int,A.split())
S=M=sum(A)
C=0
for a in A:
    C+=a
    M=min(M,abs(S-2*C))
print(M)

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

count1=0
count2=[0]*N
i=0
while i<N:
    count1+=A[i]
    count2[i]=abs(S-2*count1)
    i+=1

print(min(count2))

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

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

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N = I()
L = IL()
S = sum(L)
LL = L[::-1]
SUM = 0
while SUM<S/2:
  SUM+=L.pop()
ans1 = abs(S-SUM*2)
  
SUM=0
while SUM<S/2:
  SUM+=LL.pop()
ans2 = abs(S-SUM*2)
  
print(min(ans1,ans2))

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
ans = 100000000000000000

for i in range(1,n):
    if i == 1:
        x = sum(li[:i])
        y = sum(li[i:])
    else:
        x += li[i-1]
        y -= li[i-1]
    z = abs(x-y)
    if z < ans:
        ans = z
    
print(ans)

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

right = sum(a)
left = 0
ans = float("inf")

for i in a:
    left += i
    right -= i
    
    ans = min(ans, abs(right-left))
print(ans)

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

almost_half = sum(A)/2

former = 0
for n in range(N):
  former += A[n]
  if former == almost_half:
    print(0)
    break
    
  if former > almost_half:
    if n == 0:
      
      print(abs(A[0]-sum(A[1:])))
      break
    elif n == N-1:
      
      print(abs(A[n]-sum(A[:n])))
      break
    else:
      
      
      print(min(abs(sum(A[n:])-sum(A[:n])),abs(sum(A[n+1:])-sum(A[:n+1]))))
      break

Solution (Python):

import math
from decimal import Decimal
from itertools import permutations


def inp():
    return(int(input()))



def inlist():
    return(list(map(int, input().split())))



def instr():
    s = input()
    return(list(s[:len(s)]))



def invr():
    return(map(int, input().split()))


n = inp()
a = inlist()
pref = [0] * (n)
suf = [0] * n
pref[0] = a[0]
suf[n-1] = a[n-1]
j = n - 2
for i in range(1, n):
    pref[i] = a[i] + pref[i-1]
    suf[j] = a[j] + suf[j+1]
    j -= 1

D = 10000000000000000
for i in range(0, n-1):

    s1 = pref[i]
    s2 = suf[i+1]
    d = abs(s1 - s2)
    if d < D:

        D = d

print(D)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split(" ")]
Min = float("inf")
index = 0
L = 0
R = sum(A)
for i in range(0,len(A)):
    L+=A[i]
    R-=A[i]
    diff = abs(L - R)
    if diff < Min:
        Min = diff
print(Min)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
left = 0
SUM = sum(A)
res = SUM
for i in range(N):
    left += A[i] 
    res = min(res,abs(2*left-SUM))
print(res)

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10**7)

ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

n = ni()
a = na()

ans = inf
s = sum(a)
cur = 0
for i in range(n):
    cur += a[i]
    ans = min(ans, abs(cur * 2 - s))

print(ans)

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

Solution (Python):
from itertools import accumulate
n = int(input())
A = list(map(int,input().split()))
B = list(accumulate(A))
m = float("inf")
for i in B:
    m = min(m,abs(2*i-B[-1]))
print(m)

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

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

diff_min = 10**16

for i in range(N):
    diff_min = min(diff_min,abs(A[N-1]-2*A[i]))

print(diff_min)

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int, input().split()))
    com_left = [0] * (N+1)
    com_right = [0] * (N+1)
    total = sum(A)
    for i in range(N):
        com_left[i+1] = com_left[i] + A[i]
        com_right[i] = total - com_left[i]
    ans = 2020202020
    for i in range(N):
        diff = abs(com_left[i] - com_right[i])
        ans = min(ans, diff)
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int, input().split()))
    mid = sum(A) / 2
    x = 0
    near_length = 0
    for a in A:
        x += a
        if x >= mid:
            if x - mid > abs(x - a - mid):
                near_length = abs(x - a)
            else:
                near_length = x
            break
    ans = int(abs(mid-near_length) * 2)
    print(ans)
if __name__ == "__main__":
    main()

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

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

    s1=[0]*n
    s2=[0]*n
    for i in range(n):
        if i==0:
            s1[i]=a[i]
        else:
            s1[i]+=s1[i-1]+a[i]

    for i in range(n-1,-1,-1):
        if i==n-1:
            s2[i]=a[i]
        else:
            s2[i]+=s2[i+1]+a[i]

    ans=float("inf")
    for i in range(n-1):
        ans=min(ans,abs(s1[i]-s2[i+1]))

    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
from itertools import accumulate


N = int(input())
A = [0] + list(accumulate(map(int, input().split())))
min_diff = float("inf")
for i in range(N - 1):
    if abs(A[N] - 2 * A[i + 1]) < min_diff:
        min_diff = abs(A[N] - 2 * A[i + 1])
print(min_diff)

Solution (Python):
n=int(input())
a=[int(x) for x in input().rstrip().split()]
full=sum(a)
now=0
ans=float('inf')
for i in a:
  now+=i
  ans=min(abs(full-2*now),ans)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]



from itertools import accumulate 

N = ii()
A = li()

acc = list(accumulate(A))


for i in range(N):
    if acc[i] - (acc[-1] - acc[i]) >= 0:
        break

if i == N-1:
    print(abs(A[i] - acc[i-1]))
elif i == 0:
    print(abs(acc[i] - (acc[-1] - acc[i])))
else:
    ans = min(abs(acc[i] - (acc[-1] - acc[i])), abs(acc[i-1] - (acc[-1] - acc[i-1])))
    print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

ans = INF

sum_A = sum(A)
tmp = 0
for a in A:
	tmp += a
	ans = min(ans, abs(tmp-(sum_A-tmp)))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
L = 0
for i, a in enumerate(A):
  if L + a <= S/2:
    L += a
  else:
    R = S - L
    diff = R - L
    if 2*a - diff < diff:
      L += a
      R -= a
    break
    
R = S - L
print(abs(R - L))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
for i in range(n - 1):
	A[i + 1] += A[i]
minv = float('inf')
for a in A[:-1]:
	minv = min(minv, abs(A[-1]/2 - a))
print(int(minv * 2))

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

Solution (Python):
import numpy
import numpy as np
N=int(input())
AList=list(map(int,input().split()))
AListSum=sum(AList)
CumSum=numpy.cumsum(numpy.array(AList))
CumSum=list(CumSum)




def getNearestValue(List, num):
    idx = np.abs(np.asarray(List) - num).argmin()
    return idx


MAE=sum(AList[:(getNearestValue(CumSum, AListSum/2))+1])
USHIRO=sum(AList[(getNearestValue(CumSum, AListSum/2))+1:])
print(abs(MAE-USHIRO))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = []
sum_a = sum(a)
cum = 0
for i in a:
  cum += i
  b.append(abs(sum_a  - cum * 2))
print(min(b))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
x=sum(A)
y=10**19
a=0
for i in range(N):
    a+=A[i]
    if y>abs(x-2*a):
        y=abs(x-2*a)
        
    else:
        break

print(y)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a_cumsum = [0]*(n+1)
for i in range(n):
    a_cumsum[i+1] = a_cumsum[i] + a[i]

ans = 10**18
for i in range(n):
    ans = min(ans, abs(a_cumsum[-1] - a_cumsum[i]*2))
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
total=sum(A)
cost=total
cumulative=0
for i in range(N):
    cumulative+=A[i]
    cost=min(cost,abs(cumulative-(total-cumulative)))
print(cost)

Solution (Python):


n = int(input())
nums = [int(i) for i in input().split()]
for i in range(n - 1):
    nums[i + 1] += nums[i]

ans = float('inf')
for i in range(n - 1):
    d = abs(nums[-1] - nums[i] * 2)
    ans = min(ans, d)
print(ans)

Solution (Python):
import sys, itertools

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

S = list(itertools.accumulate(A))

mini = 10 ** 18
for i in range(N):
    mini = min(mini, abs(S[N - 1] - 2 * S[i]))

print(mini)

Solution (Python):
import sys

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))
S = [0] * (N + 1)
s = sum(A)

for i in range(N):
    S[i + 1] += S[i] + A[i]

mini = 10 ** 18
for i in range(1, N + 1):
    mini = min(mini, abs(S[N] - 2 * S[i]))

print(mini)

Solution (Python):
import bisect
n=int(input())
a=list(map(int,input().split()))
suma=[]
sumans=0
for i in range(n):
	sumans += a[i]
	suma.append(sumans)
u=bisect.bisect_right(suma, sumans//2)
print(min(abs(2*suma[u-1]-sumans), abs(2*suma[u]-sumans)))