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:
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
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    a =[0]+list(map(int, input().split()))
    b = list(accumulate(a))
    c = sum(a)
    res = 10000000000000000
    for i in range(1, n):
        s = b[i]
        t = c-s
        res = min(res, abs(s-t))
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum_r=sum(a)
sum_l=0
ans=10**20
for i in range(n-1):
    sum_l+=a[i]
    sum_r-=a[i]
    if abs(sum_r-sum_l)<ans:
        ans=abs(sum_r-sum_l)
print(ans)

Submission status: Accepted

Solution code:
import numpy

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

if res[-1] % 2 == 0:
  L = res[-1]//2
  ans = [abs(r - L) for r in res]
  print(2*min(ans))
else:
  L1 = res[-1]//2
  L2 = L1 + 1
  ans = [min(abs(r - L1),abs(r-L2)) for r in res]
  print(2*min(ans)+1)

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int, input().split()))
s = sum(al)
acc = 0
res = 10**18
for a in al:
    acc += a
    res = min(res, abs((s-acc)-acc))
print(res)

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
# arrA = list(map(int,input().split()))
arrA = np.array(input().split(),dtype=np.int64)

arrA = arrA.cumsum()
high = arrA[-1]
ans = 2020202020*200000+2

for i in arrA[:-1]:
    num = abs(high - i*2)
    ans = min(ans,num)

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
sa = sum(a)
ha = sa/2
x = 0
for i in range(n):
  x+=a[i]
  if x>=ha:
    break
y = sa-x
print(min(abs(x-y),abs((x-a[i])-(y+a[i]))))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(1, N):
    A[i] += A[i - 1]
half = A[-1] / 2

A = [abs(i - half) for i in A]
print(int(min(A) * 2))

Submission status: Accepted

Solution code:
from itertools import accumulate
def myAnswer(N:int,A:list) -> int:
   accum = list(accumulate(A))
   ans = 10**10
   now = accum.pop()
   for a in accum:
      if(abs(now - a - a) < ans):
         ans = abs(now - 2*a)
   return ans

   

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

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

def ruiseki(a):
    right, left = [], []
    l, r = 0, 0
    for i in range(n):
        l += a[i]
        left.append(l)
    
        r += a[-(i+1)]
        right.append(r)
    return left, right[::-1]

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
sA = sum(A)
ans = float('inf')
l = [A[0]]
for i in A[1::]:
    l.append(l[-1]+i)
    if sA < 2*l[-1]:
        print(min(abs(sA-2*l[-1]),abs(sA-2*l[-2])))
        exit()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
s = 0
for bar in a:
    if (s + bar) * 2 <= total:
        s += bar
    else:
        ans = min((s + bar) * 2 - total, total - s * 2)
        break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
s = sum(li)
sum_li = []
left = 0
right = s
for i,l in enumerate(li):
    if i == len(li)-1:
        break
    left += l
    right -= l
    sum_li.append(abs(left-right))
print(min(sum_li))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
s = sum(li)
sum_li = []
for i,l in enumerate(li):
    if i == 0:
        sum_li.append(l)
        continue
    sum_li.append(sum_li[-1]+l)
all_li = []
for ii,ss in enumerate(sum_li):
    if ii == len(sum_li)-1:
        break
    all_li.append(abs(s-2*ss))
#print(all_li)
print(min(all_li))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(i) for i in input().split()] 

su = [0 for _ in range(n)] #鉄の棒の切れ目の座標
for i in range(n):
    su[i] = su[i-1] + a[i]
#print(su)

tmp = 2 * pow(10, 10) + 1
for i in su:
    #if abs(i - su[-1]/2) < tmp:
    #    tmp = abs(i - su[-1]/2)
    tmp = min(tmp, abs(i - su[-1]/2))
#print(su[-1]/2, tmp)

"""
#中心に一番近い鉄の棒の切れ目と中心との距離の差
l = 0
r = n-1
while (l - r) >= 1:
    c = (l + r) // 2
    #print(l, r, c)
    if su[c] < su[-1] / 2:
        l = c
    elif su[c] > su[-1] / 2:
        r = c
#print(su[l], su[r])
tmp = min(abs(su[l] - su[-1]/2), abs(su[r] - su[-1]/2))
print(tmp)
"""

ans = 2 * tmp
print(int(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(i) for i in input().split()] 

su = [0 for _ in range(n)] #鉄の棒の切れ目の座標
for i in range(n):
    su[i] = su[i-1] + a[i]
#print(su)

tmp = 2 * pow(10, 10) + 1
for i in su:
    if abs(i - su[-1]/2) < tmp:
        tmp = abs(i - su[-1]/2)
#print(su[-1]/2, tmp)

"""
#中心に一番近い鉄の棒の切れ目と中心との距離の差
l = 0
r = n-1
while (l - r) >= 1:
    c = (l + r) // 2
    #print(l, r, c)
    if su[c] < su[-1] / 2:
        l = c
    elif su[c] > su[-1] / 2:
        r = c
#print(su[l], su[r])
tmp = min(abs(su[l] - su[-1]/2), abs(su[r] - su[-1]/2))
print(tmp)
"""

ans = 2 * tmp
print(int(ans))

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
a = LIST()
arr = [0 for _ in range(n)]
arr[0] = a[0]
for i in range(1, n):
    arr[i] = arr[i - 1] + a[i]
s = arr[-1]
mn = inf
for i in range(n):
    mn = min(mn, abs(s / 2 - arr[i]))
print(int(mn * 2))

Submission status: Accepted

Solution code:
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


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


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


    B = [0] + A
    B = list(accumulate(B))  # 累積和を格納したリスト作成

    # この問題は長さが1-Nの連続部分の和の最大値を出せというものなので以下の通り

    S=B[-1]
    minans = 10 ** 20
    for i in range(1,N):
        val=abs(B[i]-(S-B[i]))
        minans=min(minans,val)
    print(minans)


resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from itertools import accumulate
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    A=list(accumulate(A))
    min_A=100000000000
    for a in A:
        min_A=min(min_A,abs(A[-1]/2-a))
    print(int(min_A*2))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
SUM = sum(a)
cnt = 0
l = []
for i in range(n):
    cnt += a[i]
    l.append(abs(cnt - (SUM - cnt)))
print(min(l))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
countUp = 0
sumA = sum(A)
minA = 2020202020 * N
for x in range(N):
    countUp += A[x]
    sumA -= A[x]
    minA = min(minA, abs(countUp - sumA))
print(minA)

Submission status: Accepted

Solution code:
import itertools

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

reg = 10**12
Acum = list(itertools.accumulate(A))
for i in range(N):
    L = Acum[i]
    R = Acum[-1] - L
    tmp = abs(L-R)
    reg = min(reg, tmp)
print(reg)

Submission status: Accepted

Solution code:
import numpy as np

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

Acum = A.cumsum()
L = Acum[:-1]
R = Acum[-1] - L
print(abs(L-R).min())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools


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

reg=10**12
for i in range(N):
    reg = min(reg,
             abs(Acum[-1] - Acum[i] * 2))
    
print(reg)

Submission status: Accepted

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

ans = sum(a)
ans2 = 0
for i in range(n):
    if abs((ans - a[i]) - (ans2 + a[i])) < abs(ans - ans2):
        ans2 += a[i]
        ans -= a[i]
    else:
        break

print(abs(ans - ans2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
ans = float("INF")
LS = [0]*(N+1)
LS[0] = 0
for i in range(N-1):
  LS[i+1] = A[i] + LS[i]
  ans = min(ans, abs(LS[i+1] - (S - LS[i+1])))
print(ans)

Submission status: Accepted

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

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

m = float("INF")
j = -1
for i in range(1, N+1):
  if abs(c - S[i]) < m:
    m = abs(c - S[i])
    j = i

print(abs((S[N] - S[j]) - S[j]))

Submission status: Accepted

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    r = sum(a)
    l = 0
    ans = r
    for i in a:
        l += i
        r -= i
        ans = min(ans, abs(r - l))
        if l >= r:
            break
    print(ans)

main()

Submission status: Accepted

Solution code:
# B - Iron Bar Cutting
N = int(input())
A = list(map(int, input().split()))

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

for a in A:
	left += a
	right -= a
	
	diff = abs(right - left)
	ans = min(ans, diff)
	
	if left > right:
		break

print(ans)

Submission status: Accepted

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

cumulative = [0]
for i in range(N):
    cumulative.append(cumulative[i] + A[i])
lst = []
for i in range(1, N):
    a = abs(cumulative[i] - (cumulative[-1] - cumulative[i]))
    lst.append(a)
#print(lst)
print(min(lst))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 10**18
total = sum(A)
acc = 0
for a in A:
  acc += a
  ans = min(ans, abs(total - acc * 2))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

A = deque(A)
left = 0
right = 0
while len(A) > 1:
    if (left <= right):
        left += A.popleft()
    else:
        right += A.pop()
if left < right:
    left += A.pop()
else:
    right += A.pop()

print(max(left, right) - min(left, right))

Submission status: Accepted

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

for i in range(n):
    l1 += a[i] 
    if l1> s//2:break
r1 = s - l1
l2 = l1 -a[i]
r2 = s - l2
ans = min(abs(r1-l1),abs(r2-l2))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
sumA = sum(A)
res = sumA
cc = 0
for a in A:
  cc += a
  res = min(res, abs(sumA - 2 * cc))
print(res)

Submission status: Accepted

Solution code:
from numpy import cumsum

N = int(input())
A = list(map(int, input().split()))
a_cum = cumsum(A)
cand = [abs(a_cum[i] - (a_cum[-1] - a_cum[i])) for i in range(N)]
print(min(cand))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

cumsum = np.cumsum(A)

ans = float("inf")
for i in range(1, N):
    left = cumsum[i]
    right = cumsum[-1] - cumsum[i]
    tmp = abs(right - left)
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    left_sum = [0] * N # [0, i]
    right_sum = [0] * N # [i, N-1]

    left_sum[0] = A[0]
    right_sum[-1] = A[-1]

    for i in range(1, N):
        left_sum[i] = left_sum[i-1] + A[i]
        right_sum[(N-1)-i] = right_sum[(N-1)-(i-1)] + A[(N-1)-i]
    
    ans = 10 ** 18
    for i in range(N-1):
        diff = abs(left_sum[i] - right_sum[i+1])
        ans = min(ans, diff)
    
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    left_sum = [0] * N # [0, i]
    right_sum = [0] * N # [i, N-1]

    left_sum[0] = A[0]
    right_sum[-1] = A[-1]

    for i in range(1, N):
        left_sum[i] = left_sum[i-1] + A[i]
        right_sum[(N-1)-i] = right_sum[(N-1)-(i-1)] + A[(N-1)-i]
    
    ans = 10 ** 11
    for i in range(N-1):
        diff = abs(left_sum[i] - right_sum[i+1])
        ans = min(ans, diff)
    
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
num = int(input())
num_list = list(map(int,input().split()))
ans = sum(num_list)/2
#numl = sum(num_list[:num//2])
#numr = sum(num_list[num//2:num])
#print(ans,numl,numr)
total = 0
flag = "off"
for i in range(num):
  total += num_list[i]
  if total >= ans:
    #print(ans)
    if ans -(total - num_list[i]) < total - ans:
      ans = ans -(total - num_list[i])
    else:
      ans = total - ans     
    break
#print(n1,n2)
#print(min((n2-ans),(ans-n1)))
print(math.ceil(ans*2))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import itertools
import bisect

n = int(readline())
a = tuple(map(int, readline().split()))
cumsum = tuple(itertools.accumulate(a))
index = bisect.bisect(cumsum, sum(a) // 2)
ans = min(cumsum[n - 1] - cumsum[index - 1] * 2, cumsum[index] * 2 - cumsum[n - 1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))
right = sum(A)
left = 0
ans = float('inf')

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=float("inf")
s=sum(A)
k=0
for i in range(N-1):
    k+=A[i]
    ans=min(ans,abs(s-2*k))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
import bisect
n = int(input())
A = list(map(int,input().split()))
cs = np.cumsum(A).tolist()
mid = sum(A)//2
idx = bisect.bisect_left(cs, mid)
ans = float('inf')
for i in range(max(1, idx-1), min(idx+2, n)):
    x = A[:i]
    y = A[i:]
    sum_x = sum(x)
    sum_y = sum(y)
    ans = min(ans, abs(sum_x - sum_y))
print(ans)

Submission status: Accepted

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

ca = [0]
for i in range(n):
    ca.append(A[i]+ca[i])

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    acc = list(itertools.accumulate(a))
    ans = inf
    for i in range(n - 1):
        ans = min(ans, abs(acc[n - 1] - acc[i] - acc[i]))
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
nsum = sum(A)
diff = 100000000000000
left = 0
right = sum(A)
for a in A:
  left += a
  right -= a
  diff = min(diff, abs(left-right))
print(diff)

Submission status: Accepted

Solution code:
def main():
    import sys
    import bisect
    from itertools import accumulate

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

    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect(b, l // 2)
    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    from itertools import accumulate

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

    b = list(accumulate(a))
    diff = b[-1]
    total = b[-1]
    for x in b:
        diff = min(diff, abs(total - 2 * x))
    print(diff)

if __name__ == '__main__':
     main()

Submission status: Accepted

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

ans = 10**10
for i in range(n-1):
    if ans > abs(wa[i] - wa2[-(i+2)]):
        ans = abs(wa[i] - wa2[-(i+2)])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
A=[float("inf")]*(N+10)
A[0]=0
for i in range(1,N+1):
	A[i]=A[i-1]+L[i-1]
X=float("inf")
for i in range(len(A)):
	X=min(X,abs(A[N]-A[i]-A[i]))
print(X)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
ans = 10000000000
cum_sum1 = 0
cum_sum2 = sum(A_list)
for i in range(N-1):
    cum_sum1 += A_list[i]
    cum_sum2 -= A_list[i]
    tmp = abs(cum_sum1-cum_sum2)
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

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

sum_l = sum(A)
sum_r = 0
half_abs = 2020202020
for a in A:
    sum_l -= a
    sum_r += a
    tmp_abs = abs(sum_l-sum_r)
    half_abs = min(half_abs, tmp_abs)
print(half_abs)

Submission status: Accepted

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

left = sum(a)
right = 0

mini = float('inf')
for i in a:
    right += i
    left -= i
    tmp = abs(right - left)

    if tmp < mini:
        mini = tmp

print(mini)

Submission status: Accepted

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

m = sum(a) // 2
s = sum(a)
temp = 0
for x in a:
    temp += x
    if temp > m:
        H = temp
        L = temp - x
        break

print(min(H * 2 - s, s - L * 2))

Submission status: Accepted

Solution code:

n = int(input())
a = list(map(int,input().split()))
d = [0]

for i in range(n):
    d.append(d[i] + a[i])

ans = d[-1]

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = A[::-1]
S = [0] * n
S[0] = A[0]
T = [0] * n
T[0] = B[0]
ans = float('inf')
for i in range(1, n):
    S[i] = S[i - 1] + A[i]
    T[i] = T[i - 1] + B[i]
T = T[::-1]
ans = 2020202020 + 1
for i in range(n):
    diff = abs(S[i] - T[i] - A[i])
    ans = min(ans, diff)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
A = LMI()

ans = INF
accum = list(accumulate(A))
for s in accum:
    t = accum[-1] - s
    ans = min(ans, abs(s - t))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
su=0
mi=20000000000000000000
for i in range(n):
  su+=a[i]
  
c=0  
for i in range(n):
  c+=a[i]
  left=su-c
  mi=min(mi,max((c-left),(left-c)))

print(mi)

Submission status: Accepted

Solution code:
# coding: utf-8

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

bar = list(map(int,input().rstrip().split(" ")))

cur = 0

total = sum(bar)
ans = total
for i in bar:
    cur += i
    ans = min(ans, abs((total-cur) - cur))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=[A[0]] #累積和
C=[0]*N #ここで切った時の差分
for i in range(1,N):
  B.append(B[-1]+A[i])
  
for j in range(N):
  C[j]=abs((B[-1]-B[j])-B[j])

#print(C)
print(min(C))

Submission status: Accepted

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

total = sum(A)
tmp = 0
diff = total
for a in A:
    tmp += a
    diff = min(diff, abs(tmp - (total - tmp)))

print (diff)

Submission status: Accepted

Solution code:
from itertools import accumulate


def main():
    section_num = int(input())
    length = list(map(int, input().split()))
    accumulate_length = list(accumulate(length))
    answer = float("inf")
    for i in range(0, section_num):
        answer = min(answer, abs(accumulate_length[-1] - 2 * accumulate_length[i]))
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# Problem B - Iron Bar cutting

# input process
N = int(input())
a_list = list(map(int, input().split()))

# initialization
min_cost = 10**18
left_sum = 0
right_sum = sum(a_list)

# count process cumulative sum
for i in range(N-1):
    left_sum += a_list[i]
    right_sum -= a_list[i]
    distance = abs(left_sum - right_sum)
    min_cost = min(min_cost, distance)

# output process
print(min_cost)

Submission status: Accepted

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

# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np


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

	ans=10**18
	cnt=0

	Asum=sum(A)
	for i in range(n):
		cnt+=A[i]
		ans=min(ans,abs(Asum-2*cnt))
	print(ans)









if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# サンプル1はべつに3*2=6じゃなくても4*2=8（[1 3 4][2 2 4]）や5*1=10（[1 4 5]）も同じコストで実現できることがわかる
# つまり総和に近い偶数を目指す
n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
tmp = arr[0]
ans = 10**10
e = -1
for i in range(1, n):
    # 超えないギリギリ
    if tmp + arr[i] <= s // 2:
        tmp += arr[i]
        e = i
    else:
        break
c = abs(s // 2 - tmp) + abs(s // 2 - sum(arr[i:]))
# print("超えない左側の合計と右側の合計 : {}, {}".format(tmp, sum(arr[i:])))
ans = min(ans, c)

e = -1
tmp = arr[0]
for i in range(1, n):
    # 超えたギリギリ
    if tmp > s // 2:
        break
    else:
        tmp += arr[i]
        e = i
c = abs(s // 2 - tmp) + abs(s // 2 - sum(arr[i:]))
# print("超えたギリギリの左側の合計と右側の合計 : {}, {}".format(tmp, sum(arr[i:])))
ans = min(ans, c)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
L=list(map(int,input().split()))
sumL=[0 for _ in range(n)]
S=sum(L)
for i in range(n):
    if i==0:
        sumL[i]=L[i]
    else:
        sumL[i]=sumL[i-1]+L[i]
ans=2020202020
tmp1=0
tmp2=0
for i in range(n):
    tmp1=sumL[i]
    tmp2=S-sumL[i]
    ans=min(ans,abs(tmp1-tmp2))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=0
all=sum(A)
buf=all
A1=[]
A2=[]
s1=0
s2=0
c=float("inf")
for i in range(N-1):
    buf=buf-A[i]
    if c>abs((all-buf)-buf):
        c=abs((all-buf)-buf)
    else:
        break
print(c)

Submission status: Accepted

Solution code:
import math
from collections import deque
def main():
    n = int(input())
    a = [int(t)for t in input().split()]
    a_rev = a[::-1]

    cusum = [a[0]]
    rcusum = [a_rev[0]]

    for i in range(1,n):
        cusum.append(cusum[i-1]+a[i])
        rcusum.append(rcusum[i-1]+a_rev[i])
    
    rcusum = rcusum[::-1]

    result = []

    for i in range(1,n):
        result.append(abs(cusum[i-1]-rcusum[i]))

    print(min(result))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)/2
Es = []
tmp = 0
for i in range(N):
    tmp += A[i]
    Es.append(abs(tmp-S))
cut_point = Es.index(min(Es))

ans1 = abs(sum(A[cut_point:])-sum(A[:cut_point]))
ans2 = abs(sum(A[cut_point+1:])-sum(A[:cut_point+1]))
ans = min(ans1,ans2)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
ans = float("inf")
S = sum(Alist)
from itertools import accumulate
R = list(accumulate(Alist))
for r in R:
    ans = min(ans, abs(r - (S-r)))
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:
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
t = s // 2
count = 0
for a in A:
  count += a
  if count >= t:
    n = count
    m = count - a
    break
print(min(2*n - s, s - 2*m))

Submission status: Accepted

Solution code:
from itertools import accumulate
N,*A=map(int, open(0).read().split())
B=list(accumulate(A))
C=sum(A)
ans = float("inf")
for b in B[:-1]:
    ans=min(ans,max(b,C-b)-min(b,C-b))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
t = sum(dat)
target = t//2
c = 0
c2 = t
res = 99999999999999999
for i in range(n):
    c += dat[i]
    c2 -= dat[i]
    res = min(res, abs(c-c2))

print(res)

Submission status: Accepted

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

s = sum(A)

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

ans = 10 ** 20

for i in range(N):
    ans = min(ans, abs(s - 2 * A[i]))

print(ans)

Submission status: Accepted

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

len_f = sum(a)
sum_a = 0
min = 2020202021

for i in range(n):
    sum_a += a[i]
    len_f -= a[i]
    if min > abs(sum_a - len_f):
        min = abs(sum_a - len_f)

print(min)