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

Solution (Python):

def read_tokens():
    return input().strip().split(' ')

def read_ints():
    return [int(token) for token in read_tokens()]

n, = read_ints()
a = read_ints()


s = sum(a)
p = a[0]
ans = float('inf')

for i in range(1, len(a)):
    r = s - p
    ans = min(ans, abs(p - r))
    p += a[i]

print(ans)

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

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

Solution (Python):
from itertools import accumulate

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

accAs = list(accumulate(As))
sumA = accAs[-1]

ans = float('inf')
for i in range(N-1):
    len1 = accAs[i]
    len2 = sumA - len1
    ans = min(ans, abs(len1-len2))

print(ans)

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

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

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
r = 0
l = sum(a)
mini = l
s = 0
for i in a:
    r += i
    l -= i
    if abs(r-l) < mini:
        mini = abs(r-l)
print(mini)

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

M = sum(A)
mid = M // 2

t = 0
for i in range(N):
  t += A[i]
  if t >= mid:
    l = t - A[i]
    r = t
    break

ans = abs((M - r) - r)
ans = min(ans, abs((M - l) - l))

print(ans)

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

    r = sum(A)
    l = 0
    min_diff = r
    for ai in A:
        r -= ai
        l += ai
        min_diff = min(min_diff, abs(r - l))

    print(min_diff)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
ss = [0]
for A in As:
  ss.append(ss[-1]+A)
ds = []
for i in range(N):
  l = ss[i]
  d = abs(2*l-ss[-1])
  ds.append(d)
r = min(ds)
print(r)

Solution (Python):
from itertools import accumulate

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

sm = sum(a)
acm = accumulate(a)

ans = float("inf")
for l in acm:
    r = sm - l
    ans = min(ans, abs(r - l))

print(ans)

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

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

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

mini = 1e10
for i in range(0, n - 1):
  diff = abs(m[i] - (m[n - 1] - m[i]))
  if diff < mini:
    mini = diff

print(mini)

Solution (Python):

import sys, itertools


def solve(N: int, A: "List[int]"):
    B = list(itertools.accumulate(A))
    S = sum(A)
    mn = 1e12
    for i in range(N-1):
        d = abs(B[i] - (S - B[i]))
        mn = min(d, mn)
    print(mn)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
sA = sum(A)
ans = sum(A)
L = 0
for i in A:
    L += i
    R = sA-L
    ans = min(ans,abs(R-L))

print(ans)

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

s_left = [0]
s_right = [sum(a)]
for i in range(n):
    s_left.append(s_left[-1] + a[i])
    s_right.append(s_right[-1] - a[i])

df = []
for i in range(1, len(s_left) - 1):
    df.append(abs(s_left[i] - s_right[i]))

print(min(df))

Solution (Python):
import itertools
n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
cumsum = itertools.accumulate(a)
ans = 10 ** 12
for i in cumsum:
  ans = min(ans,abs(sum_a - i * 2) )
print(ans)

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

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

Solution (Python):

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

s = sum(a)
tmp = 0
for i in range(n):
    tmp += a[i]
    s -= a[i]
    if tmp >= s:
        t = a[i]
        break

ans = min(abs(tmp-s), abs(tmp-s-a[i]*2))
print(ans)

Solution (Python):

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

Solution (Python):
def accmulate(arr):
    N = len(arr)
    cs = [0]*(N+1)
    for i in range(N):
        cs[i+1] = cs[i] + arr[i]
    return cs


def query(cs, l, r):
    return cs[r] - cs[l]

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

left = accmulate(A)
right = accmulate(A[::-1])[::-1]
ans = min(abs(l-r) for l, r in zip(left, right))
print(ans)

Solution (Python):
def main():
    n=int(input())
    a=list(map(int,input().split()))
    l=sum(a)
    cnt=0
    j=n
    for i in range(n):
        cnt+=a[i]
        if cnt>l/2:
            j=i
            break
    aa=sum(a[:j])
    b=sum(a[j:])
    c=sum(a[:j+1])
    d=sum(a[j+1:])
    print(min(abs(aa-b),abs(c-d)))
main()

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

l = nums[0]
r = sum(nums[1:])
min_dif = abs(l - r)
for i in range(1, len(nums)):
	l += nums[i]
	r -= nums[i]
	min_dif = min(min_dif, abs(l - r))

print(min_dif)

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    S = sum(A)
    cumsum = accumulate(A)
    mini = min(cumsum,key=lambda x: abs(x-S/2))
    ans = abs(mini-S/2)*2
    print(int(ans))
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    total = sum(A)
    left = 0
    ans = float("inf")
    for a in A:
        left += a
        ans = min(ans, abs(left * 2 - total))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect
from itertools import accumulate

n = int(input())
aaa = list(map(int, input().split()))
acc = list(accumulate(aaa))
mid = acc[-1] // 2
i = bisect(acc, mid)
print(min(abs(2 * acc[i] - acc[-1]), abs(2 * acc[i - 1] - acc[-1])))

Solution (Python):
from itertools import accumulate
from bisect import bisect

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

A = list(accumulate(A))
i = bisect(A, A[-1]/2)

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

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

N = int(input())
A = inpl()
S = sum(A)
l = 0
ans = 10**20
for a in A:
    l += a
    ans = min(ans, abs(S-2*l))
print(ans)

Solution (Python):
from collections import Counter

mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    cum = [0]* (n+1)
    for i in range(n):
        cum[i+1] = cum[i] + a[i]
    print(min(abs(2 * i - s) for i in cum))

main()

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

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

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

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

print(ans)

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

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

B = []
b = 0
for a in A:
    b += a
    B.append(b)

ans = 10**14
for i in range(N):
    ans = min(abs(B[-1]-2*B[i]), ans)
print(ans)

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
ans = 10 ** 10
num = sum(lis)
nu = 0
for i in range(n):
  nu += lis[i]
  ans = min(ans,abs(nu-num/2))
print(int(ans//0.5))

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

Solution (Python):
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]





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


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

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

cost = int(1e18)
p = 0
for i in range(N):
    cost = min(cost, abs(S - p - p))
    p += A[i]
p = 0
for i in range(N):
    cost = min(cost, abs(S - p - p))
    p += A[N - i - 1]
print(cost)

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

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

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

Solution (Python):
N = int(input())
arr = list(map(int, input().split()))
right = 0
left = sum(arr)
min = sum(arr)
for i in arr:
    right += i
    left -= i
    if abs(right - left) < min:
        min = abs(right - left)
print(min)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    s=sum(A)
    for i in range(n-1):
        A[i+1]+=A[i]
    ans=INF
    for i in range(n-1):
        ans=min(ans,abs(A[i]-(s-A[i])))
    print(ans)
resolve()

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))
s = 0
sa = sum(A)
ans = float("inf")
for i in range(n-1):
    s += A[i]
    ans = min(ans, abs(s-(sa-s)))
    
print(ans)

Solution (Python):
n=int(input())
al=list(map(int,input().split()))
ans=10**10
l=0
r=sum(al)
for i in al:
    l+=i
    r-=i
    ans=min(ans,abs(l-r))
print(ans)

Solution (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict


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


def main():
	N = int(input())
	A = getlist()
	X = float("inf")
	s = sum(A)
	c = 0
	for i in range(N):
		c += A[i]
		X = min(X, abs(s - 2 * c))
	print(X)
	


if __name__ == '__main__':
	main()

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

left = 0
right = sum(a)

d = 10**10
for i in a:
    left += i
    right -= i
    d = min(abs(left-right), d)

print(d)

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

Solution (Python):

N = int(input())

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

asum = sum(A)

l = 0
r = asum
ans = asum
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=[int(i) for i in input().split()]
sa=sum(a)
now=0
cost=10000000000000000000000
for i in range(n-1):
    now+=a[i]
    rest=sa-now
    cost=min(cost,abs(rest-now))
print(cost)

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

ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

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

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



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

A = np.array(A, dtype=int)
cs1 = A.cumsum()
cs2 = cs1[-1] - cs1
ans = np.abs(cs1 - cs2).min()
print(ans)

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

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

SUM=sum(A)

S=0
ANS=1<<60

for a in A:
    S+=a
    ANS=min(ANS,abs((SUM-S)-S))

print(ANS)

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

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

s = sum(a)

t = 0
res = float("inf")

for i in range(n-1):
    t += a[i]
    res = min(res,abs(s-2*t))
print(res)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

s1 = 0
s2 = sum(As)

ans = s2
for a in As:
    s1 += a
    s2 -= a
    
    ans = min(ans, abs(s1 - s2))
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


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

l = sum(a)
r = 0


ans = 10**18
for i in range(n-1):
    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()))

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

mindiff = 10**12

for a in A :
    left += a
    right -= a
    mindiff = min(mindiff, abs(right-left))


print(mindiff)

Solution (Python):

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
AA = inpl()

S = sum(AA)
L = 0

ans = INF
for a in AA:
    L += a
    R = S - L
    ans = min(ans,abs(L-R))

print(ans)

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

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  sm=sum(l)
  ans=inf

  _sm=0
  for x in l:
    _sm+=x
    other=sm-_sm

    ans=min(ans,abs(_sm-other))

  return ans


print(main())

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

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

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

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

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

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

L = 0
R = sum(A)
ans = sum(A)
for i in range(N-1):
  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()))
su = sum(A)

s = 0
for a in A:
    if (s+a) * 2 <= su:
        s += a
    else:
        ans = min((s+a)*2-su, su-s*2)
        print(ans)
        break

Solution (Python):
import sys
from itertools import accumulate
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
SA = sum(A)
CA = list(accumulate(A))
ans = float('inf')
for ca in CA:
    b = SA - ca
    ans = min(ans, abs(b - ca))
print(ans)