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()))
ans = 0
s = sum(a)
al = []

sum1 = 0

for i in range(0,n):
        # if i == 1:
        #         sum1 = sum(a[0:i])
        # else:
        #         sum1 += a[i]
        sum1 += a[i]
        sum2 = s - sum1
        # print("sum1:"+str(sum1))
        # print("sum2:"+str(sum2))
        al.append(abs(sum1 - sum2))
        if sum1 == sum2:
                print(0)
                exit()

minDiffIdx = al.index(min(al))

# print("---")
# print(minDiffIdx)
# print("---")

tmp1 = sum(a[0:minDiffIdx])
tmp2 = sum(a[minDiffIdx:])
# print(tmp1)
# print(tmp2)
diff1 = abs(tmp1 - tmp2)

tmp3 = sum(a[0:minDiffIdx + 1])
tmp4 = sum(a[minDiffIdx + 1:])
# print(tmp3)
# print(tmp4)
diff2 = abs(tmp3 - tmp4)
print(min(diff1, diff2))

# d1 = diff1
# d2 = diff2

# if d1 < d2:
#         for i in range(n//2):
#                 if a[i] > 1:
#                         d1 -= a[i]
#                 if d1 < 0:
#                         ans += diff1 + d1
#                         break
#                 elif d1 == 0:
#                         ans += diff1
#                         break
#         if 
# else:

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
ans = 0

s = sum(a)

a_ = np.cumsum(a)
diff = []
for i in range(n):
        diff.append(abs(a_[i] - (s - a_[i])))
print(min(diff))

Submission status: Accepted

Solution code:
n = int(input())
l = list(input().split())
s = sum(int(i) for i in l)
m0 = 0
m1 = 0
c = 0
for i in l:
    m1 = m0
    m0 += int(i)
    if m0 * 2 == s:
        break
    elif m0 * 2 > s:
        c = min(m0 *2 -s, s - m1 * 2)
        break
        
print(c)

Submission status: Accepted

Solution code:
import sys

n_kukann = int(input())
kukann_nagasa = list(map(int, input().split()))
all_length=0
some=0
i=0
remaining=0
#print(n_kukann//2)
#N個の空のリストを作成
for i in range(n_kukann):
 all_length= all_length + kukann_nagasa[i]

abss = [all_length]*n_kukann
#print(n_kukann)
#print(all_length)
#if n_%2==0:
  # for i in range(n_kukann):
#        some=some +kukann_nagasa[i]
#        if some == all_length/2:
#           print(0)
#           sys.exit
#        else:
#            pass

#else:
for i in range(n_kukann):
    some=some +kukann_nagasa[i]
    remaining=all_length-some
    abss[i]=abs(some-remaining)
    if some == remaining:
      
       break
       sys.exit

    #elif some >= remaining:
       #print(some)
       #print(all_length-some)
       #print(min(some-all_length/2,-(all_length/2-some) ))
      # break
     #  sys.exit

    else:
        pass

#print(abss)
abss.sort()
print(abss[0])
#print(all_length)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

acc = list(accumulate(a))
ans = 10**30

mx = acc.pop()
for b in acc:
    if abs(mx - b - b) < ans:
        ans = abs(mx - 2*b)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
li_a = list(reversed(li))
from collections import deque
a = 0
b = sum(li)
while a < b//2:
  s = li.pop()
  a += s
c = sum(li)

d = 0
e = sum(li_a)
while d < e//2:
  t = li_a.pop()
  d += t
f = sum(li_a)
print(min(a-c, d-f))

Submission status: Accepted

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

left_to_me = [0]*(N+1)
for i, a in enumerate(A):
    left_to_me[i+1] = left_to_me[i]+a

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

min_cost = 10**24
for i, a in enumerate(A):
    target = abs(left_to_me[i]-right_to_me[i])
    cost = abs(target-a)
    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Accepted

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


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


n = I()
A = list(accumulate(LI()))
total = A[-1]
ret = INF
for i in range(n - 1):
    ret = min(ret, abs(total - A[i] -  A[i]))


print(ret)

Submission status: Accepted

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

part_sum = 0

for i, a in enumerate(A):
    part_sum += a
    if part_sum >= all_sum // 2:
        price = part_sum - (all_sum - part_sum)
        break
part_sum = 0

for i, a in enumerate(reversed(A)):
    part_sum += a
    if part_sum >= all_sum // 2:
        price = min(price, part_sum - (all_sum - part_sum))
        break
print(price)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int(input())
A = [int(x)*2 for x in input().split()]
L = [0] * (N+1)
for i in range(N):
    L[i+1] = L[i] + A[i]
mid = L[N] // 2
midL = bisect_left(L, mid)
l, r = L[midL-1:midL+1]
print(min(mid-l, r-mid))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_,a=open(0)
*a,=map(int,a.split())
s=m=sum(a)
c=0
for b in a:c+=b;m=min(m,abs(c+c-s))
print(m)

Submission status: Accepted

Solution code:
_,a=open(0)
*a,=map(int,a.split())
s=sum(a)
c=0
m=9e9
for b in a:c+=b;m=min(m,abs(c+c-s))
print(m)

Submission status: Accepted

Solution code:
_,a=open(0)
*a,=map(int,a.split())
s=sum(a)
c=0
m=[]
for b in a:c+=b;m+=abs(c+c-s),
print(min(m))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
s=sum(a)
c=0
m=[]
for b in a:c+=b;m+=abs(c+c-s),
print(min(m))

Submission status: Accepted

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

def solve():
    s = sum(A)
    ans = s
    a = 0
    for i in range(N-1):
        a += A[i]
        b = s - a
        ans = min(ans, abs(a - b))
    
    return ans

print(solve())

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
sm = sum(As[1:])
p = As[0]
t = abs(sm-p)
for a in As[1:-1]:
  p+=a
  sm-=a
  if t < abs(sm-p):
    break
  t = abs(sm-p)
print(t)

Submission status: Accepted

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

s = sum(A)
cnt = 0
res = sum(A)
for a in A:
    cnt += a
    res = min(res, abs(s-cnt*2))
    if res == 0:
        break

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

maekara = []
atokara = []

temp = 0
for i in range(n):
    temp += a[i]
    maekara.append(temp)
temp = 0
for i in range(n):
    temp += a[n-1-i]
    atokara.append(temp)
atokara = atokara[::-1]

ans = 2020202020 * 200000
for i in range(n-1):
    ans = min(ans, abs(maekara[i]-atokara[i+1]))

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
 
N=int(input())
A = np.array([int(x) for x in input().split()])
B = A.cumsum()

L=B
R=B[-1]-L

answer=np.abs(L-R).min()

print(answer)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
from bisect import bisect_left

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

b = list(accumulate(a))

mi = bisect_left(b, b[N-1]/2)
m = max(min(b[mi-1], b[N-1] - b[mi-1]), min(b[mi],b[N-1] - b[mi]))
ans = b[N-1] - 2 * m
print(ans)

Submission status: Accepted

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

# 累積和
Asum = [0]*N
Asum[0] = A[0]
for i in range(1,N):
    Asum[i] += Asum[i-1] + A[i]

answer = float("inf")
for i in range(N):
    answer = min(answer, abs(Asum[-1]-Asum[i]*2))

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
Sum = 0
for s in input().split():
    s = int(s)
    Sum += s
    A.append(Sum)
mid = A[-1]/2
ans = 2*min(abs(A[i]-mid) for i in range(N+1))
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left

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

    B = list(accumulate(A))
    ans = float('inf')
    for i in range(1,N):
        C = B[i] - B[0]
        D = B[N] - B[i]
        tmp = abs(C - D)
        ans = min(ans, tmp)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())), dtype='int')
s = A.sum()
i = -1
left_length = 0
while left_length < s / 2:
    i += 1
    left_length += A[i]
# print(i, left_length, s)
ans1 = 2 * left_length - s
# print(ans1)
left_length -= A[i]
ans2 = s - 2 * left_length
# print(ans2)
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
def b_iron_bar_cutting():
    N = int(input())
    A = [int(i) for i in input().split()]
    first = sum(A)
    second = 0
    ans = float('inf')
    for i in range(N):
        first -= A[i]
        second += A[i]
        ans = min(ans, abs(first - second))
    return ans

print(b_iron_bar_cutting())

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
s = sum(a)
t = i = 0
m = s
while i < n - 1 and t * 2 < s:
	t += a[i]
	i += 1
	m = min(m, abs(t * 2 - s))
print(m)

Submission status: Accepted

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

W = [0] * (N + 1)

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

x = W[-1] // 2
l = 1
r = N
while l + 1 < r:
    m = (l + r) // 2
    if W[m] < x:
        l = m
    else:
        r = m
print(min(abs(W[-1] - W[l] - W[l]), abs(W[-1] - W[r] - W[r])))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
s=sum(a)
for i in range(1,n):a[i]+=a[i-1]
print(min(abs(t*2-s)for t in a))

Submission status: Accepted

Solution code:
import sys

lines = sys.stdin.readlines()
N = int(lines[0])

stdin = list(map(int,lines[1].split(' ')))

lst = []
S1, S2 = 0, sum(stdin)
for i, a in enumerate(stdin):
    S1 += a
    S2 -= a
    lst.append(abs(S1-S2))

print(min(lst))

Submission status: Accepted

Solution code:
import sys
import numpy as np

lines = sys.stdin.readlines()
N = int(lines[0])

stdin = list(map(int,lines[1].split(' ')))

lst = []
S1, S2 = 0, sum(stdin)
for i, a in enumerate(stdin):
    S1 += a
    S2 -= a
    lst.append(abs(S1-S2))

print(min(lst))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
asum2=sum(a)/2
j,k=0,0
for i in range(n):
  j+=a[i]
  if j>=asum2:
    break
for i in range(n):
  k+=a[n-i-1]
  if k>=asum2:
    break
if j-asum2<k-asum2:
  print(int(2*j-2*asum2))
else:
  print(int(2*k-2*asum2))

Submission status: Accepted

Solution code:
n = int(input())

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

sum_A = sum(A)

A_wa = [0]
A_nokori = [sum_A]

for i in range(n) :
    A_wa.append(A_wa[i] + A[i])
    A_nokori.append(A_nokori[i] - A[i])

dif_A = []

for i in range(n) :
    dif_A.append(abs(A_nokori[i] - A_wa[i]))

print(min(dif_A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

n = map(int,input().split())
a = list(map(int,input().split()))
a_sum = sum(a)
kyoukaisen = a_sum /2
left = 0

for i in range(len(a)):
    left += a[i]
    
    if left >= kyoukaisen:
        print(min(abs(a_sum - 2*left),abs( (a_sum - (left-a[i])) - (a_sum-(a_sum - (left-a[i])))) ))
        break

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
L, R, df = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if df < 0:
        L += 1
        df += A[L]
    else:
        R -= 1
        df -= A[R]
print(abs(df))

Submission status: Accepted

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

S1, S2 = 0, sum(A)
L = []
for i in range(N):
  L.append(abs(S1 - S2))
  S1, S2 = S1 + A[i], S2 - A[i]
print(min(L))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

L = sum(a)

nagasa = 0

ans = 0

i=0

for i in range(n):
  nagasa += a[i]
  if nagasa*2 >= L:
    ans = min(abs(L - (nagasa *2)), abs(L - ((nagasa-a[i])*2)))

    break
    

print(ans)

Submission status: Accepted

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

s = sum(A)
B = [0]
for i in range(N):
    B.append(B[i] + A[i])

ans = float('inf')
for i in range(1, N):
    _ans = ans
    ans = min(ans, abs(B[i] - (s - B[i])))
    if ans > _ans:
        break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(A)
B = [0]
for i in range(N):
    B.append(B[i] + A[i])

ans = float('inf')
for i in range(1, N):
    ans = min(ans, abs(s-(2*B[i])))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
r = sum(a)
ra = 0
result = 1e30
for i in range(0,N):
    ra += a[i]
    r -= a[i]
    result = min(result, abs(ra-r))
print(int(result))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

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

wa = sum(a_list)

closest = np.inf

tmp = 0

for i in range(n):
  tmp += a_list[i]
  if np.abs(tmp * 2 - wa) < closest:
    closest = np.abs(tmp*2 -wa)
    
print(int(closest))

Submission status: Accepted

Solution code:
import sys


def main():
    n, *a = map(int, sys.stdin.read().split())

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

    print(m)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def main():
    n, *a = map(int, sys.stdin.read().split())

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate

n = int(input())
a = map(int, input().split())
acc = tuple(accumulate(a))

ret = min(abs(acc[-1] - x * 2) for x in acc)
print(ret)

Submission status: Accepted

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

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

l = sum(A)
print(min(abs(p + p - l) for p in a))

Submission status: Accepted

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

Submission status: Accepted

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

import sys
from bisect import bisect_left
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = [0] + LIST()

acc = list(accumulate(A))
mid = sum(A) / 2
l = bisect_left(acc, mid) - 1
r = bisect_left(acc, mid)
l = acc[l]
r = acc[r]
print(int(min(mid-l, r-mid)*2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
ans = 0
right = sum(A)
left = 0
for i in range(len(A)):
  right -= A[i]
  left += A[i]
  if left > right:
    break
ans1 = abs(left - right)
ans2 = abs((right + A[i]) -  (left - A[i]))

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n=int(input())
alist=list(map(int,input().split()))
blist=[alist[0]]
for i in range(1,n):
	blist.append(blist[-1]+alist[i]) #累積和
alist.reverse()
clist=[alist[0]]
for i in range(1,n):
	clist.append(clist[-1]+alist[i])
clist.reverse()
dlist=[]
for i in range(1,n):
	dlist.append(abs(blist[i-1]-clist[i]))
ans=min(dlist)
print(int(ans))

# hanbun=blist[-1]/2
# shirushi=0
# l=0
# u=n-1
# while u>l+1:
	# aida=(l+u)//2
	# if blist[aida]==hanbun:
		# index=aida
		# shirushi=1
		# break
	# elif blist[aida]>hanbun:
		# l=aida
	# elif blist[aida]<hanbun:
		# u=aida

# if shirushi==1 and blist[-1]%2==0:
	# print(0)
# else:
	# if shirushi==0:
		# if blist[u]-hanbun>hanbun-blist[l]:
			# index=l
		# else:
			# index=u

	# ans=int(2*blist[index]-blist[-1])
	# if ans<0:
		# ans=int(ans*(-1))
	# print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
l = sum(a)
l2 = l / 2
tmp = 0
err = [l,l]
errindex = [0,0]
ans = 0
for i in range(n):
    tmp += a[i]
    if err[0] > l2 - tmp and l2 - tmp >= 0:
        err[0] = l2 - tmp
    if err[1] > tmp - l2 and l2 - tmp <= 0:
        err[1] = tmp - l2

tmp2 = min(err)
tmp3 = int(tmp2)
ans = tmp3 * 2 #+1,-1をそれぞれする回数
if tmp2 - tmp3 > 0:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
sum_A = sum(A)
sum_a = 0
ans = 0
if n == 2:
    print(abs(A[0]-A[1]))
else:
    for a in A:
        if (sum_a+a)*2 <= sum_A:
            sum_a += a
        else:
            ans = min(abs(sum_A - 2*sum_a),abs(sum_A - 2*(sum_a+a)))
            print(ans)
            break

Submission status: Accepted

Solution code:
import sys,itertools
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
l = list(map(int, input().split()))
t = 0

for i in range(N):
    k = l[i]
    t += k

t_2 = t/2
pre = 0
A = 0
for i in range(N):
    A += l[i]
    d = A-t_2
    if d>0:
        if d>abs(pre):
            A -= l[i]
        break
    pre = d
print(abs(t-2*A))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))
 
calculator = 0
pluc_index = 0
minus_index = -1
for i in range(N):
    if calculator <= 0:
        calculator += A_list[pluc_index]
        pluc_index += 1
    else:
        calculator -= A_list[minus_index]
        minus_index -= 1
print(abs(calculator))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))
 
calculator = 0
pluc_index = 0
minus_index = -1
for i in range(N):
    if calculator <= 0:
        calculator += A_list[pluc_index]
        pluc_index += 1
    else:
        calculator -= A_list[minus_index]
        minus_index -= 1
if calculator<0:
  print(-calculator)
else:
  print(calculator)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
previous = 0
following = sum(A)
ans = float("inf")
for a in A:
    previous += a
    following -= a
    tmp_ans = abs(previous - following)
    if ans > tmp_ans:
        ans = tmp_ans
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

left = 0
A_sum = sum(A_list)
ans = A_sum
for a in A_list:
		left += a
		right = A_sum - left
		ans = min(ans, abs(left-right))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
i=ans1=0
while True:
    if ans1<x/2 and i<n:
        ans1+=a[i]
        i+=1
    else:
        break

i-=1
ans2=x-ans1
ans1-=a[i]
print(a[i]-abs(ans1-ans2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
def print_ans(N, ar):
    """Test Case
    >>> print_ans(3, [2, 4, 3])
    3
    >>> print_ans(12, [100, 104, 102, 105, 103, 103, 101, 105, 104, 102, 104, 101])
    0
    """
    ar = np.array(ar)
    sum_ar = np.sum(ar)
    br = np.add.accumulate(ar)
    cr = br - sum_ar / 2
    dr = abs(cr)
    # er = np.where(dr == dr.min())
    er = np.argmin(dr)
    if dr[er] == 0:
        print("0")
    else:
        left = np.sum(ar[:er + 1])
        right = np.sum(ar[er + 1:])
        print(abs(left - right))

if __name__ == '__main__':
    N = int(input().rstrip())
    ar = list(map(int, input().rstrip().split()))
    print_ans(N, ar)

Submission status: Accepted

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

left= array[0]
right = sum(array) - left

prev = 0
for i in range(0,n-1):
    diff = abs(left - right)
    left = left + array[i+1]
    right = right - array[i+1]

    if(i == 0):
        prev = diff
    else:
        if diff < prev:
            prev = diff
print(prev)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ディスカバリーチャンネルコードコンテスト2020 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
s = sum(a)
l = list(accumulate(a))
m = inf
for i in range(n):
    m = min(m,abs(l[i] - (l[-1]-l[i])))
print(m)

Submission status: Accepted

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

s = sum(A)
i = 0
half = s / 2
neibor = 0
for i in range(n+1):
	if neibor >= half:
		break
	else:
		neibor += A[i]
		
aneibor = neibor - A[i-1]
if abs(s-aneibor*2) < abs(neibor-(s-neibor)):
	print(abs(s-aneibor*2))
else:
	print(abs(neibor-(s-neibor)))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
Asum = sum(A)
Al = 0
ans = Asum
for a in A:
    Al += a
    Ar = Asum-Al
    ans = min(ans,abs(Ar-Al))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
total = sum(A)
center_index = 0
counter = 0
g = total / 2
for i, a in enumerate(A):
  counter += a
  if counter >= g:
    center_index = i
    break
#print(center_index)

left_can_minus = sum(A[:center_index]) - center_index
right_can_minus = sum(A[center_index+1:]) - (n - center_index - 1)
right = counter - g
left = g - (counter - A[center_index])
#print(right, left)
right_cost = 0
left_cost = 0
right_cost += min(left_can_minus, int(right)) * 2
right -= min(left_can_minus, int(right))
right_cost += right * 2
left_cost += min(left_can_minus, int(left)) * 2
left -= min(left_can_minus, int(left))
left_cost += left * 2
print(int(min(right_cost, left_cost)))

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]
c = a[-1]
for i in range(n):
    p = abs(a[-1]-a[i]*2)
    if c > p:
        c = p   
print(c)

Submission status: Accepted

Solution code:
import sys
    
sys.setrecursionlimit(200000)
    
N = int(input())
A = list(map(int, input().split(' ')))
    

sumA = []
for i in range(N):
    if i == 0:
        sumA.append(A[i])
    else:
        sumA.append(sumA[-1] + A[i])
    
ans = 0
    
for i in range(N):
    if sumA[-1] // 2 > sumA[i]:
        continue
    else:
        if i == 0:
            ans = abs(sumA[i] * 2 - sumA[-1])
            break
        else:
            ans = min(abs(sumA[i] * 2 - sumA[-1]), abs(sumA[i-1] * 2 - sumA[-1]))
            break
    
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from itertools import accumulate
from bisect import bisect
n = int(input())
a = list(map(int, input().split()))
s = [0] + list(accumulate(a))
m = s[-1] // 2
r = bisect(s, m)
l = r - 1
ans = min(abs(s[-1] - s[l] * 2), abs(s[-1] - s[r] * 2))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
x = sum(a)
now = 0
key = 0
for i in range(n):
    now += a[i]
    if 2*now >= x:
        key = i
        break
if key == 0:
    ans = 2*now-x
elif key == n-1:
    ans = 2*a[n-1]-x
else:
    ans = min(2*now-x,x-2*(now-a[key]))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
Asum = sum(A)
 
s=0
t=0
for i,Ai in enumerate(A):
    s+=Ai
    if s == Asum/2:
        print(0)
        exit()
    if s >= Asum/2:
        s = i
        break
for i,Ai in enumerate(A[::-1]):
    t+=Ai
    if t >= Asum/2:
        t = i
        break
 
ans1 = abs(sum(A[:s])-(Asum-sum(A[:s])))
ans2 = abs(sum(A[::-1][:t])-(Asum-sum(A[::-1][:t])))
 
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
cumu=[0]
for i in A:
    cumu.append(cumu[-1]+i)
ans = float('inf')

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

Submission status: Accepted

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

# from functools import lru_cache
# @lru_cache(maxsize=100000)

# 一度差を出してから，どんどんi-1, i+1の和で縮めていく．

def check_distance():
    ret = []
    left = A[0]
    right = sum(A[1:])
    tmp_a = abs(left - right)
    ret.append(tmp_a)

    for i in range(1, N-1):
        left += A[i]
        right -= A[i]
        tmp_a = abs(left - right)
        ret.append(tmp_a)
    return min(ret)

print(check_distance())

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
import bisect
ln = []
x = 0
for i in b:
    x += i
    ln.append(x)
index = bisect.bisect_left(ln, sum(b)/2)
if sum(b[0:index -1]) == sum(b)/2:
            print(0)
else:
                v =  sum(b[0:index + 1])- (sum(b)-sum(b[0:index + 1]))
                u = (sum(b)-sum(b[0:index]))-sum(b[0:index])
                print(min(v,u))

Submission status: Accepted

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

Asum = sum(A)

s=0
t=0
for i,Ai in enumerate(A):
    s+=Ai
    if s == Asum/2:
        print(0)
        exit()
    if s >= Asum/2:
        s = i
        break
for i,Ai in enumerate(A[::-1]):
    t+=Ai
    if t >= Asum/2:
        t = i
        break

ans1 = abs(sum(A[:s])-(Asum-sum(A[:s])))
ans2 = abs(sum(A[::-1][:t])-(Asum-sum(A[::-1][:t])))

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = sum(a)
cnt = 0
cnt_2 = 0

for i in range(n):
    cnt += a[i]
    cnt_2 = cnt - a[i]
    if cnt >= L / 2:
        break

if cnt == L / 2 or cnt_2 == L / 2:
    ans = 0
    ans_2 = 0
else:
    ans = abs(L - cnt * 2)
    ans_2 = abs(L - cnt_2 * 2)

print(min(ans, ans_2))

Submission status: Accepted

Solution code:
n = int(input())
a= [int(x) for x in input().split(" ",n-1)]
s = 0
for x in range(n):
    s += a[x-1]
r = 0
l = 0
x = 0
while r < s/2:
    r += a[x]
    x += 1
y = 1
while l < s/2:
    l += a[n-y]
    y += 1
if r == s//2:
    print(0)
else:
    k = min(r,l)  
    v = k - s/2
    m = v // 0.5
    print(int(m))

Submission status: Accepted

Solution code:
# 2019/11/23
# DISCO presents ディスカバリーチャンネル コードコンテスト2020 予選 - B

# Binary Search
def binSearch (tlis, tval):
    lf = 0
    rg = len(tlis)
    while(lf <= rg):
        ct = int((lf + rg) / 2)
        if tlis[ct] < tval:
            lf = ct + 1
        elif tlis[ct] > tval:
            rg = ct - 1
        else:
            return ct
    return ct

# Input
n = int(input())
a = list(map(int,input().split()))
acm_a = list()
acm_a.append(0)

# Sum
for i in range(len(a)):
    acm_a.append(a[i] + acm_a[i])

acm_a.append(acm_a[len(a)])
# Calc Center
center = acm_a[len(a)] / 2
ctidx = binSearch(acm_a, center)

ans = min(abs((acm_a[len(a)] - acm_a[ctidx-1]) - acm_a[ctidx-1]), abs((acm_a[len(a)] - acm_a[ctidx]) - acm_a[ctidx]), abs((acm_a[len(a)] - acm_a[ctidx+1]) - acm_a[ctidx+1]))

# Output
print(ans)