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 sys
input=sys.stdin.readline
import numpy as np

def main():
    N=[]
    n = int(input())
    N=np.array(list(map(int,input().split())))
    Ncum=N.cumsum()
    L=Ncum[:-1]
    R=Ncum[-1]-L
    ans=np.abs(L-R).min()
    print(ans)

if __name__=="__main__":
    main()

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

Solution (Python):


def main():
    _ = int(input())
    A = list(map(int, input().split()))
    ans = 0
    tmp = 0
    total = sum(A)
    for a in A:
        tmp += a
        if tmp >= total // 2:
            ans = min(2 * tmp - total, total - 2 * (tmp - a))
            break

    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
y = sum(A)
x = y * 1.0 / 2
s = 0
for i in range(N):
  s += A[i]
  if s >= x:
    break
print(min(abs(s - (y - s)), abs((s-A[i]) - (y - (s - A[i])))))

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
L = [0 for _ in range(N)]
R = [0 for _ in range(N)]
L[0] = nums[0]
for i in range(N-1):
  L[i+1] = L[i] + nums[i+1]
  R[N-i-2] = R[N-i-1] + nums[N-i-1]
ans = 10**20
for i in range(N):
  ans = min(ans, abs(R[i]-L[i]))
print(ans)

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

Solution (Python):
import math

def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    S = sum(A)
    l = A[0]
    I = 0
    m = math.ceil(S / 2)
    for i in range(1, N):
        if (l + A[i]) <= m:
            l += A[i]
        else:
            I = i - 1
            break
    if l == S / 2:
        print(0)
        exit()
    c = A[I + 1]
    r = S - l - c
    ans1 = abs(r - (l + c))
    ans2 = abs(l - (r + c))
    print(min(ans1, ans2))

if __name__ == "__main__":
    solve()

Solution (Python):
N ,*A  = map(int,open(0).read().split())

L = [0 for i in range(N+1)]
D = [0 for i in range(N)]

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

  


ans = 1e19
for i in range(N):
  
  ans = min(abs(L[i]-D[i]),ans)
print(ans)

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

cnt = 0
i = 0
while cnt < s/2:
    cnt += al[i]
    i += 1

s1 = sum(al[0:i])-sum(al[i:])
s2 = sum(al[0:i-1]) - sum(al[i-1:])

if s1 < 0:
    s1 = -s1
if s2 < 0:
    s2 = -s2
print(min(s1, s2))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    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,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7

    n=I()
    lis=LI()
    sm=list(accumulate(lis))
    u=sum(lis)
    ans=inf
    for i in range(n):
        ans=min(ans,abs(u-sm[i]-sm[i]))
    print(ans)
        
        



        
        
        
        
        
    
if __name__=="__main__":
    main()

Solution (Python):
import sys



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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    A_sum = sum(A)
    cusum = [0] * (n + 1)
    ans = float("inf")
    for i in range(1, n + 1):
        cusum[i] = cusum[i - 1] + A[i - 1]
        ans = min(ans, abs(cusum[i] - (A_sum - cusum[i])))
    print(ans)

    return


if __name__ == "__main__":
    main()

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

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

s = sum(a)
half = s / 2

cum = 0
for v in a:
    if abs(half - cum) < abs(half - (cum + v)):
        break
    cum += v
print(abs((s - cum) - cum))

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

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

N=int(input())
A=list(map(int,input().split()))
total = sum(A)
total *= 2
mindif = 1e12
sub = 0
for i in range(N):
    sub += 2 * A[i]
    dif = abs(total//2-sub)
    if dif < mindif:
        mindif = dif

print(mindif)

Solution (Python):





def resolve():
    import sys
    input = sys.stdin.readline

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

    a_list = [int(x) for x in input().rstrip().split(" ")]
    length = sum(a_list)
    center = length * 0.5

    
    center_cut = 0
    cut = 0
    for a in a_list:
        cut += a
        if abs(center_cut - center) > abs(cut - center):
            center_cut = cut
        if cut > center:
            break

    
    print(int(abs(center_cut - center) * 2))


if __name__ == "__main__":
    resolve()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l=0
r=sum(a)
ans=20202020200

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

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

mini = float('inf')

cumsum = list(itertools.accumulate(A))
A.reverse()
rev = list(itertools.accumulate(A))
rev.reverse()
cumsum.pop()
rev.pop(0)

for i, j in zip(cumsum,rev):
    if abs(i-j) < mini:
        mini = abs(i-j)

print(mini)

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

sumA = sum(A)

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

print(min([abs(cum[i]-(sumA-cum[i])) for i in range(1,N+1)]))

Solution (Python):

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


length = sum(A)
left = 0
dist = 0
mdist = length
midx = 0
for i,a in enumerate(A):
    left += a
    right = length - left
    dist = abs(left - right)
    if dist < mdist:
        mdist = dist
        midx = i
ans = mdist
print(ans)

Solution (Python):
from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
ac = list(accumulate(a))
total = ac[-1]
ans = float('inf')
for i in range(n-1):
    l, r = ac[i], total - ac[i]
    ans = min(ans, abs(l-r))
print(ans)

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

a_sum = sum(a)
x = 0
y = 0
for i in range(n):
    x += a[i]

    if x * 2 >= a_sum:
        y = a[i]
        break

ans = min(abs(2 * x - a_sum), abs(a_sum - 2 * (x - y)))
print(ans)

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

from itertools import accumulate
cum=list(accumulate(l))

tot=sum(l)
ans=2020202020*100000
for i in cum:
    ans=min(ans, abs(tot-i*2))
print(ans)

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

from itertools import accumulate

tot=sum(l)
ans=2020202020*100000
for i in list(accumulate(l)):
    ans=min(ans, abs(tot-(i*2)))

for i in list(accumulate(l[::-1])):
    ans=min(ans, abs(tot--(i*2)))

print(ans)

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

from itertools import accumulate

tot=sum(l)
ans=2020202020*100000
for i in list(accumulate(l)):
    if i <= tot//2:
        ans=min(ans, abs(tot-(i*2)))

for i in list(accumulate(l[::-1])):
    if i <= tot//2:
        ans=min(ans, abs(tot-(i*2)))

print(ans)

Solution (Python):
from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
cumsum = list(accumulate(a))
total = cumsum[-1]

ans = 10**20
for left in cumsum:
    right = total - left
    ans = min(ans, abs(left - right))

print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from itertools import accumulate

    N = int(readline())
    A = list(map(int, readline().split()))
    acc = list(accumulate(A))
    S = sum(A)

    ans = INF
    for first in acc:
        second = S - first
        ans = min(ans, abs(first - second))

    print(ans)


if __name__ == '__main__':
    main()

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

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

av = s / 2
x = 0
y = 0

for i in range(N):
  x += A[i]
  
  if x < av < x +A[i+1]:
    
    y = s - x - A[i+1]
    
    if x > y:
      print(A[i+1]-x+y)
      
    else:
      print(A[i+1]-y+x)
  elif x == av:
    print(0)

Solution (Python):
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    total_length = sum(a_list)
    ans = total_length
    accum_length = 0

    for i in range(n):
        accum_length += a_list[i]
        ans = min(ans, abs(2 * accum_length - total_length))

    print(ans)


if __name__ == "__main__":
    main()

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

l = sum(a)
s = cumsum(a)
c = l*2
for i, x in enumerate(a):
    d = abs(s[i] * 2 - l)
    c = min(d, c)

print(c)

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

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

pL= 0
pR = sumA
cost = abs(pR - pL)
for i in range(N):
    pL += A[i]
    pR -= A[i]
    cost = min(cost, abs(pR - pL))
print(cost)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
first_half = 0
latter_half = sum(a)
min_diff = latter_half
for i in range(n):
    first_half += a[i]
    latter_half -= a[i]
    diff = abs(first_half - latter_half)
    min_diff = min(min_diff, diff)
print(min_diff)

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

sum1 = 0
count = 0
total_length = sum(a)

for i in range(len(a)):
    sum1 += a[i]
    if sum1 >= total_length / 2:
        count1 = abs(sum1 - sum(a[i + 1:]))
        count2 = abs(sum1 - a[i] - sum(a[i:]))
        if count1 <= count2:
            count = count1
        else:
            count = count2
        break
print(count)

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

l = sum(a)
half = 0
tmp=a[0]
while tmp<l/2:
    half+=1
    tmp+=a[half]
    
    
print(int(min(tmp-l/2,abs(tmp-a[half]-l/2))*2))

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
suml=sum(L)
l,ll,i=L[0],0,0
while l<suml/2:
  i+=1
  ll=l
  l+=L[i]

key=min(l-suml/2,suml/2-ll)
print(int(key*2))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=20202020200
sum_A=0
sum_B=sum(A)
for i in range(N):
    sum_A+=A[i]
    sum_B-=A[i]
    tmp=abs(sum_A-sum_B)
    if tmp<ans:
        ans=tmp
print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
l = 0
r = N - 1
L = A[l]
R = A[r]
while r-l!=1:
  if L>=R:
    R += A[r-1]
    r -= 1
  else:
    L += A[l+1]
    l += 1
print(abs(L-R))

Solution (Python):
import sys, math, re
from functools import lru_cache
from collections import defaultdict
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N = ii()
    A = list(mi())
    s = sum(A)
    t = 0
    m = math.inf
    for i in range(N):
        t += A[i]
        m = min(m, abs(s-2*t))

    print(m)

if __name__ == '__main__':
    main()

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

a = s[0]
b = sum(s)-a
ans = abs(a-b)

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

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

mm = 10**18
s = sum(a)
l, r = 0, 0

for i in range(n):
    l += a[i]
    r = s - l
    mm = min(mm, abs(l-r))

print(mm)

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

half = sum(a)/2
l, r, i = 0, a[0], 0

while r < half:
    i += 1
    l = r
    r += a[i]

print(int(min(half - l, r - half)*2 ))

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

MMM = map(int,input().split())
MM =list(MMM)
mini = 2020202020
list1 = []
total = 0
for i in MM:
  total+=i
  list1.append(total)
for i in list1:
  x = abs(total - 2*i)
  if mini > x:
    mini = x
print(mini)

Solution (Python):
n = (int)(input())
a = list(map(int, input().split(" ")))
total = sum(a)
temp = 0
for i in range(n):
  
  temp += a[i]
  if temp >= total - temp:
    ko1 = abs(temp - (total - temp))
    temp -= a[i]
    ko2 = total - 2 * temp
    print(min(ko1, ko2))
    break

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

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


import numpy as np

def main():
	n=II()
	A=LI()

	A=np.array(A)

	B=np.cumsum(A)
	C=np.cumsum(A[::-1])

	

	D=abs(B[:-1]-C[:-1][::-1])
	

	print(np.min(D))









if __name__ == "__main__":
	main()

Solution (Python):
from itertools import accumulate


N, *A = map(int, open(0).read().split())
A_acc = list(accumulate(A, initial=0))
min_diff = float("inf")
for i in range(1, N):
    left, right = A_acc[i], A_acc[N] - A_acc[i]
    min_diff = min(min_diff, abs(right - left))
print(min_diff)

Solution (Python):
from itertools import accumulate

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

count = 0
ironbar = list(accumulate(A))

min_value = ironbar[-1]
for i in range(N):
    bet = abs(ironbar[i] - (ironbar[-1] - ironbar[i]))

    if min_value > bet:
        min_value = bet
    else:
        print(min_value)
        exit()

Solution (Python):
from itertools import accumulate
n = int(input())
a = list(map(int,input().split()))
b = list(accumulate(a))
a_sum = sum(a)
dn = float("inf")


for i in range(n-1):
    dn = min(abs(a_sum - 2*b[i]) ,dn)
print(dn)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
total = sum(A)
mi = float('inf')
left = 0
for i in range(n):
    left += A[i]
    total -=A[i]
    mi = min(mi, abs(left - total))
print(mi)

Solution (Python):
import bisect
from itertools import accumulate
N = int(input())
A = list(map(int, input().split()))
a = 0
Asum = list(accumulate(A))
x = bisect.bisect_right(Asum, Asum[N-1]/2)
total = Asum[N-1]
if x >= N:
    a = Asum[x-1]
    b = total-Asum[x-1]
    ans = abs(a-b)
if x == 0:
    a = Asum[x]
    b = total-Asum[x]
    ans = abs(a-b)
else:
    a = Asum[x-1]
    b = total-Asum[x-1]
    ans = abs(a-b)
    a = Asum[x]
    b = total-Asum[x]
    ans = min(ans, abs(a-b))
print(ans)

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

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

Solution (Python):
import numpy as np
n, *a = map(int, open(0).read().split())
s = np.cumsum(np.array(a))
print(min([abs(s[n - 1] - s[i] - s[i]) for i in range(n - 1)]))

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

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

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

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

ans = [0, 0]
ans_abs = 10**200

A_sum = sum(A)
A_1 = 0
A_2 = A_sum

for i in range(N - 1):
    A_1 += A[i]
    A_2 -= A[i]
    ans_abs = min(ans_abs, abs(A_1 - A_2))

print(ans_abs)

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

ans = [0, 0]
ans_abs = 10**200

A_sum = sum(A)
A_1 = 0
A_2 = A_sum

for i in range(N - 1):
    A_1 += A[i]
    A_2 -= A[i]
    ans_abs = min(ans_abs, abs(A_1 - A_2))

print(ans_abs)

Solution (Python):

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

left = 0
right = sum(lis)
ans = 1001001001001

for i in range(n):
    left += lis[i]
    right -= lis[i]
    ans = min(ans, abs(left-right))

print(ans)

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



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

ans = INF
for i in range(1, N):
    left = acc[i]
    right = acc[-1] - acc[i]
    ans = min(ans, abs(left - right))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sumlow = 0
sumhigh = 0
i = 0
j = N-1
while i<=j:
    if sumlow <= sumhigh:
        sumlow += A[i]
        i += 1
    else:
        sumhigh += A[j]
        j -= 1
print(abs(sumlow-sumhigh))

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

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

tmp_x = -1
tmp_x_diff = -1
for i in range(N - 1):
    sum1 = cumsum[i + 1]
    sum2 = cumsum[-1] - cumsum[i + 1]
    diff = abs(sum2 - sum1)
    if tmp_x_diff == -1 or diff < tmp_x_diff:
        tmp_x = i
        tmp_x_diff = diff
print(tmp_x_diff)

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

    mid = 0
    diff = float("inf")
    total = sum(A)
    p = 0
    
    for i in range(n):
        
        if abs(total-p) < diff:
            diff = abs(total - p)
            mid = i
        p += A[i]
        total -= A[i]
    front = A[:mid] 
    back =  A[mid:]
    print(abs(sum(front)-sum(back)))
resolve()

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

Solution (Python):
import itertools
n=int(input())
a=list(map(int,input().split()))
cumsum=[0]+list(itertools.accumulate(a))
sum_num=sum(a)
ans=10**100
for i in range(1,n):
    yen=abs(cumsum[i]-(sum_num-cumsum[i]))
    if yen<ans:
        ans=yen
print(ans)

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

SUM=sum(a)
for i in range(n):
  diff[i]=abs(s[i]-(SUM-s[i]))
  

print(min(diff))

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

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

import numpy as np

c=np.cumsum(a)
s=sum(a)

ans=float('inf')
index=0
for i in range(n):
    if abs(2*c[i]-s)<ans:
        ans=abs(2*c[i] - s)
        index = i

print(ans)

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

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

    ans = 10**10
    for i in range(1, N):
        l = s[i] - s[0]
        r = s[-1] - s[i]
        ans = min(ans, abs(l - r))

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin


n = int(stdin.readline().strip())
a_lst = [int(x) for x in stdin.readline().strip().split()]

pos = len(a_lst) // 2
min_diff = 10000000000000000000

while(True):
  left = sum(a_lst[:pos])
  right = sum(a_lst[pos:])
  diff = right - left
  
  if min_diff > abs(diff): min_diff = abs(diff)
  else: break
  
  if diff > 0: pos += 1
  elif diff < 0: pos -= 1
    
print(min_diff)

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

sumA = sum(A)
half = sumA / 2

r = 0.0

t1 = 0.0
t2 = 0.0
for a in A:
    t2 = t1
    t1 += a

    if t1 >= half:
        r = min(abs(half - t1), abs(half - t2))
        break

print(int(r * 2))

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

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

sum_A = sum(A)
sum_now = 0
cost = 99999999999
for a in A:
  sum_now += a
  cost = min(cost, abs(sum_now - (sum_A - sum_now)))
print(cost)

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

diff = 10**10
sum1 = sum(L)
l_sum = 0
ans = 10**10

for i in range(n-1):
    l_sum+=L[i]
    diff = abs(l_sum- sum1 + l_sum)
    ans = min(ans,diff)
    
print(ans)

Solution (Python):
import sys
import collections
import bisect


def main():
    n = int(input())
    AList = list(map(int, input().split()))
    ASum = sum(AList)
    c = 0
    for i in range(n):
        c += AList[i]
        if c >= ASum / 2:
            k = i
            break
    ans = min(2 * c - ASum, ASum - 2 * c + 2 * AList[k])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
ans=float('INF')
sa=[0]
for i,a in enumerate(A):
    sa.append(sa[i]+a)
for i in range(n+1):
    ans=min(ans,abs(sa[i]-(sa[-1]-sa[i])))
print(ans)

Solution (Python):
n = int(input())
sa = [0]
ans = float("INF")
for i, ai in enumerate(list(map(int, input().split()))):
    sa.append(sa[i] + ai)
for i in range(n + 1):
    ans = min(ans, abs(sa[i] - (sa[-1] - sa[i])))
print(ans)

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

slist=[0]
for a in alist:
  slist.append(slist[-1]+a)  


answer=float("inf")
for i in range(N+1):
  s1=slist[i]
  s2=slist[-1]-s1
  answer=min(answer,abs(s1-s2))
  
print(answer)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = sum(a)
c = 0
for i in range(n):
  c += a[i]
  if c >= b / 2:
    k = i
    break
cut1 = c - (b - c)
cut2 = b - c + a[k] - (c - a[k])
ans = min(cut1, cut2)
print(ans)

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

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

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

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


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

ans=aA_n[-1]
for i in range (N-1):
    _tempans=abs(aA_n[i]-aA_r[i+1])
    if _tempans<ans:
        ans=_tempans

print(ans)

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

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

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

ans = 10**11
sum_A = sum(A)
l = 0

for a in A:
    l += a
    if abs(l - (sum_A - l)) < ans:
        ans = abs(l - (sum_A - l))

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
t = [0]
for i in range(n):
    t.append(t[i] + a[i])
t.pop(0)
p = t[-1] // 2
if p in t:
    print(0)
else:
    for i in range(n):
        if p < t[i]:
            print(min(abs(sum(a[0:i+1]) - sum(a[i+1:n])),abs(sum(a[0:i]) - sum(a[i:n]))))
            break

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

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

l = 0
r = sum(a)
ans = 10**10

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

print(ans)

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

total = sum(a)
ans = float("inf")
t = 0
for i in a:
  t += i
  total -= i
  ans = min(ans, abs(total-t))
print(ans)

Solution (Python):
from itertools import accumulate
import bisect

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

acc = list(accumulate(a))



result = []
for i in range(n):
    result.append(abs((acc[-1] - acc[i] ) - acc[i]))
print(min(result))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Sum = 0
Temp = 0
for a in A:
    Sum += a
for i in range(N):
    Temp += A[i]
    if Temp > Sum//2:
        index = i
        break

print(min(abs(Sum-2*Temp), abs(Sum-2*(Temp-A[index]))))

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

for i in range(n):
    b += a[i]
    tmp = abs(2*b-t)
    ans = tmp if i==0 else min(ans, tmp)

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
tot=sum(a)
b=0
c=10**10
d=0
for i in range(n):
  b+=a[i]
  d=abs(tot/2 - b)
  if d<c:
    c=d
print(int(2*c))

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

Solution (Python):
import bisect


n = int(input())
la = [int(w) for w in input().split()]
la_sum = [0]*(n)
ans = 10**10

la_sum[0] = la[0]
for i in range(1, n):
    la_sum[i] = la_sum[i-1] + la[i]


for i in range(0, n):
    l = la_sum[i]
    r = la_sum[-1]-la_sum[i]
    d = abs(l-r)
    ans = min(ans, d)
print(ans)

Solution (Python):

n, *a = map(int, open(0).read().split())
r = [a[0]]
for i in range(1, n):
    r.append(r[i - 1] + a[i])
print(min(abs(2 * i - r[-1]) for i in r))

Solution (Python):

n, *a = map(int, open(0).read().split())
s = sum(a)
r = [a[0]]
for i in range(1, n):
    r.append(r[i - 1] + a[i])
print(min(abs(2 * i - s) for i in r))

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

L=sum(A)
X=0
M=float("inf")
for i in range(N-1):
    X+=A[i]
    M=min(M,abs(2*X-L))
print(M)

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

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

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

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

    print(ans)

if __name__ == '__main__':
    resolve()

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

for i in range(N-1):
  left += A[i]
  right -= A[i]
  sa = abs(left-right)
  if i == 0:
    ans = sa
  elif ans > sa:
    ans = sa

print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
num0 = 0
num1 = sum(a)

ans = 10**10
for i in range(0, N):
    num0 += a[i]
    num1 -= a[i]
    if ans > abs(num1-num0):
        ans = abs(num1-num0)
print(ans)