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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = 0
    sa = [0]
    
    for a in A:
        sa.append(sa[-1]+a)
    
    ans = INF
    for i in range(1, N):
        l = sa[i] - sa[0]
        r = sa[-1] - sa[i]
        ans = min(ans, abs(r-l))
    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

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

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

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

A_sum = 0
for Ai in A:
  A_sum += Ai

s1 = 0
i = 0
while s1 < A_sum / 2:
  s1 += A[i]
  i += 1

s2 = s1 - A[i - 1]
ans = min(abs(A_sum - s1 * 2), abs(A_sum - s2 * 2))
print(ans)

Solution (Python):
import sys; readline = sys.stdin.readline
from collections import deque

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

l = 0
Mm = 0

total = sum(A)

for a in A:
    nl = l + a
    m = min(nl, total - nl)
    if m <= Mm:
        break
    else:
        Mm = m
    l = nl

print(total - Mm * 2)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

s = sum(A)/2

t = 0
d = 10**10
for i in range(N):
    t += A[i]
    d = min(d, abs(s - t))

print(int(d*2))

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

m = 2020202020
b = sum(A)
a = 0
for i in range(N):
  a += A[i]
  b -= A[i]
  ab = abs(a-b)
  if ab < m:
    m = ab
print(m)

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

L=sum(A)
l=0
for i in range(n):
  l+=A[i]
  if l >= L/2:
    r=l-A[i]
    break
print(min(abs((L-r)-r),abs((L-l)-l)))

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

l = 0
r = N-1

l_sum = 0
r_sum = 0
while l <= r:
    if l_sum <= r_sum:
        l_sum += A[l]
        l += 1
    else:
        r_sum += A[r]
        r -= 1
        

ans = abs(l_sum - r_sum)
print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
sum1 = [a[0]]
sum2 = [sum(a)-a[0]]
s = sum(a)
ls = []
for i in range(1,n):
    sum1.append(sum1[i-1]+a[i])
    sum2.append(s - sum1[i])

for i in range(n-1):
    ls.append(abs(sum1[i] - sum2[i]))

print(min(ls))

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

sum_all = sum(A)
sum_tmp1 = 0
ans = float('inf')
for i in range(N):
  sum_tmp1 += A[i]
  sum_tmp2 = sum_all - sum_tmp1
  ans = min(ans,abs(sum_tmp1-sum_tmp2))
print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = [0]
a = 0
S = sum(j for j in A)
ans = [0, 0]
for i in range(n):
  a += A[i]
  if a >= S/2:
    ans[0] = abs(a-A[i]-(S-(a-A[i])))
    ans[1] = abs(a-(S-a))
    break
print(min(ans))

Solution (Python):
import bisect as bi

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

B=[0]
for Ai in A:
    B.append(B[-1]+Ai)


l=bi.bisect_left(B,s/2);r=bi.bisect_right(B,s/2)

ans=-1
if l<r:
    ans=0
else:
    B1=B[l-1];B2=B[l]
    d1=abs((s-B1)-B1);d2=abs((s-B2)-B2)
    ans=min(d1,d2)

print(ans)

Solution (Python):
def cumsum(inlist):
    s = 0
    outlist = []
    for i in inlist:
        s += i
        outlist.append(s)
    return outlist

n = int(input())
irons = [ int(v) for v in input().split() ]

irons_cum = cumsum(irons)

irons_sep = [ (irons_cum[-1] - irons_cum[i], irons_cum[i])  for i in range(n-1)]
irons_def = [ abs(a-b)  for a, b in irons_sep]
print(min(irons_def))

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

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

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

L = 0
R = sum(vA)

res = R
for a in vA:
    L += a
    R -= a
    res = min(res, abs(R-L))

print(res)

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    s = [0] * (N + 1)
    for i in range(0, N):
        s[i+1] = s[i] + A[i]
    ans = (1 << 63) - 1
    for j in range(1, N):
        left = s[j] - s[0]
        right = s[N] - s[j]
        ans = min(ans, abs(right - left))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
X=[0]*(N+1)
for i,a in enumerate(A):
    X[i+1]=X[i]+a
l,r=0,X[-1]
for x in X:
    if x>=X[-1]//2:
        r=x
        break
    l=x
print(min(X[-1]-2*l,2*r-X[-1]))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
A=list(map(int, input().split()))
n=sum(A)/2
m=sum(A)
for i in A:
    n-=i
    m=min(abs(n),m)
print(int(m*2))

Solution (Python):
import itertools

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

total = sum(a)



temp = 10**10

for i in range(n):
    l = s[i]
    r = total - s[i]

    temp = min(temp, abs(l-r))

print(temp)

Solution (Python):
from math import ceil
n=int(input())
a=[int(i) for i in input().split()]
s=sum(a)
th=ceil(s/2)
l=0
r=0
k=0
for i in range(n):
    k+=a[i]
    if k >= th:
        l=k-(s-k)
        break

k=0
for i in range(n-1, -1, -1):
    k+=a[i]
    if k >= th:
        r=k-(s-k)
        break

print(min(l,r))

Solution (Python):
import sys

input = sys.stdin.readline

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

asum = sum(A)
asumhalf = asum/2.0
foresum = 0
presum = 0
for ai in A:
	foresum += ai
	if foresum>=asumhalf: break
	presum = foresum

latsum = asum-foresum
difa = abs(foresum-latsum)
latsum = asum-presum
difb = abs(presum-latsum)
print(min(difa,difb))

Solution (Python):
from itertools import accumulate

N = int(input())
*A, = map(int, input().split())
acc_length = list(accumulate(A))
total_length = acc_length[-1]
ans = 10**10
for a in acc_length:
    ans = min(ans, abs(total_length - 2*a))
print(ans)

Solution (Python):
import bisect
from itertools import accumulate
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
A_acc = [0]+list(accumulate(A))
L = A_acc[-1]
if L/2 in A_acc:
    print(0)
    exit()
ind = bisect.bisect_left(A_acc, L/2)
left = A_acc[ind-1]
left_anti = L-left
right = A_acc[ind]
right_anti = L - right

l = max(left, left_anti)-min(left, left_anti)
r = max(right, right_anti)-min(right, right_anti)
print(min(l, r))

Solution (Python):
def examB(inf):
    N = I()
    A = LI()
    sumA = sum(A)
    cur = 0
    need = inf
    for i in range(N):
        need = min(need,abs(cur*2-sumA))
        cur += A[i]
    print(need)

def examC():
    return

def examD():
    return

def examE():
    return

import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB(inf)

Solution (Python):
def solve():
    n, *a_l = map(int, open(0).read().split())
    l, r = 0, sum(a_l)
    d = r - l
    for i in range(n):
        l += a_l[i]
        r -= a_l[i]
        if d > abs(l - r):
            d = abs(l - r)
    
    print(d)


if __name__ == '__main__':
    solve()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
tmp = 0
key = 0
SUMA = sum(a)

for i in range(n):
    if tmp + a[i] >= SUMA / 2:
        key = i
        break
    tmp += a[i]

ans = abs(sum(a[0:key]) - sum(a[key::]))
tmp = 0
key += 1
ans = min(abs(sum(a[0:key]) - sum(a[key::])), ans)
print(ans)

Solution (Python):

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

sum_a = sum(A)

min_diff = sum_a
sum_i = 0
for i in range(N):
    sum_i += A[i]
    diff = (sum_a - sum_i) - sum_i
    
    min_diff = min(min_diff, abs(diff))

print(min_diff)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=float('inf')
b=[a[0]]
for i in range(1,n):
    b.append(b[i-1]+a[i])

for i in range(n-1):
    c=abs(b[-1]-2*b[i])
    if c<=ans:
        ans=c
        
print(ans)

Solution (Python):
import itertools
import bisect
N = int(input())
A = [int(_) for _ in input().split()]
C = list(itertools.accumulate([0] + A))
i = bisect.bisect(C, C[-1] // 2)

x = 2 * C[i] - C[-1]

y = C[-1] - 2 * C[i - 1]
print(min(x, y))

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

L=[A[0]]
for i in range(1,N):
    L.append(L[-1]+A[i])
S=sum(A)

ans=10**15
for i in range(N):
    if abs(S-2*L[i])<ans:
        ans=abs(S-2*L[i])
print(ans)

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

i = 1
j = n-2
left = a[0]
right = a[-1]
while i <= j:
    if left >= right:
        right += a[j]
        j -= 1
    else:
        left += a[i]
        i += 1

print(abs(left-right))

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

sa=float("inf")


wa1=0
wa2=sum(a)

for i in range(n-1):
  wa1+=a[i]
  wa2-=a[i]
  temp=abs(wa1-wa2)
  sa=min(sa,temp)
print(sa)

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

s = sum(a)
cur = 0
m = float('inf')
for i in range(n):
	cur+=a[i]
	diff = abs(cur-(s-cur))
	m = min(m,diff)
print(m)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    A=list(map(int,input().split()))
    suma=sum(A)
    tmpa=0
    halfa=suma/2
    b=INF
    for i in A:
        tmpa+=i
        b=min(abs(halfa-tmpa),b)
    print(int(b*2))
        

if __name__ == '__main__':
    main()

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

l = 0
r = 0
l_num = 0
r_num = -1
while n > 1:
    if l <= r:
        l += a[l_num]
        l_num += 1
    else:
        r += a[r_num]
        r_num -= 1
    n -= 1

ans = min(abs(r - (l + a[l_num])), abs(l - (r + a[r_num])))

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
l = A[0]
r = sum(A[1:])
res = abs(l-r)
for i in range(1,N):
    l += A[i]
    r -= A[i]
    res = min(res, abs(l - r))

print(res)

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

total = sum(a)

pre = 0
for i in range(n):
    if pre + a[i] >= total // 2:
        x = abs((total - pre * 2))
        y = abs((total - (pre + a[i])*2))
        print(min(x, y))
        break
    pre += a[i]

Solution (Python):
import sys
from itertools import accumulate
from bisect import bisect_left
read = sys.stdin.read

N, *A = map(int, read().split())
A = list(accumulate([0] + A))
a = A[-1] // 2
n = bisect_left(A, a) - 1

print(min(abs(2 * A[n] - A[-1]), abs(2 * A[n + 1] - A[-1])))

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))






def main():
    n = ii()
    a = li()
    s = sum(a)

    stmp = 0
    point = 0
    for i in range(len(a)):
        stmp += a[i]
        if stmp*2 >= s:
            point = i
            break
    
    stmptmp = stmp-a[i]

    ans=0
    
    ans = min(abs(stmptmp*2-s),abs(stmp*2-s))
    print(ans)


    





main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
for i in range(1,n):
    a[i]+=a[i-1]
i=0
while a[i]<=a[n-1]//2:
    i+=1
left=a[i]
right=a[n-1]-a[i]
ans=abs(left-right)
left=a[i-1]
right=a[n-1]-a[i-1]
ans=min(ans,abs(left-right))
print(ans)

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

N = int(input())
A = list(map(int, input().split()))
B = deque(A)
left = 0
left += B.popleft()
right = 0
while B != deque():
    if right < left:
        right += B.pop()
    else:
        left += B.popleft()
print(abs(left - right))

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


total = sum(A)

half = total / 2

v1 = 0
v2 = total
min_d = None

for i in range(N):
    v1 += A[i]
    v2 = total - v1

    d = abs(v2 - v1)

    if min_d is None:
        min_d = d
    elif min_d > d:
        min_d = d

print(min_d)

Solution (Python):
def main():

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

    ans = float('inf')
    l, r = 0, tot
    for i in range(N):
        l += A[i]
        r -= A[i]
        ans = min(ans, abs(l-r))
    return ans

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

Solution (Python):
n=int(input())
alist = list(int(i) for i in input().split())

gokei=sum(alist)
now=0

for i in range(n):
    now+=alist[i]
    if now>=gokei//2:
        break
    mae=now



ans=min(gokei-mae*2,now*2-gokei)
print(ans)

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

length = sum(a)

x = 0
for i in range(n):
  if x+a[i] > length /2:
    y = length - (x + a[i])
    break
  x += a[i]

z = min(y+a[i]-x,x+a[i]-y)
print(z)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
wa=[a[0]]
for i in range(1,n):
    wa.append(a[i]+wa[-1])
s=sum(a)

ans=float("inf")
for i in range(n):
    ans=min(ans,abs(s-wa[i]*2))
print(ans)

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

sum_start = 0
sum_end = 0
index_start = 0
index_end = n - 1

while True:
    if index_start > index_end:
        break
    if sum_start < sum_end:
        sum_start += a[index_start]
        index_start += 1
    else:
        sum_end += a[index_end]
        index_end -= 1

print(abs(sum_start - sum_end))

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n = ri()
    la = rli()
    s = sum(la)
    half = s/2
    l = 0
    m = s
    for a in la:
        l += a
        m = min(m, abs(l-half))
    print(int(m*2))



if __name__ == '__main__':
    main()

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

m = sum(A)
plc = 0
l = sum(A)
S = 0

for i in range(N):
    S += A[i]
    if abs(S-l/2) < m:
        m = abs(S-l/2)
        plc = i

print(abs(sum(A[:(plc+1)])-sum(A[(plc+1):])))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
cnt=0
i=0
while(cnt<suma/2):
    cnt+=a[i]
    i+=1
cnt2=cnt-a[i-1]
half=float(suma)/2

nerai=min(abs(suma-cnt-half),abs(cnt-half),abs(cnt2-half),abs(suma-cnt2-half))
print(int(nerai*2))

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

suma = 0
for i in a:
  suma += i

diff = float('INF')
part = 0
for i in a:
  part += i
  if abs(part * 2 - suma) < diff:
    diff = abs(part * 2 - suma)

print (diff)

Solution (Python):


import sys
import math
import collections


n=int(input())

tmp = input().split()
a = list(map(lambda a: int(a), tmp))

total=0
for i in range(0,n):
	total+=a[i]
tmp=0
for i in range(0,n):
	tmp+=a[i]*2
	if(tmp>total):
		if(tmp-total<total-tmp+a[i]*2):
			print(tmp-total)
		else:
			print(total-tmp+a[i]*2)
		sys.exit()

Solution (Python):
def cumsum(s):
    n = len(s)
    cs = [0] * (n+1)
    for i in range(n):
        cs[i+1] = cs[i] + s[i]
    return cs

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

cs = cumsum(a)
ans = float("inf")
for i in range(n+1):
    ans = min(ans, abs(cs[i] - (cs[n] - cs[i])))
print(ans)

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

ans = float('inf')
left = 0
right = 0
allA = sum(A)


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

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

S = sum(a_list)
from itertools import accumulate

acc = list(accumulate(a_list))

ans = float("inf")
for i in range(N):
    ans = min(ans, abs((S - acc[i]) - acc[i]))

print(ans)

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

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

s = sum(bar)
ans = s

left = 0
right = s
for i in range(n):
    left += bar[i]
    right -= bar[i]
    ans = min(ans, abs(right - left))
print(ans)

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

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

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

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

lin = list(accumulate(li)) 

ma = sum(li)
sa = 10**10
for i in range(n):
    hidari = lin[i]
    migi = ma-hidari
    sa = min(sa,abs(hidari-migi))

print(sa)

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

asum = sum(a)
num = 0
ai = 0
for i in range(n):
    num += a[i]
    ai = i
    if num * 2 >= asum:
        break

f1 = asum - num
b1 = num

num -= a[ai]
f2 = asum - num
b2 = num

print(min(abs(f1 - b1), abs(b2 - f2)))

Solution (Python):
N = int(input())
A = [int(x) for x in input().split(' ')]
all_s = 0
for x in A:
    all_s += x

half_size = all_s / 2.

acc = 0
for x in A:
    prev = acc
    acc += x
    if acc == half_size:
        print(0)
        break
    if acc > half_size:
        s1 = all_s - 2 * prev
        s2 = 2 * acc - all_s
        print(min(s1,s2))
        break

Solution (Python):
import queue
import math
import copy
"""
N = int(input())



A = []
for i in range(N):
	A.append(int(input()))

print(A)

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

b = 0
c = 0
m = 2020202021
for a in A:
	if b <= T:
		if m > (T-b)//0.5:
			m = (T-b)//0.5
	else:
		if m > (b-T)//0.5:
			m = (b-T)//0.5
	b += a
if b <= T:
	if m > (T-b)//0.5:
		m = (T-b)//0.5
else:
	if m > (b-T)//0.5:
		m = (b-T)//0.5
print(int(m))

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

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

aa = [0]*(n+1)

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

sm = sum(a)
ans = sm
for i in range(n):
    ans = min(ans,abs(sm-2*aa[i]))

print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    s = [0]
    for i in a:
        s.append(s[-1]+i)
    ans = float("inf")
    for i in range(n):
        k = abs(s[-1]-s[i]-s[i])
        ans = min(ans,k)
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import sys
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()

num = sum(a)
ans = MAX_INT
l = 0
for i in a:
  l += i
  ans = min(ans, abs(l - (num - l)))
print(ans)

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

L = sum(As)
L2 = L * 2
acc = 0
minD = float('inf')
for a in As:
    acc += a * 2
    d = abs(acc - L)
    minD = minD if minD < d else d
print(minD)

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

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

left = 0
right = sum(A)
ans = 202020202020 * 200000

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

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
A=sum(a_list)
ha=0
for i in a_list:
    ha+=i
    if ha>=A/2:
        hb=ha-i
        break
ans=min(ha+ha-A,A-2*hb)
print(ans)

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

print(min(ans))

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

S = sum(A)

Sb = 0
for i,a in enumerate(A):
    Sb += a
    if Sb>=S/2:
        break

ans = abs(S-Sb*2)
Sb -= A[i]
ans = min(ans,abs(S-Sb*2))
print(ans)

Solution (Python):
from itertools import accumulate
n = int(input())
l = list(map(int, input().split()))
suf = list(accumulate(l[::-1]))[::-1] + [0]
res = min(abs(p - suf[i + 1]) for i, p in enumerate(accumulate(l)))
print(res)

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

left = 0
right = sum(A)
for i in range(N):
    left_new = left + A[i]
    right_new = right - A[i]
    if left_new == right_new:
        print(0)
        exit()
    elif left_new > right_new:
        print(min(left_new - right_new, right - left))
        exit()
    left, right = left_new, right_new

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

left = A[0]
right = sum(A)-A[0]

s = abs(left-right)

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

print(s)

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

left = 0
right = sum(A)

ans = float('inf')

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

print(ans)

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    total = sum(A)
    tmp = 0
    ans = float('inf')

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

if __name__ == "__main__":
    main()

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

l = [0]*(N-1)
r = [0]*(N-1)

l[0] = A[0]
r[N-2] = A[-1]

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

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

ans = float('inf')
for i, j in zip(l,r):
    ans = min(ans, abs(i-j))
print(ans)

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

N = int(input())
A = [int(x) for x in input().strip().split()]
length = sum(A)
l = 0
r = length
half = length
for a in A:
    l += a
    r -= a
    half = min(half, abs(r - l))

print(half)

Solution (Python):
import numpy as np
N = int(input())
A = list(map(int,input().split()))
num = float("inf")
B = np.cumsum(A)
sA = sum(A)
for i in range(N):
    num = min(num, abs(sA-2*B[i]))
print(num)

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

a=[A[0]]
for x in A[1:]:
    a.append(a[-1]+x)
sumA=sum(A)
b=[sumA-x for x in a]
print(min([abs(x-y) for x,y in zip(a,b)]))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
a = 0
ans = float('inf')
for ai in A:
    a += ai
    ans = min(abs(sumA-a-a), ans)
print(ans)

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 ** 10
    for i in range(n-1):
        t += a[i]
        sa -= a[i]
        mt = min(mt, abs(t - sa))
    print(mt)


if __name__ == '__main__':
    solve()

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

Solution (Python):
n=int(input())
a=[int(j) for j in input().split()]
from itertools import accumulate
l=[0]+list(accumulate(a))
ans=10**18
for i in range(n+1):
    ans=min(ans,abs(l[i]-(l[-1]-l[i])))
print(ans)

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

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    S = string_to_int(inputs[0])
    s_sum = deque([0])
    for s in S:
        s_sum.append(s_sum[-1] + s)
    s_sum.popleft()

    min_diff = None
    for i in range(len(s_sum)):
        front = s_sum[i]
        back = s_sum[-1] - front
        d = abs(front - back)
        if min_diff is None or min_diff > d:
            min_diff = d
    return min_diff


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

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

Solution (Python):
import math
N = int(input())
A = list(map(int, input().split()))
L = sum(A)
S = 0
for i in range(N):
    S+=A[i]
    if S>=math.ceil(L/2):
        break
ans = min(S*2-L, L-(S-A[i])*2)
print(ans)

Solution (Python):
n=int(input())
A=[int(i) for i in input().split()]
B=[0]
for a in A:
  B.append(B[-1]+a)
m=B[-1]//2
import bisect
p=bisect.bisect_left(B,m)
l,r=B[p-1],B[p]
ans=min(abs(B[-1]-2*l),abs(B[-1]-2*r))
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy


from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])
    
def main():
    n=inp()
    a=inpl()
    S=sum(a)
    pre = 0
    for i in range(n):
        pre+=a[i]
        if pre == S/2:
            print(0)
            return
        if pre > S/2:
            x=pre
            y=pre-a[i]
            break
    x = abs(S-2*x)
    y= abs(S-2*y)
    print(min(x,y))
if __name__ == "__main__":
    main()

Solution (Python):

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

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

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

min_cost = 1000000000000000000000
for i in range(N + 1):
	min_cost = min(min_cost, abs(sum_A[-1] - sum_A[i] - sum_A[i]))

print(min_cost)

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

S = sum(A)
s = 0
for i in range(N):
    s += A[i]
    if s > S//2:
        p = i
        break
print(min(abs(s-(S-s)),abs(s-A[i]-(S-(s-A[i])))))

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
cs = [0] * N
cs[0] = a[0]
res = float("inf")
for i in range(N - 1):
  cs[i + 1] = cs[i] + a[i + 1]
for i in range(N - 1):
  
  res = min(abs((cs[-1] - cs[i]) - cs[i]), res)
print(res)

Solution (Python):
import math
n = input()
nums =list( map(int, input().split(" ")))
total = sum(nums)
part = 0
ans = 10 ** 20
for num in nums:
    part += num
    total -= num
    ans = min(ans,abs(total - part))
print(ans)

Solution (Python):
import sys
import math

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

m=sum(a)
tmp=0
ans=m
for i in range(N-1):
    tmp+=a[i]
    ans=min(ans,abs(m-tmp*2))

print(ans)

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

d = 10**20
l = 0
r = sum(A)
for i in range(N):
    l += A[i]
    r -= A[i]
    d = min(d, abs(l-r))
    
print(d)

Solution (Python):
import bisect
n = int(input())
a = list(map(int, input().split( )))
s_a = []
sm = 0
for i in range(n):
    sm+= a[i]
    s_a.append(sm)
    
al = s_a[-1]
half = al//2

i = bisect.bisect_left(s_a,half)
ans1 = s_a[i-1]
ans2 = s_a[i]


print(min(al-2*ans1,2*ans2-al))