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:
N = int(input())
A = list(map(int, input().split()))
len_F = sum(A)
min_F = 2020202021
sum_A = 0

for i in range(N):
    sum_A += A[i]
    len_F -= A[i]
    if(abs(sum_A - len_F) < min_F):
        min_F = abs(sum_A - len_F)
        
print(min_F)

Submission status: Accepted

Solution code:
import sys

N, *A = map(int, sys.stdin.read().split())

d = 10 ** 20
r = 0
l = sum(A)
for a in A:
    r += a
    l -= a
    if abs(l - r) < d:
        d = abs(l - r)
print(d)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
total = sum(A)
i = 0
tmp=A[i]
while total/2 > tmp:
  i += 1
  tmp += A[i]
ans = min(2*tmp-total, total-2*(tmp-A[i]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(item) for item in input().split()]

all_sum = sum(A_list)

F_sum_list = [A_list[0]]

for j in range(1,N):
    F_sum_list.append(F_sum_list[-1] + A_list[j])

delta_list = [abs(all_sum - 2* i) for i in F_sum_list]

print(min(delta_list))

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
tmp=sys.maxsize
s = sum(a)
t = 0
for i in range(n):
    t += a[i]
    tmp = min(tmp,abs(s-t*2))
print(tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
s = sum(A)
p = 10**20
t = 0
for i in range(n):
    t += A[i]
    p = min(p, abs(s-t*2))
print(p)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
P = 10**20
X = 0
for i in range(N):
  X = X + A[i]
  P = min(P,abs(S-X*2))
print(P)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
sep_i = 0
for i, a in enumerate(A):
  sum_left += a
  if sum_left >= mid:
    sep_i = i
    sum_left2 = sum_left - a
    break
sum_right, sum_right2 = sum(A[sep_i + 1:]), sum(A[sep_i:])
print(min(abs(sum_right - sum_left), abs(sum_right2 - sum_left2)))

Submission status: Accepted

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

def main():
    N = int(input())
    a = list(map(int, input().split()))

    l = [0 for i in range(N)]
    l[0] = a[0]

    for i in range(1, N):
        l[i] = l[i-1] + a[i]
    ans = int(1e15)
    for i in range(N):
        ans = min(ans, abs(l[-1] - l[i] * 2))
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
sep_idx = 0
for i, l in enumerate(A):
  sum_left += l
  if sum_left >= mid:
    sep_i = i
    sum_left2 = sum_left - l
    break
sum_right, sum_right2 = sum(A[sep_i + 1:]), sum(A[sep_i:])
print(min(abs(sum_right - sum_left), abs(sum_right2 - sum_left2)))

Submission status: Accepted

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

    total = sum(A)
    s = 0
    m = total
    for a in A:
        s += a
        tmp = abs(total- 2*s)
        if tmp < m:
            m = tmp

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
seperation_i = 0
# for i, l in enumerate(A[:-1]):
for i, l in enumerate(A):
  sum_left += l
  if sum_left >= mid:
    seperation_i = i
    sum_left2 = sum_left - l
    break
sum_right, sum_right2 = sum(A[seperation_i + 1:]), sum(A[seperation_i:])
ans = min(abs(sum_left - sum_right), abs(sum_left2 - sum_right2))
print(ans)

Submission status: Accepted

Solution code:
# B - Iron Bar Cutting
from bisect import bisect
from itertools import accumulate


def main():
    N, *A = map(int, open(0).read().split())
    cumsum = tuple(accumulate(A))
    idx = bisect(cumsum, cumsum[-1] // 2)
    print(min(cumsum[idx] * 2 - cumsum[-1], cumsum[-1] - cumsum[idx - 1] * 2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
mid = sum(A)/2
length = 0
i = 0
while True:
    if length >= mid:
        length = min(abs(length-mid),abs(length-mid-A[i-1]))+mid
        break
    length += A[i]
    i += 1
print(int(length-(sum(A)-length)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
mid = total // 2
sum_left, sum_left2 = 0, 0
seperation_i = 0
for i, l in enumerate(A[:-1]):
  sum_left += l
  if sum_left >= mid:
      seperation_i = i
      sum_left2 = sum_left - l
      break
sum_right, sum_right2 = sum(A[seperation_i + 1:]), sum(A[seperation_i:])
print(min(abs(sum_right - sum_left), abs(sum_right2 - sum_left2)))

Submission status: Accepted

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

l=0
r=0

li=0
ri=0

for i in range(N):
    if l<=r:
        l+=A[li]
        li+=1
    else:
        r+=A[-1-ri]
        ri+=1
        
print(abs(l-r))

Submission status: Accepted

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

S = sum(A)
s = 0
res = S

for a in A:
    s += a
    cost = max(s, S - s) - min(s, S - s)
    res = min(res, cost)

print(res)

Submission status: Accepted

Solution code:
n = int(input())
*a, = map(int, input().split())
tot = sum(a)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
left_sum = 0
ans = float('inf')
for i in range(N):
    left_sum += A[i]
    right_sum = total - left_sum
    ans = min(ans, abs(left_sum - right_sum))
print(ans)

Submission status: Accepted

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

# say we pick some prefix of the bars
# what's the min cost to make that prefix the halfway point?
# never contract, always expand?
pref = 0
for x in a:
    pref += x
    rem = tot - pref
    ans = min(ans, abs(rem - pref))

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]

ans = 10 ** 18
a = sum(L)
b = 0

for i in range(N):
    index = (i + 1) * -1
    a -= L[index]
    b += L[index]

    if a == b:
        print(0)
        exit()

    ans = min(ans, abs(a - b))

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    s = sum(a)
    t = 0
    r = inf
    for c in a:
        t += c
        k = abs(s-t*2)
        if r > k:
            r = k

    return r


print(main())

Submission status: Accepted

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

tot = sum(A)
epi = tot
pre = 0
dif = tot
for i in range(n):
	pre += A[i]
	epi -= A[i]

	if abs(pre-epi) > dif:
		break
	else:
		dif = abs(pre-epi)

print(dif)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sumall = 0
for a in A:
	sumall += a
ans = N * 2020202021
tmp = 0
for a in A:
	tmp += a
	ans = min(ans, abs(sumall - 2 * tmp))
print(ans)

Submission status: Accepted

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

S = sum(A)

cost = 1e15
acc_sum = 0
for a in A:
  acc_sum += a
  cost = min(cost, abs(S-2*acc_sum))
  
print(cost)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from math import ceil
n=int(input())
a=list(map(int,input().split()))
b=sum(a)
s=0
lis=[]
lis2=[]
if sum(a)%2==0:
  for i in range(n):
    s+=a[i]
    lis.append(abs(b//2-s))
  print(2*min(lis))
else:
  for i in range(n):
    s+=a[i]
    lis.append(abs(b//2-s))
    lis2.append(abs(ceil(b/2)-s))
  print(2*min(min(lis),min(lis2))+1)

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int,input().split()))
b = []
add = 0
for i in a:
    add += i
    b.append(add)

for i in range(len(b)):
    if b[i]> add / 2:
        x = abs(add - 2 * b[i])
        y = add - 2 * b[i-1]
        ans = min(x,y)
        break
print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_b
num=int(input())
lenlist=input().split()
lensum=0
sumlist=[]
for i in range(num):
    lensum+=int(lenlist[i])
    sumlist.append(lensum)
for i in range(num-1):
    if sumlist[i] <= lensum/2 <= sumlist[i+1]:
        print(min(abs(lensum-2*sumlist[i]),abs(lensum-2*sumlist[i+1])))
        break

Submission status: Accepted

Solution code:
N = int(input().strip())
arr = list(map(int, input().split()))
_sum = sum(arr)
now = 0
ans = 1e18
for i in arr:
    now += i
    rest = _sum - now
    ans = min(ans, abs(rest-now))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
S=sum(A)
X=0
Y=S
d=S
for i in range(N):
    X+=A[i]
    Y-=A[i]
    d=min(d,abs(X-Y))
print(d)

Submission status: Accepted

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

total = sum(A)

s = 0
for a in A:
	s += a
	if 2 * s >= total:
		break

B = A[::-1]
t = 0
for b in B:
	t += b
	if 2 * t >= total:
		break

print(min(2*s-total, 2*t-total))

Submission status: Accepted

Solution code:
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 ** 20
    for v in a:
        t += v
        mt = min(mt, abs(sa - t - t))
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_b

N = int(input())
A = list(map(int, input().split()))
# rA = A.copy()[::-1]

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

# print(A)
# print(cA)

diff = int(1e18)
for cut in range(1, N):
    # print(cut)
    costL = cA[cut - 1]
    costR = cA[-1] - cA[cut - 1]
    # print("  ", costL, costR)
    diff = min(diff, abs(costL - costR))

print(diff)

Submission status: Accepted

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

cnt = 0
ans = s
for i in a:
    cnt += i
    ans = min(ans, abs(s - 2 * cnt))

print(ans)

Submission status: Accepted

Solution code:
def solve(n, a):
    total = sum(a)
    best = total
    cur = 0
    for i in range(n):
        cur += a[i]
        best = min(best, abs(2*cur - total))
    return best

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
Asum = sum(A)
Asum2 = 0
c = 0
for mid in range(len(A)):
    if 2*Asum2 > Asum:
        c =1
        Asum3 = Asum2
        mid -= 1
        Asum2 -= A[mid]
        break
    Asum2 += A[mid]
if c == 0:
    if 2*Asum2 > Asum:
        Asum3 = Asum2
        mid -= 1
        Asum2 -= A[mid]
if abs(Asum - 2*Asum2) <= abs(Asum - 2*Asum3):
    Asuma = Asum2
else:
    Asuma = Asum3
c = abs(Asum - 2*Asuma)
print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
h = 0
ans = 0
for i in a:
    if (h + i > s / 2):
        ans = min(int(abs(h - s / 2) / 0.5), int(abs((h + i) - s / 2) / 0.5))
        break
    h += i
print(ans)

Submission status: Accepted

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

left = 0
right = sum(A)
ans = float('inf')
for a in A:
    left += a
    right -= a
    ans = min(ans, abs(left - right))

print(ans)

Submission status: Accepted

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

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

ans = float('inf')
for i in range(n-1):
    s = cum[i+1]
    t = cum[-1] - s
    ans = min(ans, abs(s-t))

print(ans)

Submission status: Accepted

Solution code:
import bisect
import math
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    A_sum = sum(A)
    A_cumsum_l = [0 for _ in range(N)]
    A_cumsum_l[0] = A[0]
    for i in range(1,N):
        A_cumsum_l[i] = A_cumsum_l[i-1] + A[i]
    A_cumsum_r = [0 for _ in range(N)]
    for i in range(N):
        A_cumsum_r[i] = A_sum - A_cumsum_l[i]
    
    index = 0
    sub = 10**12
    for i in range(N):
        if abs(A_cumsum_l[i]-A_cumsum_r[i])<sub:
            index = i
            sub = abs(A_cumsum_l[i]-A_cumsum_r[i])


    print(sub)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]

S = sum(A)

v = 0
u = 0
for a in A:
    v += a
    if v >= S / 2:
        break
    u += a

print(min(v * 2 - S, S - u * 2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
ans = s
now = 0
for x in a:
    now += x
    ans = min(ans, abs(s - now - now))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
p=q=sum(A)
for i in range(N):
  p-=2*A[i]
  q=min(abs(p),q)
print(q)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
total=sum(arr)
acum=[arr[0]]
for i in range(1,n):
  acum.append(arr[i]+acum[-1])
ans=10**18
for i in range(n):
  tmp=abs(acum[i]-(total-acum[i]))
  ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:

def resolve():
    N=int(input())
    A=[int(i) for i in input().split()]
    for i in range(1,N):
        A[i]+=A[i-1]
    print(min([abs(A[-1]-2*A[i]) for i in range(N)]))





if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
l = A[0]
r = 0
indexl = 1
indexr = N-1
for i in range(N-1):
	if l <= r:
		l += A[indexl]
		indexl += 1
	elif l > r:
		r += A[indexr]
		indexr -= 1
print(abs(l-r))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

sum_l = [0]+list(accumulate(a))
sum_r = [0]+list(accumulate(a[-1::-1]))

cost = sum_r[-1]
for i in range(n+1):
    cost = min(cost, abs(sum_l[i]-sum_r[n-i]))
print(cost)

Submission status: Accepted

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

S=sum(A)
s=0

ans=10**10
for i in range(N):
  s+=A[i]
  ans=min(ans,abs((S-s)-s))
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_b

import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


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

    cum_len = [0] * len(al)
    cum_len[0] = al[0]
    for i in range(1, len(al)):
        cum_len[i] = cum_len[i-1] + al[i]

    # print(sum(al))
    ans = 10**10
    for i in range(len(al)):
        left = cum_len[i]
        right = cum_len[-1] - cum_len[i]
        # print(left, right)
        ans = min(ans, abs(left - right))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
SUM = sum(A)
def sum_half():
    cnt = 0
    for i in range(N):
        cnt += A[i]
        if cnt>SUM/2:
            return i
index = sum_half()
min_cost = 10000000000
p = sum(A[:index+1])-sum(A[index+1:])
q = sum(A[index:])-sum(A[:index])
if min_cost>p:
    min_cost = p
if min_cost>q:
    min_cost = q
print(min_cost)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split()))
Sum = sum(l)
l_l = 0
ans = 10**10
for i in range(N):
  l_l += l[i]
  ans = min(ans,abs(Sum-(2*l_l)))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li_a = map(int, input().split())
li_a_left = list()
total = 0
for l in li_a:
    if len(li_a_left) == 0:
        li_a_left.append(l)
    else:
        li_a_left.append(li_a_left[-1] + l)
    total += l

print(min(abs(left - (total - left)) for left in li_a_left))

Submission status: Accepted

Solution code:
from itertools import accumulate
from operator import add
x = int(input())
lst = [int(i) for i in input().split()]
a = list(accumulate(lst,add))

s = sum(lst)
b = []
for i in list(a):
  b.append(s - i)

c = []
for i in range(x):
  c.append(abs(b[i] - a[i]))
print(min(c))

Submission status: Accepted

Solution code:
from itertools import accumulate as ac
n=int(input())
a=list(ac(list(map(int,input().split()))))
d=float("INF")
for i in range(n):
    d=min(d,abs(a[-1]-2*a[i]))
print(d)

Submission status: Accepted

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

li = [0]*(n+1)

for i in range(n):
    li[i+1] = li[i] + a[i]
    
#print(li)

li_sum = sum(a)

ans = 10**18

for i in range(n):
    #print(ans)
    if ans > abs(li_sum - 2*li[i]):
        ans = abs(li_sum - 2*li[i])
        
print(ans)

Submission status: Accepted

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

sun = [0] * (n +1)

for i in range(0, n):
    sun[i + 1] = sun[i] + li[i]

delta = [0] * n
for i in range(0, n):
    delta[i] = sun[i] - (sun[n] -sun[i])

ans = abs(delta[0])
for i in range(0, n):
    if ans > abs(delta[i]):
        ans = abs(delta[i])
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
sum = [0]
for i in range(0, n):
    asum = sum[i] + As[i]
    sum.append(asum)
#print(sum)
ans = 10**18
for j in range(0, n):
    l = sum[j]
    r = sum[n] - sum[j]
    find = abs(l-r)
    ans = min(ans, find)
print(ans)

Submission status: Accepted

Solution code:
def acinput():
    return list(map(int, input().split(" ")))

import numpy as np

N=int(input())
bars=acinput()

bar_cs=np.cumsum(bars)

res=10**15

for i in range(N-1):
    R=bar_cs[-1]-bar_cs[i]
    L=bar_cs[i]
    res=min(res,np.abs(R-L))
    #print(i,L,R)

print(res)

Submission status: Accepted

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

i = 0
j = n-1
sumL =0
sumR = 0
while(i <= j):
    if(sumL < sumR):
        sumL += arr[i]
        i +=1
    else:
        sumR += arr[j]
        j -=1

print(abs(sumL - sumR))

Submission status: Accepted

Solution code:
n=int(input())
l= list((int(x) for x in input().split()))
beg=end=i=0
n-=1
while(i<=n):
	if(beg>end):
		end+=l[n]
		n-=1
	else:
		beg+=l[i]
		i+=1
print(abs(end-beg))

Submission status: Accepted

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

i = 1
j = n-2
forSum = arr[0]
revSum = arr[-1]

while(i <= j):
    if(forSum < revSum):
        forSum += arr[i]
        i += 1
    else:
        revSum += arr[j]
        j -= 1


print(abs(forSum-revSum))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = [int(x) for x in input().split()]
cumsum = list(itertools.accumulate(A))
ans = float("INF")
for i in range(N):
  ans = min(ans,abs(cumsum[i]-(cumsum[-1]-cumsum[i])))
print(ans)

Submission status: Accepted

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    st = sum(a)
    t = 0
    abs_t = 10 ** 10
    for i in range(n-1):
        t += a[i]
        st -= a[i]
        abs_t = min(abs_t, abs(st - t))
    print(abs_t)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
n = [int(i) for i in input().split()]
l = sum(n)
s=0
for i in range(N):
        s+=n[i]
        if s>=l/2:
                if abs(l/2-s) > s1:
                        s-=n[i]
                e = l-s
                break     
        s1=abs(l/2 - s)
print(abs(s-e))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
L_i = 0
R_i = n - 1
L = 0
R = 0
while L_i <= R_i:
  if L >= R:
    R += A[R_i]
    R_i -= 1
  else:
    L += A[L_i]
    L_i += 1
    
print(abs(L-R))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from itertools import accumulate

    n = int(input())
    a = list(accumulate([0] + list(map(int, input().split()))))
    ans = float('inf')

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

total = sum(A)
cut = 0
check = []

for i in A:
    cut += i
    check.append(abs(cut + cut - total))

print(min(check))

Submission status: Accepted

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

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

for i in range(n):
  if m[i]>=a/2:
    a1=m[i]
    a2=m[i-1]
    break

a1=abs(a1*2-a)
a2=abs(a2*2-a)
print(min(a1,a2))

Submission status: Accepted

Solution code:
def find_equal(lis, n):
    sum_l = 0
    sum_r = 0
    r_c = n - 1
    l_c = 0
    all_processed = 0
    while True:
        if sum_l <= sum_r:
            sum_l += lis[l_c]
            l_c += 1
            all_processed += 1
        elif sum_r < sum_l:
            sum_r += lis[r_c]
            r_c -= 1
            all_processed += 1
        if all_processed == n:
            break
    return sum_l, sum_r


n = int(input())
nums = list(map(int, input().split()))
ans = find_equal(nums, len(nums))
print(max(ans) - min(ans))

Submission status: Accepted

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

s = sum(a)

half = [0,s]
for i in range(n):
    if abs(half[1]-half[0])>abs(half[1]-half[0]-2*a[i]):
        half[0]+=a[i]
        half[1]-=a[i]
    else:
        break
print(abs(half[1]-half[0]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, A: "List[int]"):
    r = [0] * (N+1)
    rr = [0] * (N+1)

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

    ans = INF
    for i in range(1,N):
        ans = min(ans, abs(r[i]-rr[i]))

    print(ans)

    
    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))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

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

a = [0]
for aa in A:
    a.append(a[-1] + aa)
x = a[-1]/2
i = bisect.bisect(a, x)
d = min(x - a[i-1], a[i] - x)
print(int(d / 0.5))

Submission status: Accepted

Solution code:
from itertools import accumulate
import math

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

ans = 10**10

sum_list_aaa =list(accumulate(list_aaa))

for i in range(N):
    zen = sum_list_aaa[i]
    go = sum_list_aaa[-1]-sum_list_aaa[i]
    ans = min(ans,abs(zen-go))

print(ans)

Submission status: Accepted

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

ans = [0 for _ in range(n+1)]

for i in range(n):
    ans[i+1] = ans[i] + l[i]

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

print(num)

Submission status: Accepted

Solution code:
N=input()
b=input().split()
sum=0
bigsum=0
for i in b:
    sum+=eval(i)
standard=sum/2
for i in b:
    bigsum+=eval(i)
    if bigsum>standard:
        smallsum=bigsum-eval(i)
        break
if bigsum-standard>standard-smallsum:
    print(sum-2*smallsum)
else:
    print(2*bigsum-sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
l = S//2
ans = S
s = 0
for k in range(N):
    s += A[k]
    ans = min(ans,abs(l-s)+abs(S-s-l))
print(ans)

Submission status: Accepted

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

ans = 10**10
s = sum(a_li)
tmp = 0

for i in range(n):
    tmp += a_li[i]
    res = abs(s - 2*tmp)
    ans = min(ans,res)
    
print(ans)

Submission status: Accepted

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

total = sum(a)

minimam = total
left = 0
right = total

for ai in a:
    left += ai
    right -= ai
    if abs(right - left) < minimam:
        minimam = abs(right - left)

print(minimam)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
from itertools import accumulate
X = list(accumulate([0]+x))
ans = float('inf')
for i in range(1,n):
    ans = min(ans,abs(X[-1]-X[i]*2))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import numpy as np
a=np.array(a)
b=np.cumsum(a)
#print(b)
c=sum(a)-b
#print(c)
d=abs(b-c)
#print(d)
print(min(d))

Submission status: Accepted

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

total = sum(A)
count = 0
cut = 0
for num, a in enumerate(A):
    count += a
    if count >= total//2:
        cut = num
        break

left = sum(A[:cut+1])
right = sum(A[cut+1:])
ans1 = abs(left-right)

left -= A[cut]
right += A[cut]
ans2 = abs(left-right)

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import bisect
import math

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

S = []
s = 0
for a in A:
    s += a
    S.append(s)


i = bisect.bisect_left(S, g)
print(min(math.ceil(sa-S[i-1]*2), math.ceil(S[i]*2-sa)))

Submission status: Accepted

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

s=0
for i in range(len(x)):
	s+=x[i]
i=0
l=0
while (l<(s//2)):
	l+=x[i]
	i+=1
r=abs(s-l-l) 
i=len(x)-1
l=0
while(l<(s//2)):
	l+=x[i]
	i-=1
if abs((s-l)-l)<r:
	r=abs(s-l-l) 
print (r)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
for tc in [0]*1:

    n=int(input())
    l=list(map(int,input().split()))
    tot=sum(l)
    f=0
    ans=0
    for i in range(n):
        ans+=l[i]
        f=0
        if ans*2==tot:
            f=1
            break
    if f:
        print(0)
    else:
        ans=0
        tr=10**9+7+2020202020
        for i in range(n):
            ans+=l[i]
            #print(ans,tr)
            tr=min(tr,abs(ans-abs(tot-ans)))
        print(tr)

Submission status: Accepted

Solution code:
a= int (input())
lis=list(map(int,input().split()))
for i in range (1,a):
    lis[i]=lis[i]+lis[i-1]
k=lis[-1]/2
for i in range(a):
    if lis[i]>=k:
        l=lis.index(lis[i])
        break
u=abs(2*lis[l]-lis[-1])
v=abs(2*lis[l-1]-lis[-1])
print(min(u,v))

Submission status: Accepted

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

j=0

for i in range(n):
  j+=l[i]
  if j*2==s:
    print(0)
    break
  elif j*2>s:
    print(min((j*2-s),(s-(j-l[i])*2)))
    break

Submission status: Accepted

Solution code:
import numpy as np
import bisect
N = int(input())
A = list(map(int,input().split()))
sumA = sum(A)
cumsum = np.cumsum(A)
ans = 2020202020
for a in cumsum:
    ans = min(ans,abs(2*a-sumA))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def calc_sum(v, n):
    a = [0] * (n)
    for i in range(n):
        if i == 0:
            a[i] = v[i]
            continue
        a[i] = a[i-1] + v[i]
    return a

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

    a = calc_sum(v, n)
    b = list(reversed(calc_sum(list(reversed(v)), n)))

    ans = INF
    for i in range(n-1):
        ans = min(ans, abs(a[i] - b[i+1]))
    print(ans)
if __name__ == '__main__':
    main()