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: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a = l = list(map(int, stdin.readline().rstrip().split()))

lf = 0
lb = 0
while len(a) > 0:
    if lf <= lb:
        lf += a.pop(0)
    else:
        lb += a.pop(len(a)-1)
h = 0
if lf <= lb:
    h = lb-lf
else:
    h = lf-lb
print(h)

Submission status: Time Limit Exceeded

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

total = sum(A)

barlen = total/2

costs = [0 for x in range(N-1)]
for i in range(1, N):
#  print(abs(sum(A[:i]) - sum(A[i:])))
  costs[i-1] = abs(sum(A[:i]) - sum(A[i:]))
  
print(min(costs))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

bar1 = []
bar2 = []
diff = []
for i in range(1, N):
    bar1.append(sum(A[:i]))
    bar2.append(sum(A[i:]))
    diff.append(abs(sum(A[:i]) - sum(A[i:])))
print(min(diff))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Ai = [int(i) for i in input().split()]

leftSum = 0
rightSum = 0


for i in range(0,N):
    if(leftSum < rightSum):
        leftSum = leftSum + Ai.pop(0)
    else:
        rightSum = rightSum + Ai.pop(len(Ai)-1)
ans = abs(leftSum-rightSum)
print(ans)

Submission status: Time Limit Exceeded

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

Asum = sum(A)

sumdiflist = []

for i in range(N-1):
  B = A[:i+1]
  Bsum = sum(B) #前半の和
  temp = Asum - Bsum #後半の和
  sumdif = Bsum - temp
  if(sumdif<0):
    sumdif = -sumdif
  sumdiflist.append(sumdif)

print(min(sumdiflist))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
balance = []
for i in range(n):
  balance.append(abs(sum(L[0:i]) - sum(L[i:n])))

k = balance.index(min(balance))
print(abs(sum(L[0:k]) - sum(L[k:n])))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = np.array(list(map(int,input().split())), dtype=np.int64)
print(min(abs(sum(A[0:i+1])-sum(A[i+1:n])) for i in range(n-1)))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())  #数値入力 「N」だけの入力のとき
A=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

def check():
    for i in range(N):
        if sum(A[:i]) == sum(A[i:]):
            return True
    return False

def check_distance():
    ret = []
    for i in range(1, N):
        tmp_a = abs(sum(A[:i]) - sum(A[i:]))
        ret.append(tmp_a)
        # print(f"at {i}, abs:{tmp_a}")
    return min(ret)

if check():
    print(0)
else:
    print(check_distance())

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
su=sum(a)
c=[]#[abs(sum(a[:i+1])-su/2)*2 for i in range(n)]

for i in range(n):
  c+=[abs(sum(a[:i+1])-su/2)*2]
  if sum(a[:i+1])-su/2 >0 or sum(a[:i+1])-su/2==0:
    break
print(int(min(c)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def Solve (N, A):

    l = []
    for i in range(1, N):
        l.append(abs(sum(A[:i]) - sum(A[i:])))
    return min(l)

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

Submission status: Time Limit Exceeded

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

lr = "l"

L = 0
R = 0

while A:
    if lr == "l":
        L += A[0]
        del A[0]
        if L > R:
            lr = "r"
    else:
        R += A[-1]
        del A[-1]
        if L <= R:
            lr = "l"


print(abs(L - R))

Submission status: Time Limit Exceeded

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

minimum = sum(A)
for i, l in enumerate(A):
  a = sum(A[:i])
  b = sum(A[i:])
  minimum = min(minimum, abs(a-b))
print(minimum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n-1):
    zen = sum(a[:i+1])
    kou = sum(a[i+1:])
    value = abs(zen - kou)
    if i == 0:
        ans = value
    if value < ans:
        ans = value
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_s = list(map(int, input().split()))
sum_a_s = sum(a_s)

for i in range(n):
	tmp_sum = sum(a_s[:i])
	if tmp_sum >= sum_a_s - tmp_sum:
		break

tmp_res1 = abs(sum(a_s[:i - 1]) * 2 - sum_a_s)
tmp_res2 = abs(sum(a_s[:i]) * 2 - sum_a_s)
res = min(tmp_res1, tmp_res2)

print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
x = 2020202020
for i in range(1, n):
    x = min(x, abs(sum(a[:i]) - sum(a[i:])))
print(x)

Submission status: Time Limit Exceeded

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

mini = 2020202020 * 200000
for j in range(1,N):
    F, B = A_list[:j], A_list[j:]
    delta = abs(sum(F) - sum(B))
    if delta <= mini:
        mini = delta

print(mini)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=float('inf')
for i in range(1,n):
    s1=sum(a[0:i])
    s2=sum(a[i:])
    ans=min(ans,abs(s1-s2))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 100000000000000
for i in range(n):
  if sum(a[:i]) == sum(a[i:]):
    ans = 0
    break
  elif ans > abs(sum(a[:i]) - sum(a[i:])):
    ans = abs(sum(a[:i]) - sum(a[i:]))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))
x = 0
y = 0
for i in range(0, n):
    if x <= y:
        x += a.pop(0)
    else:
        y += a.pop(-1)
print(abs(x - y))

Submission status: Time Limit Exceeded

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

ans = 217937901270479417
for i in range(1,len(a)):
    ans = min(ans, abs(sum(a[:i]) - sum(a[i:])))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
h = list(map(int, input().split()))
b = []
for i in range(len(h)):
    b.append(abs(sum(h[0:i]) - sum(h[i:len(h)])))
print(min(b))

Submission status: Time Limit Exceeded

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

for i in range(n):
	if sum(a_s[:i]) >= sum(a_s[i:]):
		break

tmp_res1 = abs(sum(a_s[:i - 1]) - sum(a_s[i - 1:]))
tmp_res2 = abs(sum(a_s[:i]) - sum(a_s[i:]))
res = min(tmp_res1, tmp_res2)

print(res)

Submission status: Time Limit Exceeded

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

half=[]
for i in range(N):
  half.append(abs(sum(A[:i])-sum(A[i:])))
print(min(half))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
tmp = [a[0]]
for i in range(1, n):
  if sum(tmp) > sum(a) / 2:
    break
  tmp.append(a[i])
print(min(sum(tmp) * 2 - sum(a), sum(a) - sum(tmp[:-1]) * 2))

Submission status: Time Limit Exceeded

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

ans, sumA2 = abs(sum(A) - 0), 0
for i in range(N):
    sumA2 += A[i]
    ans = min(ans, abs(sum(A) - sumA2 * 2))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
c = 0
for i in range(n):
    if (c + a[i] >= sum(a)/2):
        d = i
        break
    c += a[i]
print(min(sum(a)-2*c,2*(c+a[d])-sum(a)))

Submission status: Time Limit Exceeded

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

left_side = 0
right_side = 0
ans = 1e12

for i in range(1, N):
    left_side = sum(A[:i])
    right_side = sum(A[i:])
    ans = min(ans, abs(left_side-right_side))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = float('inf')
for i in range(n-1):
    zen = sum(a[:i+1])
    kou = sum(a[i+1:])
    value = abs(zen - kou)
    if value < ans:
        ans = value
print(ans)

Submission status: Time Limit Exceeded

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


sa=float('inf')
for i in range(n):
  sa=min(sa,abs(sum(A[:i])-sum(A[i:])))
print(sa)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

cand = []
for i in range(1, N):
    diff = abs(sum(A[:i]) - sum(A[i:]))
    cand.append(diff)
print(min(cand))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
tmp=10**20
for i in range(1,n):
  tmp=min(tmp,abs(sum(a[:i])-sum(a[i:])))
print(tmp)

Submission status: Time Limit Exceeded

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

m = 2020202020
for i in range(N):
  a = sum(A[0:i])
  b = sum(A[i:])
  ab = abs(a-b)
  if ab < m:
    m = ab
print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
tmp = 0
key = 0
for i in range(n):
    if tmp + a[i] >= sum(a) / 2:
        key = i
        break
    tmp += a[i]

ans = abs(sum(a[0:key]) - sum(a[key::]))
tmp = 0
key = 0
for i in range(n):
    tmp += a[i]
    if tmp >= sum(a) / 2:
        key = i + 1
        break

ans = min(abs(sum(a[0:key]) - sum(a[key::])), ans)
print(ans)

Submission status: Time Limit Exceeded

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

sa=float("inf")

for i in range(n-1):
  wa1=sum(a[0:1+i])
  wa2=sum(a[1+i:])
  temp=abs(wa1-wa2)
  sa=min(sa,temp)
print(sa)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
diff = []
for i in range(1, N):
    diff.append(abs(sum(A[:i]) - sum(A[i:])))
print(min(diff))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
sum1 = []
sum2 = []
s = sum(a)
ls = []
for i in range(1,n):
    sum1.append(sum(a[:i]))
    sum2.append(s - sum(a[:i]))

for i in range(n-1):
    ls.append(abs(sum1[i] - sum2[i]))

print(min(ls))

Submission status: Time Limit Exceeded

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

aa = [0]*(n+1)

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

ans = sum(a)
for i in range(n):
    ans = min(ans,abs(sum(a)-2*aa[i]))

print(ans)

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

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

if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

for i,a in enumerate(A,1):
  L[i] = L[i-1] + a
for i in range(N):
  D[i] = L[-1]-L[i]
#L.pop(0)
  
#print(L)
#print(D)
ans = 1e19
for i in range(N):
  #print(L[i],D[i],L[i]-D[i])
  ans = min(abs(L[i]-D[i]),ans)
print(ans)

Submission status: Accepted

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

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

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

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

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,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    n=I()
    lis=LI()
    sm=list(accumulate(lis))
    u=sum(lis)
    ans=inf
    for i in range(n):
        ans=min(ans,abs(u-sm[i]-sm[i]))
    print(ans)
        
        



        
        
        
        
        
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


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


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


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


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

    return


if __name__ == "__main__":
    main()

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)
half = s / 2

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(mindif)

Submission status: Accepted

Solution code:
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()
# row = [int(input().rstrip()) for _ in range(n)]

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

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

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

    # できるだけ中心に近い切れ目
    center_cut = 0
    cut = 0
    for a in a_list:
        cut += a
        if abs(center_cut - center) > abs(cut - center):
            center_cut = cut
        if cut > center:
            break

    # 中心に近い切れ目を中心に一致させる
    print(int(abs(center_cut - center) * 2))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

mini = float('inf')

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

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

print(mini)

Submission status: Accepted

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

sumA = sum(A)

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

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

Submission status: Accepted

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

# 差が最小の切れ目を見つける
length = sum(A)
left = 0
dist = 0
mdist = length
midx = 0
for i,a in enumerate(A):
    left += a
    right = length - left
    dist = abs(left - right)
    if dist < mdist:
        mdist = dist
        midx = i
ans = mdist
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

from itertools import accumulate

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

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

print(ans)

Submission status: Accepted

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

from itertools import accumulate

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

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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


def main():
    from itertools import accumulate

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

av = s / 2
x = 0
y = 0

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

Submission status: Accepted

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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


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

    print(m)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(mm)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

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

import numpy as np

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

	A=np.array(A)

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

	#print(B,C)

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

	print(np.min(D))









if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from itertools import accumulate


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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

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

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

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
total = sum(A)
mi = float('inf')
left = 0
for i in range(n):
    left += A[i]
    total -=A[i]
    mi = min(mi, abs(left - total))
print(mi)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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