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



Sample input:
3
2 4 3


Sample output:
3

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))

s = sum(a) / 2
i = 0
c = 0
while (True):
    c += a[i]
    if c > s:
        d = c - a[i]
        break
    i += 1
    

x = c - s
y = s - d
ans = min(x, y) * 2
print(int(ans))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a = [int(item) for item in input().split()]
cumsum = [0] * (n + 1)
total_len = sum(a)
for i in range(n):
    cumsum[i+1] = cumsum[i] + a[i]

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

print(ans)

Submission status: Accepted

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

s = sum(A)
idx = 0
s1 = 0
half = s//2
for i, a in enumerate(A):
    s1 += a
    if half < s1:
        idx = i
        break

s2 = sum(A[i+1:])
ans1 = abs(s1-s2)
s3 = sum(A[i:])
ans2 = abs(s1-s3-A[i])
print(min([ans1, ans2]))

Submission status: Accepted

Solution code:
#coding:utf-8
import bisect
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = True



def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    n = II()
    a = LMIIS()

    s = sum(a)

    m = s
    x = 0
    for i in range(n):
        x += a[i]
        m = min(m, abs(s/2 - x))
    ans = int(m * 2)


    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
d_A = sum(A)

d1 = 0
d2 = d_A
delta = 1000000000000
for i in range(0,N):
    d1 += A[i]
    d2 -= A[i]
    if abs(d1 - d2) <= delta:
        delta = abs(d1-d2)
print(delta)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    a = tuple(map(int, input().split()))

    ret = float('inf')

    left = 0
    right = sum(a)
    for a_ in a:
        left += a_
        right -= a_
        ret = min(ret, abs(left - right))

    return ret


print(solve())

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))

total = sum(As)
current = 0
half = None
for i in range(len(As)):
    if current + As[i] >= (total / 2):
        if abs(current - total / 2) <= abs((current + As[i]) - total / 2):
            half = current
        else:
            half = current + As[i]
        break
    current += As[i]
print(abs((total - half) - half))
exit(0)

Submission status: Accepted

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

L = 0
R = 0
l = 0
r = N - 1
for i in range(N):
    if L < R:
        L += A[l]
        l += 1
    else:
        R += A[r]
        r -= 1

print(abs(L-R))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = [int(i) for i in input().split(" ")]
s = [a[0]]
for i in a[1:]:
    s.append(s[-1]+i)
s = np.array(s)
ar = np.argmin(np.abs(s-s[-1]/2))
if s[ar]==s[-1]/2:
    print(0)
else:
    te=s[ar]-s[-1]/2
    print(int(abs(te)//0.5))

Submission status: Accepted

Solution code:
import sys
import heapq
from collections import defaultdict
import itertools
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
L = sum(A)
length = 0
mid = None
for i in range(N):
    length += A[i]
    if length >= L / 2:
        mid = i
        break
#print('mid={}, length={}'.format(mid, length))

d = min(length - L / 2, L / 2 - (length - A[mid]))
print(int(d * 2))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=float(sum(a))
c=b/2
d=0
e=0
while d<c:
  d=d+a[e]
  e=e+1
print(int(min(d-c,c-d+a[e-1])*2))

Submission status: Accepted

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

r = 0
l = 0
k = 0
for i in range(len(a)):
    k = i
    if l+a[i] > s//2:break
    l += a[i]
r = s - a[k] - l

print(min(max(l+a[k],r)-min(l+a[k],r),max(l,r+a[k])-min(l,r+a[k])))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()
total = sum(A)
A = list(accumulate(A))
r = bisect_left(A, total / 2)
l = r - 1
if r == n - 1:
    print(abs((total - A[l]) - A[l]))
else:
    print(min(abs((total - A[r]) - A[r]), abs((total - A[l]) - A[l])))

Submission status: Accepted

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

m=0
for i in range(N):
  if m+A[i]<=S//2:
    m+=A[i]
  else:
    ind=i
    break
  
if m==S/2:
  print(0)
  exit()
  
n=S-m-A[ind]

if n<m:
  n+=A[ind]
else:
  m+=A[ind]
  
print(abs(m-S//2)+abs(n-S//2))

Submission status: Accepted

Solution code:
n = int(input())
a_array = [int(x) for x in input().split()]
ruiseki = [0]
now = 0
sum_a = sum(a_array)
half = sum_a//2
diff = float("inf")
for a in a_array:
    ruiseki.append(ruiseki[-1] + a)
    left = ruiseki[-1]
    right = sum_a - left
    diff = min(diff, abs(half-left) + abs(half-right))
print(diff)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ret = float('inf')
s = sum(a)
curSum = 0
for cut in range(len(a)):
    ret = min(ret, abs(curSum - s + curSum))
    curSum += a[cut]
print(ret)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
li=[]
x=0
for i in range(n):
  x+=a[i]
  li.append(abs(s-2*x))

print(min(li))

Submission status: Accepted

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


A = list(accumulate(A))
ans = float('inf')
for a in A[:-1]:
    ans = min(ans, abs((A[-1] - a) - a))


print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ls=list(map(int,input().split(" ")))

left=0
right=sum(ls)
diff=right-left
prev_diff=10**100
for i in ls:
    left+=i
    right-=i
    diff=abs(right-left)
    if diff>=prev_diff:
        break
    prev_diff=diff
print(prev_diff)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        A = list(map(int, input().split()))

        sum_all = sum(A)
        min_diff = sum_all
        left = 0
        right = sum_all
        for i in range(N):
            left += A[i]
            right -= A[i]
            tmp_diff = abs(right - left)
            min_diff = min(min_diff, tmp_diff)

        print(min_diff)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int,input().split()))

import itertools
al_ac = list(itertools.accumulate(al))

ans = float("inf")

for a in al_ac:
    if ans > abs( (al_ac[-1] - a) -a) :
        ans = abs( (al_ac[-1] - a) -a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
#show_flg=True

ans=0
n=I()
a=LI()

tot=sum(a)
acc=0
for i in range(n):
    acc+=a[i]
    if acc>=tot/2:
        k=i
        break
if tot==acc*2:
    print(0)
    exit()
else:
    l=min(abs(2*acc-tot),abs(2*(acc-a[k])-tot))
    ans=l

show(k,tot,acc,acc-a[k])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split()))
sm = sum(an)
left = 0
mini = sm
for a in an:
  left += a
  dev = abs(sm-2*left)
  mini = min(dev, mini)
print(mini)

Submission status: Accepted

Solution code:
def main():  
    n = int(input())
    a = list(map(int,input().split()))
    asum = sum(a)
    asum2 = asum // 2
    x = 0
    tmp = 0
    for i in range(len(a)):
        tmp += a[i]
        if tmp >= asum2:
            x = i
            break
    spa1 = sum(a[0:x])
    spa2 = sum(a[0:x+1])
    ans1 = asum - 2*spa1
    ans2 = 2*spa2 - asum
    print(min([ans1,ans2]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

d=[0]*n
d[0]=sum(a)
for i in range(n-1):
    d[i+1]=d[i]-2*a[i]
d=[abs(x) for x in d]
print(min(d))

Submission status: Accepted

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

L=sum(A)

LS=A[0]
for i in range(1,N):
    LS+=A[i]
    if LS>L/2:
        L1=LS-A[i]
        L2=L-L1
        L3=LS
        L4=L-LS
        ans=min(abs(L1-L2),abs(L3-L4))
        print(ans)
        exit()

Submission status: Accepted

Solution code:
n=int(input())
l=tuple(map(int,input().split()))
s=sum(l)
m=0
p=0
while 2*m<s:
  m+=l[p]
  p+=1
a=2*m-s
b=s-2*(m-l[p-1])
print(min(a,b))

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
s=[0]
for i in range(n):
  s.append(s[i]+a[i])
h=sum(a)/2
for i in range(n+1):
  if s[i]==h:
    print(0)
    sys.exit()
  if s[i]>h:
    x=i
    break
ans=min(int((s[x]-h)*2),int((h-s[x-1])*2))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n= int(input())
a = list(map(int,input().split()))
ans = float("inf")
flag_1 = 0
flag_2 = sum(a)
for i in range(n):
  flag_1 += a[i]
  flag_2 -= a[i]
  ans = min(ans,abs(flag_1-flag_2))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(float, input().split()))
mid = sum(A)/2
tmp = 0
for i in range(N):
    tmp+=A[i]
    if tmp>mid:
        if tmp-mid>mid-(tmp-A[i]):
            print(int(sum(A[i:])-sum(A[:i])))
        else:
            print(int(tmp-sum(A[i+1:])))
        break

Submission status: Accepted

Solution code:
from numpy import cumsum

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

all = sum(L)
cum = list(cumsum(L))

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

Submission status: Accepted

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

l=0
r=sum(A)
ans=float('inf')
for i in range(n):
  l+=A[i]
  r-=A[i]
  ans=min(ans,abs(l-r))
print(ans)

Submission status: Accepted

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

goukei = sum(a)
nagasa = goukei
if nagasa%2 == 1:
  nagasa = nagasa//2 + 1
else:
  nagasa = nagasa//2

leftA = 0
leftB = 0
rightA = 0
rightB = 0
for aa in a:
  leftB = leftA
  leftA += aa
  if leftA > nagasa:
    break

rightA = goukei - leftA
rightB = goukei - leftB
print(min(abs(leftA-rightA), abs(leftB-rightB)))

Submission status: Accepted

Solution code:
N = int(input())
An = list(map(int,input().split(' ')))

An_half_sum = sum(An)/2
i = 0
sum_count =0
for i in range(N):
    sum_count += An[i]
    if An_half_sum <= sum_count:
        break
min_num = min(sum_count - An_half_sum, An_half_sum - sum_count + An[i])
print(int(min_num*2))
#print(sum_count - An_half_sum)
#print(An_half_sum - sum_count + An[i])

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i)for i in input().split()]
d,s=0,sum(a)
i=s
for p in a:
    d+=p
    f=abs(s-2*d)
    if f<i:i=f
print(int(i))

Submission status: Accepted

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

length = sum(A)
left = 0
left1 = 0
left2 = 0

for i in range(N):
    left += A[i]
    if left >= length//2:
        left1 = left
        left2 = left - A[i]
        break

right1 = length - left1
right2 = length - left2

print(min(abs(left1 - right1), abs(left2 - right2)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
N=input()
A=map(int, sys.stdin.readline().split())

L=[]
for i,a in enumerate(A):
    if i==0:
        L.append(a)
    else:
        L.append(L[-1]+a)

ans=float("inf")
for x in L:
    y=L[-1]-x
    ans=min(ans,abs(x-y))

print ans

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    tmp1=0
    tmp2=0
    low=0
    high=n-1
    while low!=high:
        if tmp1<tmp2:
            tmp1+=a[low]
            low+=1
        else:
            tmp2+=a[high]
            high-=1
    if tmp1<tmp2:
        print(abs(tmp1+a[low]-tmp2))
    else:
        print(abs(tmp2+a[low]-tmp1))

    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a_array = list(map(int, input().split()))
a_sum = sum(a_array)
left = 0
for i in range(n):
    left += a_array[i]
    if left > a_sum / 2:
        break
print(min(abs(a_sum - 2 * left), abs(a_sum - 2 * left + 2 * a_array[i])))

Submission status: Accepted

Solution code:
import numpy as np
import bisect

N = int(input())
A = np.array(list(map(int, input().split())))
s = sum(A)
cum = A.cumsum()
index = bisect.bisect_left(cum, s / 2)
if index == 0:
    answer = abs(s - 2 * A[0])
else:
    answer = min(abs(s - 2 * cum[index-1]), abs(s - 2 * cum[index]))
print(answer)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans =0
count = 0

n=int(input())
A=list(map(int,input().split()))
SA=sum(A)
SA2=SA/2
mindis=SA
for ai in A:
    count+=ai
    mindis=min(mindis,abs(count*2-SA))
print(mindis)

Submission status: Accepted

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

b=[0]*n
b[0]=a[0]
for i in range(1,n):
    b[i]=a[i]+b[i-1]
suma=sum(a)

import bisect
ncut = bisect.bisect(b,suma//2)

ans1=abs( sum(a[:ncut])-sum(a[ncut:]) )
if ncut<n:
    ans2=abs( sum(a[:ncut+1])-sum(a[ncut+1:]) )
else:
    ans2=10**12
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
left = 0
right = sum(A)
diff = abs(left - right)
for i in range(N):
    left += A[i]
    right -= A[i]
    if abs(left - right) < diff:
        diff = abs(left - right)
print(diff)

Submission status: Accepted

Solution code:
import collections as c
ip = lambda : map(int, input().split())
# ##############
N = int(input())
a = c.deque([int(i) for i in input().split()])
l,r = 0,0
while len(a) > 0:
    if l>=r:
        r += a.pop()
    else:
        l += a.popleft()
print(abs(l-r))

Submission status: Accepted

Solution code:
from sys import stdin
N = int(input())
A= [int(x) for x in stdin.readline().rstrip().split()]

mindif = 2020202020*200000
right = sum(A)
left = 0

for i in range(N):
    right -= A[i]
    left += A[i]
    if mindif > abs(right-left):
        mindif =  abs(right-left)

print(mindif)

Submission status: Accepted

Solution code:
N = int(input())
b = [int(x) for x in input().split()]
d = sum(b)
ans = d
for i in range(N):
  if d < 0:
    break
  d -= b[i] * 2
  ans = min(ans, abs(d))
print(ans)

Submission status: Accepted

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

l = sum(a)

sum_1 = 0
split = 0
for i, x in enumerate(a):
    sum_1 += x
    if sum_1 > (l / 2):
        split = i
        break

sum_2 = sum_1 - a[split]

ans = min(2 * sum_1 - l, l - 2 * sum_2)
print(ans)

Submission status: Accepted

Solution code:
import math
a=int(input())
b=list(map(int,input().split()))
c=sum(b)
d=0
e=float("inf")
f=0
for i in b:
  d+=i
  if abs((c/2)-d)<e:
    e=abs((c/2)-d)
    f=d
print(abs(c-f*2))

Submission status: Accepted

Solution code:
n=int(input())
A=[int(i) for i in input().split()]

tar=sum(A)/2

now=0
pre=0
for i in range(n):
    now+=A[i]
    if pre<=tar and tar<=now:
        #print(pre,tar,now)
        break
    pre=now

if tar-pre>now-tar:
    print(abs(now-(sum(A)-now)))
else:
    print(abs(pre-(sum(A)-pre)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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
        
#print(l_sum, r_sum)
ans = abs(l_sum - r_sum)
print(ans)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)
# print(B) #DB

l=bi.bisect_left(B,s/2);r=bi.bisect_right(B,s/2)
# print(l,r) #DB
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
import itertools

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

total = sum(a)

# print(total)
# print(s)
temp = 10**10

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

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

print(temp)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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
    # print(sum_a - sum_i, sum_i)
    min_diff = min(min_diff, abs(diff))

print(min_diff)

Submission status: Accepted

Solution code:
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])
#print(b)
for i in range(n-1):
    c=abs(b[-1]-2*b[i])
    if c<=ans:
        ans=c
        #print(ans)
print(ans)

Submission status: Accepted

Solution code:
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)
#2*(C[i]-x)=C[-1]-x
x = 2 * C[i] - C[-1]
#2*(C[i-1]+y)=C[-1]+y
y = C[-1] - 2 * C[i - 1]
print(min(x, y))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
#print(A)
L=[A[0]]
for i in range(1,N):
    L.append(L[-1]+A[i])
S=sum(A)
#print(S)
ans=10**15
for i in range(N):
    if abs(S-2*L[i])<ans:
        ans=abs(S-2*L[i])
print(ans)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)