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()))
SUM = sum(a) 
ans = []
l = [[0, SUM]]
for i in range(n):
    l.append([l[i][0] + a[i], SUM - (l[i][0] + a[i])])
for i in l:
    ans.append(abs(i[0] - i[1]))
print(min(ans))

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

for i in range(N):
    d.append(abs(sA[i] - (sA[-1] - sA[i])))
print(min(d))

Solution (Python):
def modelAnswer(N:int,A:list) -> int:
   T1 = [A[0]]
   T2 = [sum(A)]
   for i in range(1,N):
      T1.append(T1[-1]+A[i])
      T2.append(T2[0] - T1[i-1])
   
   ans = 10**10
   for i in range(len(T1)-1):
      
      ans = min(ans,abs(T1[i]-T2[i+1]))
   return ans


def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(modelAnswer(N,A[:]))
if __name__ == '__main__':
   main()

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

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

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

Solution (Python):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(input())
    A = np.array(input().split(), np.int64)
    B = np.append(0, A.cumsum())
    C = A[::-1].cumsum()
    C = np.append(C[::-1], 0)
    print(np.abs(B-C).min())



if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

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

r = sum(A)
l = 0

ans = np.inf
for ai in A:
    l += ai
    r -= ai
    ans = min(np.abs(r-l), ans)

print(ans)

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

sum_a = sum(a)
tmp = 0
p = sum_a - 2 * tmp

for x in a:
    tmp += x
    p = min(p, abs(sum_a - 2 * tmp))

print(p)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
f=0
p=suma-2*f
for i in a:
    f+=i
    p=min(p,abs(suma-2*f))
print(p)

Solution (Python):
from statistics import median


from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect










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

def readInts():
  return list(map(int,input().split()))
def main():
    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_)
if __name__ == '__main__':
  main()

Solution (Python):
n=int(input())
A =list(map(int,input().split()))
k = sum(A)/2
cnt=0
cnt2 = 0
for i in A:
    k -= i
    if k < 0:
        cnt += abs(k) 
        cnt2 += k + i
        break
    
print(int(2*min(cnt,cnt2)))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
count=0
suml=sum(l)
i=0
while count<suml/2:
    count+=l[i]
    i+=1
k=suml/2
a=l[i-1]
if count==k:
    print(0)
else:
    print(int(2*(min(count-k,a-count+k))))

Solution (Python):
n = int(input())
A = []

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

sum_b = sum(B)
left = 0
for i in range(n-1):
    left += B[i]
    right = sum_b - left
    A.append(abs(right - left))
print(min(A))

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
r = ans = sum(a)
l = 0
ans = float('inf')
for i in a:
    r -= i
    l += i
    ans = min(ans,abs(r-l))
print(ans)

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

print(ans)

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

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

Solution (Python):
from numpy import*
input();a=array(list(map(int,input().split())))
print(int(min(abs(cumsum(a)-(sum(a)/2)))*2))

Solution (Python):
from numpy import*
n, *a = map(int, open(0).read().split())
a = array(a)
print(int(min(abs(cumsum(a)-(sum(a)/2)))*2))

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

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

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

s = sum(A)
ans = float("inf")
cum = 0

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

print(ans)

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

result = 2020202020
num_left = 0
num_right = sum(a)

for i in range(n):
    num_left += a[i]
    num_right -= a[i]
    result = min(result, abs(num_left - num_right))
print(result)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
index = 0
suml = 0
sumr = sum(A)
diff = sumr
ans = 0
for i in range(n):
    suml += A[i]
    sumr -= A[i]
    tmp  = abs(sumr -suml)
    if diff < tmp:
        ans = diff
        break
    diff = tmp
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n = int(input())
    a =[0]+list(map(int, input().split()))
    b = list(accumulate(a))
    c = sum(a)
    res = 10000000000000000
    for i in range(1, n):
        s = b[i]
        t = c-s
        res = min(res, abs(s-t))
    print(res)


if __name__ == '__main__':
    main()

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

Solution (Python):
import numpy

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

if res[-1] % 2 == 0:
  L = res[-1]//2
  ans = [abs(r - L) for r in res]
  print(2*min(ans))
else:
  L1 = res[-1]//2
  L2 = L1 + 1
  ans = [min(abs(r - L1),abs(r-L2)) for r in res]
  print(2*min(ans)+1)

Solution (Python):
n = int(input())
al = list(map(int, input().split()))
s = sum(al)
acc = 0
res = 10**18
for a in al:
    acc += a
    res = min(res, abs((s-acc)-acc))
print(res)

Solution (Python):









import numpy as np






N = int(input())

arrA = np.array(input().split(),dtype=np.int64)

arrA = arrA.cumsum()
high = arrA[-1]
ans = 2020202020*200000+2

for i in arrA[:-1]:
    num = abs(high - i*2)
    ans = min(ans,num)

print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
sa = sum(a)
ha = sa/2
x = 0
for i in range(n):
  x+=a[i]
  if x>=ha:
    break
y = sa-x
print(min(abs(x-y),abs((x-a[i])-(y+a[i]))))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
for i in range(1, N):
    A[i] += A[i - 1]
half = A[-1] / 2

A = [abs(i - half) for i in A]
print(int(min(A) * 2))

Solution (Python):
from itertools import accumulate
def myAnswer(N:int,A:list) -> int:
   accum = list(accumulate(A))
   ans = 10**10
   now = accum.pop()
   for a in accum:
      if(abs(now - a - a) < ans):
         ans = abs(now - 2*a)
   return ans

   

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

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

def ruiseki(a):
    right, left = [], []
    l, r = 0, 0
    for i in range(n):
        l += a[i]
        left.append(l)
    
        r += a[-(i+1)]
        right.append(r)
    return left, right[::-1]

l, r = ruiseki(a)
ans = [abs(l[i] - r[i+1]) for i in range(n-1)]
print(min(ans))

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
sA = sum(A)
ans = float('inf')
l = [A[0]]
for i in A[1::]:
    l.append(l[-1]+i)
    if sA < 2*l[-1]:
        print(min(abs(sA-2*l[-1]),abs(sA-2*l[-2])))
        exit()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
s = 0
for bar in a:
    if (s + bar) * 2 <= total:
        s += bar
    else:
        ans = min((s + bar) * 2 - total, total - s * 2)
        break

print(ans)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
s = sum(li)
sum_li = []
left = 0
right = s
for i,l in enumerate(li):
    if i == len(li)-1:
        break
    left += l
    right -= l
    sum_li.append(abs(left-right))
print(min(sum_li))

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
s = sum(li)
sum_li = []
for i,l in enumerate(li):
    if i == 0:
        sum_li.append(l)
        continue
    sum_li.append(sum_li[-1]+l)
all_li = []
for ii,ss in enumerate(sum_li):
    if ii == len(sum_li)-1:
        break
    all_li.append(abs(s-2*ss))

print(min(all_li))

Solution (Python):

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

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


tmp = 2 * pow(10, 10) + 1
for i in su:
    
    
    tmp = min(tmp, abs(i - su[-1]/2))


"""

l = 0
r = n-1
while (l - r) >= 1:
    c = (l + r) // 2
    
    if su[c] < su[-1] / 2:
        l = c
    elif su[c] > su[-1] / 2:
        r = c

tmp = min(abs(su[l] - su[-1]/2), abs(su[r] - su[-1]/2))
print(tmp)
"""

ans = 2 * tmp
print(int(ans))

Solution (Python):

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

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


tmp = 2 * pow(10, 10) + 1
for i in su:
    if abs(i - su[-1]/2) < tmp:
        tmp = abs(i - su[-1]/2)


"""

l = 0
r = n-1
while (l - r) >= 1:
    c = (l + r) // 2
    
    if su[c] < su[-1] / 2:
        l = c
    elif su[c] > su[-1] / 2:
        r = c

tmp = min(abs(su[l] - su[-1]/2), abs(su[r] - su[-1]/2))
print(tmp)
"""

ans = 2 * tmp
print(int(ans))

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
a = LIST()
arr = [0 for _ in range(n)]
arr[0] = a[0]
for i in range(1, n):
    arr[i] = arr[i - 1] + a[i]
s = arr[-1]
mn = inf
for i in range(n):
    mn = min(mn, abs(s / 2 - arr[i]))
print(int(mn * 2))

Solution (Python):

import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


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


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

    

    S=B[-1]
    minans = 10 ** 20
    for i in range(1,N):
        val=abs(B[i]-(S-B[i]))
        minans=min(minans,val)
    print(minans)


resolve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from itertools import accumulate
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    A=list(accumulate(A))
    min_A=100000000000
    for a in A:
        min_A=min(min_A,abs(A[-1]/2-a))
    print(int(min_A*2))

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from itertools import accumulate
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    A=list(accumulate(A))
    min_A=100000000000
    for a in A:
        min_A=min(min_A,abs(A[-1]/2-a))
    print(int(min_A*2))

if __name__=='__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
SUM = sum(a)
cnt = 0
l = []
for i in range(n):
    cnt += a[i]
    l.append(abs(cnt - (SUM - cnt)))
print(min(l))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
countUp = 0
sumA = sum(A)
minA = 2020202020 * N
for x in range(N):
    countUp += A[x]
    sumA -= A[x]
    minA = min(minA, abs(countUp - sumA))
print(minA)

Solution (Python):
import itertools

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

reg = 10**12
Acum = list(itertools.accumulate(A))
for i in range(N):
    L = Acum[i]
    R = Acum[-1] - L
    tmp = abs(L-R)
    reg = min(reg, tmp)
print(reg)

Solution (Python):
import numpy as np

N = int(input())
A = [int(x) for x in input().split()]
A = np.array(A)

Acum = A.cumsum()
L = Acum[:-1]
R = Acum[-1] - L
print(abs(L-R).min())

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

Solution (Python):
import itertools


N = int(input())
A = [int(x) for x in input().split()]
Acum = list(itertools.accumulate(A))

reg=10**12
for i in range(N):
    reg = min(reg,
             abs(Acum[-1] - Acum[i] * 2))
    
print(reg)

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

ans = sum(a)
ans2 = 0
for i in range(n):
    if abs((ans - a[i]) - (ans2 + a[i])) < abs(ans - ans2):
        ans2 += a[i]
        ans -= a[i]
    else:
        break

print(abs(ans - ans2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
ans = float("INF")
LS = [0]*(N+1)
LS[0] = 0
for i in range(N-1):
  LS[i+1] = A[i] + LS[i]
  ans = min(ans, abs(LS[i+1] - (S - LS[i+1])))
print(ans)

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

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

m = float("INF")
j = -1
for i in range(1, N+1):
  if abs(c - S[i]) < m:
    m = abs(c - S[i])
    j = i

print(abs((S[N] - S[j]) - S[j]))

Solution (Python):
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    r = sum(a)
    l = 0
    ans = r
    for i in a:
        l += i
        r -= i
        ans = min(ans, abs(r - l))
        if l >= r:
            break
    print(ans)

main()

Solution (Python):

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

left = 0
right = sum(A)
ans = right

for a in A:
	left += a
	right -= a
	
	diff = abs(right - left)
	ans = min(ans, diff)
	
	if left > right:
		break

print(ans)

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

cumulative = [0]
for i in range(N):
    cumulative.append(cumulative[i] + A[i])
lst = []
for i in range(1, N):
    a = abs(cumulative[i] - (cumulative[-1] - cumulative[i]))
    lst.append(a)

print(min(lst))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = 10**18
total = sum(A)
acc = 0
for a in A:
  acc += a
  ans = min(ans, abs(total - acc * 2))
print(ans)

Solution (Python):
from collections import deque

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

A = deque(A)
left = 0
right = 0
while len(A) > 1:
    if (left <= right):
        left += A.popleft()
    else:
        right += A.pop()
if left < right:
    left += A.pop()
else:
    right += A.pop()

print(max(left, right) - min(left, right))

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

for i in range(n):
    l1 += a[i] 
    if l1> s//2:break
r1 = s - l1
l2 = l1 -a[i]
r2 = s - l2
ans = min(abs(r1-l1),abs(r2-l2))
print(ans)

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]
sumA = sum(A)
res = sumA
cc = 0
for a in A:
  cc += a
  res = min(res, abs(sumA - 2 * cc))
print(res)

Solution (Python):
from numpy import cumsum

N = int(input())
A = list(map(int, input().split()))
a_cum = cumsum(A)
cand = [abs(a_cum[i] - (a_cum[-1] - a_cum[i])) for i in range(N)]
print(min(cand))

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

Solution (Python):
import numpy as np

N = int(input())
A = [0] + list(map(int, input().split()))

cumsum = np.cumsum(A)

ans = float("inf")
for i in range(1, N):
    left = cumsum[i]
    right = cumsum[-1] - cumsum[i]
    tmp = abs(right - left)
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    left_sum = [0] * N 
    right_sum = [0] * N 

    left_sum[0] = A[0]
    right_sum[-1] = A[-1]

    for i in range(1, N):
        left_sum[i] = left_sum[i-1] + A[i]
        right_sum[(N-1)-i] = right_sum[(N-1)-(i-1)] + A[(N-1)-i]
    
    ans = 10 ** 18
    for i in range(N-1):
        diff = abs(left_sum[i] - right_sum[i+1])
        ans = min(ans, diff)
    
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    left_sum = [0] * N 
    right_sum = [0] * N 

    left_sum[0] = A[0]
    right_sum[-1] = A[-1]

    for i in range(1, N):
        left_sum[i] = left_sum[i-1] + A[i]
        right_sum[(N-1)-i] = right_sum[(N-1)-(i-1)] + A[(N-1)-i]
    
    ans = 10 ** 11
    for i in range(N-1):
        diff = abs(left_sum[i] - right_sum[i+1])
        ans = min(ans, diff)
    
    print(ans)
if __name__ == "__main__":
    main()

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

Solution (Python):
import math
num = int(input())
num_list = list(map(int,input().split()))
ans = sum(num_list)/2



total = 0
flag = "off"
for i in range(num):
  total += num_list[i]
  if total >= ans:
    
    if ans -(total - num_list[i]) < total - ans:
      ans = ans -(total - num_list[i])
    else:
      ans = total - ans     
    break


print(math.ceil(ans*2))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import itertools
import bisect

n = int(readline())
a = tuple(map(int, readline().split()))
cumsum = tuple(itertools.accumulate(a))
index = bisect.bisect(cumsum, sum(a) // 2)
ans = min(cumsum[n - 1] - cumsum[index - 1] * 2, cumsum[index] * 2 - cumsum[n - 1])
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split(" ")))
right = sum(A)
left = 0
ans = float('inf')

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

print(ans)

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

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

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

Solution (Python):
import numpy as np
import bisect
n = int(input())
A = list(map(int,input().split()))
cs = np.cumsum(A).tolist()
mid = sum(A)//2
idx = bisect.bisect_left(cs, mid)
ans = float('inf')
for i in range(max(1, idx-1), min(idx+2, n)):
    x = A[:i]
    y = A[i:]
    sum_x = sum(x)
    sum_y = sum(y)
    ans = min(ans, abs(sum_x - sum_y))
print(ans)

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

ca = [0]
for i in range(n):
    ca.append(A[i]+ca[i])

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

print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n = II()
    a = LI()
    acc = list(itertools.accumulate(a))
    ans = inf
    for i in range(n - 1):
        ans = min(ans, abs(acc[n - 1] - acc[i] - acc[i]))
    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
nsum = sum(A)
diff = 100000000000000
left = 0
right = sum(A)
for a in A:
  left += a
  right -= a
  diff = min(diff, abs(left-right))
print(diff)

Solution (Python):
def main():
    import sys
    import bisect
    from itertools import accumulate

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

    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect(b, l // 2)
    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    from itertools import accumulate

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

    b = list(accumulate(a))
    diff = b[-1]
    total = b[-1]
    for x in b:
        diff = min(diff, abs(total - 2 * x))
    print(diff)

if __name__ == '__main__':
     main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
wa = []
wa2 = []



s = 0
s1 = 0
for i in range(n):
    s +=a[i]
    wa.append(s)
    s1 +=a[-(i+1)]
    wa2.append(s1)

ans = 10**10
for i in range(n-1):
    if ans > abs(wa[i] - wa2[-(i+2)]):
        ans = abs(wa[i] - wa2[-(i+2)])
print(ans)

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
A=[float("inf")]*(N+10)
A[0]=0
for i in range(1,N+1):
	A[i]=A[i-1]+L[i-1]
X=float("inf")
for i in range(len(A)):
	X=min(X,abs(A[N]-A[i]-A[i]))
print(X)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
ans = 10000000000
cum_sum1 = 0
cum_sum2 = sum(A_list)
for i in range(N-1):
    cum_sum1 += A_list[i]
    cum_sum2 -= A_list[i]
    tmp = abs(cum_sum1-cum_sum2)
    if tmp < ans:
        ans = tmp
print(ans)

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

sum_l = sum(A)
sum_r = 0
half_abs = 2020202020
for a in A:
    sum_l -= a
    sum_r += a
    tmp_abs = abs(sum_l-sum_r)
    half_abs = min(half_abs, tmp_abs)
print(half_abs)

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

left = sum(a)
right = 0

mini = float('inf')
for i in a:
    right += i
    left -= i
    tmp = abs(right - left)

    if tmp < mini:
        mini = tmp

print(mini)

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

m = sum(a) // 2
s = sum(a)
temp = 0
for x in a:
    temp += x
    if temp > m:
        H = temp
        L = temp - x
        break

print(min(H * 2 - s, s - L * 2))

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
d = [0]

for i in range(n):
    d.append(d[i] + a[i])

ans = d[-1]

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = A[::-1]
S = [0] * n
S[0] = A[0]
T = [0] * n
T[0] = B[0]
ans = float('inf')
for i in range(1, n):
    S[i] = S[i - 1] + A[i]
    T[i] = T[i - 1] + B[i]
T = T[::-1]
ans = 2020202020 + 1
for i in range(n):
    diff = abs(S[i] - T[i] - A[i])
    ans = min(ans, diff)
print(ans)

Solution (Python):
import sys
from itertools import accumulate

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
A = LMI()

ans = INF
accum = list(accumulate(A))
for s in accum:
    t = accum[-1] - s
    ans = min(ans, abs(s - t))
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
su=0
mi=20000000000000000000
for i in range(n):
  su+=a[i]
  
c=0  
for i in range(n):
  c+=a[i]
  left=su-c
  mi=min(mi,max((c-left),(left-c)))

print(mi)

Solution (Python):


n = int(input().rstrip())

bar = list(map(int,input().rstrip().split(" ")))

cur = 0

total = sum(bar)
ans = total
for i in bar:
    cur += i
    ans = min(ans, abs((total-cur) - cur))
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=[A[0]] 
C=[0]*N 
for i in range(1,N):
  B.append(B[-1]+A[i])
  
for j in range(N):
  C[j]=abs((B[-1]-B[j])-B[j])


print(min(C))

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

total = sum(A)
tmp = 0
diff = total
for a in A:
    tmp += a
    diff = min(diff, abs(tmp - (total - tmp)))

print (diff)

Solution (Python):
from itertools import accumulate


def main():
    section_num = int(input())
    length = list(map(int, input().split()))
    accumulate_length = list(accumulate(length))
    answer = float("inf")
    for i in range(0, section_num):
        answer = min(answer, abs(accumulate_length[-1] - 2 * accumulate_length[i]))
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):



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


min_cost = 10**18
left_sum = 0
right_sum = sum(a_list)


for i in range(N-1):
    left_sum += a_list[i]
    right_sum -= a_list[i]
    distance = abs(left_sum - right_sum)
    min_cost = min(min_cost, distance)


print(min_cost)

Solution (Python):
import sys

input = sys.stdin.buffer.readline







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

	ans=10**18
	cnt=0

	Asum=sum(A)
	for i in range(n):
		cnt+=A[i]
		ans=min(ans,abs(Asum-2*cnt))
	print(ans)









if __name__ == "__main__":
	main()

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

Solution (Python):


n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
tmp = arr[0]
ans = 10**10
e = -1
for i in range(1, n):
    
    if tmp + arr[i] <= s // 2:
        tmp += arr[i]
        e = i
    else:
        break
c = abs(s // 2 - tmp) + abs(s // 2 - sum(arr[i:]))

ans = min(ans, c)

e = -1
tmp = arr[0]
for i in range(1, n):
    
    if tmp > s // 2:
        break
    else:
        tmp += arr[i]
        e = i
c = abs(s // 2 - tmp) + abs(s // 2 - sum(arr[i:]))

ans = min(ans, c)
print(ans)

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

Solution (Python):
n=int(input())
L=list(map(int,input().split()))
sumL=[0 for _ in range(n)]
S=sum(L)
for i in range(n):
    if i==0:
        sumL[i]=L[i]
    else:
        sumL[i]=sumL[i-1]+L[i]
ans=2020202020
tmp1=0
tmp2=0
for i in range(n):
    tmp1=sumL[i]
    tmp2=S-sumL[i]
    ans=min(ans,abs(tmp1-tmp2))
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
c=0
all=sum(A)
buf=all
A1=[]
A2=[]
s1=0
s2=0
c=float("inf")
for i in range(N-1):
    buf=buf-A[i]
    if c>abs((all-buf)-buf):
        c=abs((all-buf)-buf)
    else:
        break
print(c)

Solution (Python):
import math
from collections import deque
def main():
    n = int(input())
    a = [int(t)for t in input().split()]
    a_rev = a[::-1]

    cusum = [a[0]]
    rcusum = [a_rev[0]]

    for i in range(1,n):
        cusum.append(cusum[i-1]+a[i])
        rcusum.append(rcusum[i-1]+a_rev[i])
    
    rcusum = rcusum[::-1]

    result = []

    for i in range(1,n):
        result.append(abs(cusum[i-1]-rcusum[i]))

    print(min(result))

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = sum(A)/2
Es = []
tmp = 0
for i in range(N):
    tmp += A[i]
    Es.append(abs(tmp-S))
cut_point = Es.index(min(Es))

ans1 = abs(sum(A[cut_point:])-sum(A[:cut_point]))
ans2 = abs(sum(A[cut_point+1:])-sum(A[:cut_point+1]))
ans = min(ans1,ans2)

print(ans)