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):
n=int(input())
a=list(map(int,input().split()))
l=0
l0=0
a_sum=sum(a)
for i in range(n):
    l+=a[i]
    if l*2>=a_sum:
        l0=l-a[i]
        break
print(min(2*l-a_sum,a_sum-2*l0))

Solution (Python):
N = int(input())
Alist = list(map(int, input().split()))
ans = float("inf")
S = sum(Alist)
from itertools import accumulate
R = list(accumulate(Alist))
for r in R:
    ans = min(ans, abs(r - (S-r)))
print(ans)

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

all_sum = sum(A_list)
F_sum_list = [A_list[0]]

for j in range(1,N):
    F_sum_list.append(F_sum_list[-1] + A_list[j])

delta_list = [abs(all_sum - 2* i) for i in F_sum_list]

print(min(delta_list))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
t = s // 2
count = 0
for a in A:
  count += a
  if count >= t:
    n = count
    m = count - a
    break
print(min(2*n - s, s - 2*m))

Solution (Python):
from itertools import accumulate
N,*A=map(int, open(0).read().split())
B=list(accumulate(A))
C=sum(A)
ans = float("inf")
for b in B[:-1]:
    ans=min(ans,max(b,C-b)-min(b,C-b))
print(ans)

Solution (Python):
n = int(input())
dat = list(map(int, input().split()))
t = sum(dat)
target = t//2
c = 0
c2 = t
res = 99999999999999999
for i in range(n):
    c += dat[i]
    c2 -= dat[i]
    res = min(res, abs(c-c2))

print(res)

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

s = sum(A)

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

ans = 10 ** 20

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

print(ans)

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

len_f = sum(a)
sum_a = 0
min = 2020202021

for i in range(n):
    sum_a += a[i]
    len_f -= a[i]
    if min > abs(sum_a - len_f):
        min = abs(sum_a - len_f)

print(min)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
len_F = sum(A)
min_F = 2020202021
sum_A = 0

for i in range(N):
    sum_A += A[i]
    len_F -= A[i]
    if(abs(sum_A - len_F) < min_F):
        min_F = abs(sum_A - len_F)
        
print(min_F)

Solution (Python):
import sys

N, *A = map(int, sys.stdin.read().split())

d = 10 ** 20
r = 0
l = sum(A)
for a in A:
    r += a
    l -= a
    if abs(l - r) < d:
        d = abs(l - r)
print(d)

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

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

all_sum = sum(A_list)

F_sum_list = [A_list[0]]

for j in range(1,N):
    F_sum_list.append(F_sum_list[-1] + A_list[j])

delta_list = [abs(all_sum - 2* i) for i in F_sum_list]

print(min(delta_list))

Solution (Python):
import sys
n = int(input())
a = list(map(int, input().split()))
tmp=sys.maxsize
s = sum(a)
t = 0
for i in range(n):
    t += a[i]
    tmp = min(tmp,abs(s-t*2))
print(tmp)

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
s = sum(A)
p = 10**20
t = 0
for i in range(n):
    t += A[i]
    p = min(p, abs(s-t*2))
print(p)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
P = 10**20
X = 0
for i in range(N):
  X = X + A[i]
  P = min(P,abs(S-X*2))
print(P)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
sep_i = 0
for i, a in enumerate(A):
  sum_left += a
  if sum_left >= mid:
    sep_i = i
    sum_left2 = sum_left - a
    break
sum_right, sum_right2 = sum(A[sep_i + 1:]), sum(A[sep_i:])
print(min(abs(sum_right - sum_left), abs(sum_right2 - sum_left2)))

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

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

    l = [0 for i in range(N)]
    l[0] = a[0]

    for i in range(1, N):
        l[i] = l[i-1] + a[i]
    ans = int(1e15)
    for i in range(N):
        ans = min(ans, abs(l[-1] - l[i] * 2))
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
sep_idx = 0
for i, l in enumerate(A):
  sum_left += l
  if sum_left >= mid:
    sep_i = i
    sum_left2 = sum_left - l
    break
sum_right, sum_right2 = sum(A[sep_i + 1:]), sum(A[sep_i:])
print(min(abs(sum_right - sum_left), abs(sum_right2 - sum_left2)))

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

    total = sum(A)
    s = 0
    m = total
    for a in A:
        s += a
        tmp = abs(total- 2*s)
        if tmp < m:
            m = tmp

    print(m)
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
seperation_i = 0

for i, l in enumerate(A):
  sum_left += l
  if sum_left >= mid:
    seperation_i = i
    sum_left2 = sum_left - l
    break
sum_right, sum_right2 = sum(A[seperation_i + 1:]), sum(A[seperation_i:])
ans = min(abs(sum_left - sum_right), abs(sum_left2 - sum_right2))
print(ans)

Solution (Python):

from bisect import bisect
from itertools import accumulate


def main():
    N, *A = map(int, open(0).read().split())
    cumsum = tuple(accumulate(A))
    idx = bisect(cumsum, cumsum[-1] // 2)
    print(min(cumsum[idx] * 2 - cumsum[-1], cumsum[-1] - cumsum[idx - 1] * 2))


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
mid = sum(A)/2
length = 0
i = 0
while True:
    if length >= mid:
        length = min(abs(length-mid),abs(length-mid-A[i-1]))+mid
        break
    length += A[i]
    i += 1
print(int(length-(sum(A)-length)))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
seperation_i = 0
for i, l in enumerate(A[:-1]):
  sum_left += l
  if sum_left >= mid:
      seperation_i = i
      sum_left2 = sum_left - l
      break
sum_right, sum_right2 = sum(A[seperation_i + 1:]), sum(A[seperation_i:])
print(min(abs(sum_right - sum_left), abs(sum_right2 - sum_left2)))

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

l=0
r=0

li=0
ri=0

for i in range(N):
    if l<=r:
        l+=A[li]
        li+=1
    else:
        r+=A[-1-ri]
        ri+=1
        
print(abs(l-r))

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

S = sum(A)
s = 0
res = S

for a in A:
    s += a
    cost = max(s, S - s) - min(s, S - s)
    res = min(res, cost)

print(res)

Solution (Python):
n = int(input())
*a, = map(int, input().split())
tot = sum(a)

ret = tot
l, r = 0, tot
for i in range(n):
    l += a[i]
    r -= a[i]
    ret = min(ret, abs(l - r))
print(ret)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
left_sum = 0
ans = float('inf')
for i in range(N):
    left_sum += A[i]
    right_sum = total - left_sum
    ans = min(ans, abs(left_sum - right_sum))
print(ans)

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




pref = 0
for x in a:
    pref += x
    rem = tot - pref
    ans = min(ans, abs(rem - pref))

print(ans)

Solution (Python):
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil
sys.setrecursionlimit(200000)
input = sys.stdin.readline









N, = map(int, input().split())
L = [int(v) for v in input().split()]

ans = 10 ** 18
a = sum(L)
b = 0

for i in range(N):
    index = (i + 1) * -1
    a -= L[index]
    b += L[index]

    if a == b:
        print(0)
        exit()

    ans = min(ans, abs(a - b))

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    s = sum(a)
    t = 0
    r = inf
    for c in a:
        t += c
        k = abs(s-t*2)
        if r > k:
            r = k

    return r


print(main())

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

tot = sum(A)
epi = tot
pre = 0
dif = tot
for i in range(n):
	pre += A[i]
	epi -= A[i]

	if abs(pre-epi) > dif:
		break
	else:
		dif = abs(pre-epi)

print(dif)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sumall = 0
for a in A:
	sumall += a
ans = N * 2020202021
tmp = 0
for a in A:
	tmp += a
	ans = min(ans, abs(sumall - 2 * tmp))
print(ans)

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

S = sum(A)

cost = 1e15
acc_sum = 0
for a in A:
  acc_sum += a
  cost = min(cost, abs(S-2*acc_sum))
  
print(cost)

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

print(ans)

Solution (Python):
from math import ceil
n=int(input())
a=list(map(int,input().split()))
b=sum(a)
s=0
lis=[]
lis2=[]
if sum(a)%2==0:
  for i in range(n):
    s+=a[i]
    lis.append(abs(b//2-s))
  print(2*min(lis))
else:
  for i in range(n):
    s+=a[i]
    lis.append(abs(b//2-s))
    lis2.append(abs(ceil(b/2)-s))
  print(2*min(min(lis),min(lis2))+1)

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

a = list(map(int,input().split()))
b = []
add = 0
for i in a:
    add += i
    b.append(add)

for i in range(len(b)):
    if b[i]> add / 2:
        x = abs(add - 2 * b[i])
        y = add - 2 * b[i-1]
        ans = min(x,y)
        break
print(ans)

Solution (Python):

num=int(input())
lenlist=input().split()
lensum=0
sumlist=[]
for i in range(num):
    lensum+=int(lenlist[i])
    sumlist.append(lensum)
for i in range(num-1):
    if sumlist[i] <= lensum/2 <= sumlist[i+1]:
        print(min(abs(lensum-2*sumlist[i]),abs(lensum-2*sumlist[i+1])))
        break

Solution (Python):
N = int(input().strip())
arr = list(map(int, input().split()))
_sum = sum(arr)
now = 0
ans = 1e18
for i in arr:
    now += i
    rest = _sum - now
    ans = min(ans, abs(rest-now))
print(ans)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
S=sum(A)
X=0
Y=S
d=S
for i in range(N):
    X+=A[i]
    Y-=A[i]
    d=min(d,abs(X-Y))
print(d)

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

total = sum(A)

s = 0
for a in A:
	s += a
	if 2 * s >= total:
		break

B = A[::-1]
t = 0
for b in B:
	t += b
	if 2 * t >= total:
		break

print(min(2*s-total, 2*t-total))

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    sa = sum(a)
    t = 0
    mt = 10 ** 20
    for v in a:
        t += v
        mt = min(mt, abs(sa - t - t))
    print(mt)


if __name__ == '__main__':
    solve()

Solution (Python):


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


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




diff = int(1e18)
for cut in range(1, N):
    
    costL = cA[cut - 1]
    costR = cA[-1] - cA[cut - 1]
    
    diff = min(diff, abs(costL - costR))

print(diff)

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

cnt = 0
ans = s
for i in a:
    cnt += i
    ans = min(ans, abs(s - 2 * cnt))

print(ans)

Solution (Python):
def solve(n, a):
    total = sum(a)
    best = total
    cur = 0
    for i in range(n):
        cur += a[i]
        best = min(best, abs(2*cur - total))
    return best

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Asum = sum(A)
Asum2 = 0
c = 0
for mid in range(len(A)):
    if 2*Asum2 > Asum:
        c =1
        Asum3 = Asum2
        mid -= 1
        Asum2 -= A[mid]
        break
    Asum2 += A[mid]
if c == 0:
    if 2*Asum2 > Asum:
        Asum3 = Asum2
        mid -= 1
        Asum2 -= A[mid]
if abs(Asum - 2*Asum2) <= abs(Asum - 2*Asum3):
    Asuma = Asum2
else:
    Asuma = Asum3
c = abs(Asum - 2*Asuma)
print(c)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
h = 0
ans = 0
for i in a:
    if (h + i > s / 2):
        ans = min(int(abs(h - s / 2) / 0.5), int(abs((h + i) - s / 2) / 0.5))
        break
    h += i
print(ans)

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

left = 0
right = sum(A)
ans = float('inf')
for a in A:
    left += a
    right -= a
    ans = min(ans, abs(left - right))

print(ans)

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

cum = [0]
for a in A:
    cum.append(cum[-1]+a)

ans = float('inf')
for i in range(n-1):
    s = cum[i+1]
    t = cum[-1] - s
    ans = min(ans, abs(s-t))

print(ans)

Solution (Python):
import bisect
import math
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    A_sum = sum(A)
    A_cumsum_l = [0 for _ in range(N)]
    A_cumsum_l[0] = A[0]
    for i in range(1,N):
        A_cumsum_l[i] = A_cumsum_l[i-1] + A[i]
    A_cumsum_r = [0 for _ in range(N)]
    for i in range(N):
        A_cumsum_r[i] = A_sum - A_cumsum_l[i]
    
    index = 0
    sub = 10**12
    for i in range(N):
        if abs(A_cumsum_l[i]-A_cumsum_r[i])<sub:
            index = i
            sub = abs(A_cumsum_l[i]-A_cumsum_r[i])


    print(sub)


if __name__ == "__main__":
    main()

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

S = sum(A)

v = 0
u = 0
for a in A:
    v += a
    if v >= S / 2:
        break
    u += a

print(min(v * 2 - S, S - u * 2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
ans = s
now = 0
for x in a:
    now += x
    ans = min(ans, abs(s - now - now))
print(ans)

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

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
total=sum(arr)
acum=[arr[0]]
for i in range(1,n):
  acum.append(arr[i]+acum[-1])
ans=10**18
for i in range(n):
  tmp=abs(acum[i]-(total-acum[i]))
  ans=min(ans,tmp)
print(ans)

Solution (Python):

def resolve():
    N=int(input())
    A=[int(i) for i in input().split()]
    for i in range(1,N):
        A[i]+=A[i-1]
    print(min([abs(A[-1]-2*A[i]) for i in range(N)]))





if __name__ == "__main__":
    resolve()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
l = A[0]
r = 0
indexl = 1
indexr = N-1
for i in range(N-1):
	if l <= r:
		l += A[indexl]
		indexl += 1
	elif l > r:
		r += A[indexr]
		indexr -= 1
print(abs(l-r))

Solution (Python):
from itertools import accumulate

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

sum_l = [0]+list(accumulate(a))
sum_r = [0]+list(accumulate(a[-1::-1]))

cost = sum_r[-1]
for i in range(n+1):
    cost = min(cost, abs(sum_l[i]-sum_r[n-i]))
print(cost)

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

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

S=sum(A)
s=0

ans=10**10
for i in range(N):
  s+=A[i]
  ans=min(ans,abs((S-s)-s))
print(ans)

Solution (Python):


import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    al = list(map(int, input().split()))

    cum_len = [0] * len(al)
    cum_len[0] = al[0]
    for i in range(1, len(al)):
        cum_len[i] = cum_len[i-1] + al[i]

    
    ans = 10**10
    for i in range(len(al)):
        left = cum_len[i]
        right = cum_len[-1] - cum_len[i]
        
        ans = min(ans, abs(left - right))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
SUM = sum(A)
def sum_half():
    cnt = 0
    for i in range(N):
        cnt += A[i]
        if cnt>SUM/2:
            return i
index = sum_half()
min_cost = 10000000000
p = sum(A[:index+1])-sum(A[index+1:])
q = sum(A[index:])-sum(A[:index])
if min_cost>p:
    min_cost = p
if min_cost>q:
    min_cost = q
print(min_cost)

Solution (Python):
N = int(input())
l = list(map(int,input().split()))
Sum = sum(l)
l_l = 0
ans = 10**10
for i in range(N):
  l_l += l[i]
  ans = min(ans,abs(Sum-(2*l_l)))
print(ans)

Solution (Python):
n = int(input())
li_a = map(int, input().split())
li_a_left = list()
total = 0
for l in li_a:
    if len(li_a_left) == 0:
        li_a_left.append(l)
    else:
        li_a_left.append(li_a_left[-1] + l)
    total += l

print(min(abs(left - (total - left)) for left in li_a_left))

Solution (Python):
from itertools import accumulate
from operator import add
x = int(input())
lst = [int(i) for i in input().split()]
a = list(accumulate(lst,add))

s = sum(lst)
b = []
for i in list(a):
  b.append(s - i)

c = []
for i in range(x):
  c.append(abs(b[i] - a[i]))
print(min(c))

Solution (Python):
from itertools import accumulate as ac
n=int(input())
a=list(ac(list(map(int,input().split()))))
d=float("INF")
for i in range(n):
    d=min(d,abs(a[-1]-2*a[i]))
print(d)

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

li = [0]*(n+1)

for i in range(n):
    li[i+1] = li[i] + a[i]
    


li_sum = sum(a)

ans = 10**18

for i in range(n):
    
    if ans > abs(li_sum - 2*li[i]):
        ans = abs(li_sum - 2*li[i])
        
print(ans)

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

sun = [0] * (n +1)

for i in range(0, n):
    sun[i + 1] = sun[i] + li[i]

delta = [0] * n
for i in range(0, n):
    delta[i] = sun[i] - (sun[n] -sun[i])

ans = abs(delta[0])
for i in range(0, n):
    if ans > abs(delta[i]):
        ans = abs(delta[i])
    
print(ans)

Solution (Python):
n = int(input())
As = list(map(int, input().split()))
sum = [0]
for i in range(0, n):
    asum = sum[i] + As[i]
    sum.append(asum)

ans = 10**18
for j in range(0, n):
    l = sum[j]
    r = sum[n] - sum[j]
    find = abs(l-r)
    ans = min(ans, find)
print(ans)

Solution (Python):
def acinput():
    return list(map(int, input().split(" ")))

import numpy as np

N=int(input())
bars=acinput()

bar_cs=np.cumsum(bars)

res=10**15

for i in range(N-1):
    R=bar_cs[-1]-bar_cs[i]
    L=bar_cs[i]
    res=min(res,np.abs(R-L))
    

print(res)

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

i = 0
j = n-1
sumL =0
sumR = 0
while(i <= j):
    if(sumL < sumR):
        sumL += arr[i]
        i +=1
    else:
        sumR += arr[j]
        j -=1

print(abs(sumL - sumR))

Solution (Python):
n=int(input())
l= list((int(x) for x in input().split()))
beg=end=i=0
n-=1
while(i<=n):
	if(beg>end):
		end+=l[n]
		n-=1
	else:
		beg+=l[i]
		i+=1
print(abs(end-beg))

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

i = 1
j = n-2
forSum = arr[0]
revSum = arr[-1]

while(i <= j):
    if(forSum < revSum):
        forSum += arr[i]
        i += 1
    else:
        revSum += arr[j]
        j -= 1


print(abs(forSum-revSum))

Solution (Python):
import itertools
N = int(input())
A = [int(x) for x in input().split()]
cumsum = list(itertools.accumulate(A))
ans = float("INF")
for i in range(N):
  ans = min(ans,abs(cumsum[i]-(cumsum[-1]-cumsum[i])))
print(ans)

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    st = sum(a)
    t = 0
    abs_t = 10 ** 10
    for i in range(n-1):
        t += a[i]
        st -= a[i]
        abs_t = min(abs_t, abs(st - t))
    print(abs_t)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
n = [int(i) for i in input().split()]
l = sum(n)
s=0
for i in range(N):
        s+=n[i]
        if s>=l/2:
                if abs(l/2-s) > s1:
                        s-=n[i]
                e = l-s
                break     
        s1=abs(l/2 - s)
print(abs(s-e))

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
L_i = 0
R_i = n - 1
L = 0
R = 0
while L_i <= R_i:
  if L >= R:
    R += A[R_i]
    R_i -= 1
  else:
    L += A[L_i]
    L_i += 1
    
print(abs(L-R))

Solution (Python):



def main():
    from itertools import accumulate

    n = int(input())
    a = list(accumulate([0] + list(map(int, input().split()))))
    ans = float('inf')

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

    print(ans)


if __name__ == '__main__':
    main()

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

total = sum(A)
cut = 0
check = []

for i in A:
    cut += i
    check.append(abs(cut + cut - total))

print(min(check))

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

for i in range(n-1):
  m[i+1]+=m[i]
a=m[n-1]

for i in range(n):
  if m[i]>=a/2:
    a1=m[i]
    a2=m[i-1]
    break

a1=abs(a1*2-a)
a2=abs(a2*2-a)
print(min(a1,a2))

Solution (Python):
def find_equal(lis, n):
    sum_l = 0
    sum_r = 0
    r_c = n - 1
    l_c = 0
    all_processed = 0
    while True:
        if sum_l <= sum_r:
            sum_l += lis[l_c]
            l_c += 1
            all_processed += 1
        elif sum_r < sum_l:
            sum_r += lis[r_c]
            r_c -= 1
            all_processed += 1
        if all_processed == n:
            break
    return sum_l, sum_r


n = int(input())
nums = list(map(int, input().split()))
ans = find_equal(nums, len(nums))
print(max(ans) - min(ans))

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

s = sum(a)

half = [0,s]
for i in range(n):
    if abs(half[1]-half[0])>abs(half[1]-half[0]-2*a[i]):
        half[0]+=a[i]
        half[1]-=a[i]
    else:
        break
print(abs(half[1]-half[0]))

Solution (Python):

import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, A: "List[int]"):
    r = [0] * (N+1)
    rr = [0] * (N+1)

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

    ans = INF
    for i in range(1,N):
        ans = min(ans, abs(r[i]-rr[i]))

    print(ans)

    
    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 bisect

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

a = [0]
for aa in A:
    a.append(a[-1] + aa)
x = a[-1]/2
i = bisect.bisect(a, x)
d = min(x - a[i-1], a[i] - x)
print(int(d / 0.5))

Solution (Python):
from itertools import accumulate
import math

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

ans = 10**10

sum_list_aaa =list(accumulate(list_aaa))

for i in range(N):
    zen = sum_list_aaa[i]
    go = sum_list_aaa[-1]-sum_list_aaa[i]
    ans = min(ans,abs(zen-go))

print(ans)

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

ans = [0 for _ in range(n+1)]

for i in range(n):
    ans[i+1] = ans[i] + l[i]

num = 2020202020
for i in range(1,n):
    num = min(num,abs(ans[n] - ans[i]*2))

print(num)

Solution (Python):
N=input()
b=input().split()
sum=0
bigsum=0
for i in b:
    sum+=eval(i)
standard=sum/2
for i in b:
    bigsum+=eval(i)
    if bigsum>standard:
        smallsum=bigsum-eval(i)
        break
if bigsum-standard>standard-smallsum:
    print(sum-2*smallsum)
else:
    print(2*bigsum-sum)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
l = S//2
ans = S
s = 0
for k in range(N):
    s += A[k]
    ans = min(ans,abs(l-s)+abs(S-s-l))
print(ans)

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

ans = 10**10
s = sum(a_li)
tmp = 0

for i in range(n):
    tmp += a_li[i]
    res = abs(s - 2*tmp)
    ans = min(ans,res)
    
print(ans)

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

total = sum(a)

minimam = total
left = 0
right = total

for ai in a:
    left += ai
    right -= ai
    if abs(right - left) < minimam:
        minimam = abs(right - left)

print(minimam)

Solution (Python):
n = int(input())
x = list(map(int,input().split()))
from itertools import accumulate
X = list(accumulate([0]+x))
ans = float('inf')
for i in range(1,n):
    ans = min(ans,abs(X[-1]-X[i]*2))
print(ans)

Solution (Python):
n = int(input())
x = list(map(int,input().split()))
from itertools import accumulate
X = list(accumulate([0]+x))
ans = float('inf')
for i in range(1,n):
    ans = min(ans,abs(X[-1]-X[i]*2))
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import numpy as np
a=np.array(a)
b=np.cumsum(a)

c=sum(a)-b

d=abs(b-c)

print(min(d))

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

total = sum(A)
count = 0
cut = 0
for num, a in enumerate(A):
    count += a
    if count >= total//2:
        cut = num
        break

left = sum(A[:cut+1])
right = sum(A[cut+1:])
ans1 = abs(left-right)

left -= A[cut]
right += A[cut]
ans2 = abs(left-right)

print(min(ans1, ans2))

Solution (Python):
import bisect
import math

N = int(input())
A = [int(i) for i in input().split()]
sa = sum(A)
g = sa/2

S = []
s = 0
for a in A:
    s += a
    S.append(s)


i = bisect.bisect_left(S, g)
print(min(math.ceil(sa-S[i-1]*2), math.ceil(S[i]*2-sa)))

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

s=0
for i in range(len(x)):
	s+=x[i]
i=0
l=0
while (l<(s//2)):
	l+=x[i]
	i+=1
r=abs(s-l-l) 
i=len(x)-1
l=0
while(l<(s//2)):
	l+=x[i]
	i-=1
if abs((s-l)-l)<r:
	r=abs(s-l-l) 
print (r)

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

all_sum = sum(A_list)

F_sum_list = [A_list[0]]

for j in range(1,N):
    F_sum_list.append(F_sum_list[-1] + A_list[j])

delta_list = [abs(all_sum - 2* i) for i in F_sum_list]

print(min(delta_list))

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

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

Solution (Python):
for tc in [0]*1:

    n=int(input())
    l=list(map(int,input().split()))
    tot=sum(l)
    f=0
    ans=0
    for i in range(n):
        ans+=l[i]
        f=0
        if ans*2==tot:
            f=1
            break
    if f:
        print(0)
    else:
        ans=0
        tr=10**9+7+2020202020
        for i in range(n):
            ans+=l[i]
            
            tr=min(tr,abs(ans-abs(tot-ans)))
        print(tr)

Solution (Python):
a= int (input())
lis=list(map(int,input().split()))
for i in range (1,a):
    lis[i]=lis[i]+lis[i-1]
k=lis[-1]/2
for i in range(a):
    if lis[i]>=k:
        l=lis.index(lis[i])
        break
u=abs(2*lis[l]-lis[-1])
v=abs(2*lis[l-1]-lis[-1])
print(min(u,v))

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

j=0

for i in range(n):
  j+=l[i]
  if j*2==s:
    print(0)
    break
  elif j*2>s:
    print(min((j*2-s),(s-(j-l[i])*2)))
    break

Solution (Python):
import numpy as np
import bisect
N = int(input())
A = list(map(int,input().split()))
sumA = sum(A)
cumsum = np.cumsum(A)
ans = 2020202020
for a in cumsum:
    ans = min(ans,abs(2*a-sumA))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
T = 0
cost = float("inf")
for a in A:
    T+=a
    S-=a
    cost = min(cost,abs(T-S))
print(cost)

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def calc_sum(v, n):
    a = [0] * (n)
    for i in range(n):
        if i == 0:
            a[i] = v[i]
            continue
        a[i] = a[i-1] + v[i]
    return a

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))

    a = calc_sum(v, n)
    b = list(reversed(calc_sum(list(reversed(v)), n)))

    ans = INF
    for i in range(n-1):
        ans = min(ans, abs(a[i] - b[i+1]))
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
s = sum(a)
al = []

sum1 = 0

for i in range(0,n):
        
        
        
        
        sum1 += a[i]
        sum2 = s - sum1
        
        
        al.append(abs(sum1 - sum2))
        if sum1 == sum2:
                print(0)
                exit()

minDiffIdx = al.index(min(al))





tmp1 = sum(a[0:minDiffIdx])
tmp2 = sum(a[minDiffIdx:])


diff1 = abs(tmp1 - tmp2)

tmp3 = sum(a[0:minDiffIdx + 1])
tmp4 = sum(a[minDiffIdx + 1:])


diff2 = abs(tmp3 - tmp4)
print(min(diff1, diff2))