Problem:


Score: 200 points

Problem Statement

Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters.

Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:

Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).

Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.

Find the minimum amount of money needed before cutting the bar into two parts with the same length.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.

Sample Input 1

3
2 4 3

Sample Output 1

3

The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:

Shrink the second section from the left. The lengths of the sections are now [2, 3, 3].

Shrink the first section from the left. The lengths of the sections are now [1, 3, 3].

Shrink the second section from the left. The lengths of the sections are now [1, 2, 3], and we can cut the bar at the second notch from the left into two parts of length 3 each.

Sample Input 2

12
100 104 102 105 103 103 101 105 104 102 104 101

Sample Output 2

0



Sample input:
3
2 4 3


Sample output:
3

Submission status: Accepted

Solution code:
import numpy as np
def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]

N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
sum_A = np.sum(A)
res = np.cumsum(A)
value = getNearestValue(res, sum_A/2)
print(abs(value - (sum_A - value)))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    arr = list(map(int,input().split()))
    dp = []
    for i in arr:
        if not dp:
            dp.append(i)
        else:
            dp.append(dp[-1]+i)

    ans = float('inf')

    for i in range(n-1):
        left = dp[i]
        right = dp[-1]-dp[i]

        ans = min(ans,abs(left-right))

    print(ans)

main()

Submission status: Accepted

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


def f(A, k):
    s = 0
    for p, a in enumerate(A):
        if s+a > k:
            return (s, p)
        s += a


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    S = sum(A)
    k = S/2
    (s, p) = f(A, k)
    
    x = int(min(k-s, s+A[p]-k)*2 + 0.5)
    print(x)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
sum_a_ls = sum(a_ls)

i = 0
short_bou = 0
half = sum_a_ls / 2#小数なるかも
while True:
    if short_bou + a_ls[i] > half:
        break
    else:
        short_bou += a_ls[i]
        i+=1

if half == short_bou:
    print(0)
else:
    sub_diff = min(half - short_bou, short_bou + a_ls[i] - half)
    print(int(sub_diff * 2))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_left
from itertools import accumulate


n = ni()
a = list(li())

suma = sum(a)
cuma = [0] + list(accumulate(a))
half = suma / 2
halfidx = bisect_left(cuma, half) - 1
if halfidx == -1:
    print(cuma[1] - (cuma[n] - cuma[1]))
else:
    first = cuma[n] - cuma[halfidx] - cuma[halfidx]
    latter = cuma[halfidx+1] - (cuma[n] - cuma[halfidx+1])
    print(min(first, latter))

Submission status: Accepted

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

s=sum(l)
m=0
i=-1
while m<s//2:
    i+=1
    m+=l[i]
    


print(min(2*m-s,s-2*(m-l[i])))

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
acc = [0]*(n+1)
for i in range(n):
    acc[i+1] = acc[i]+a[i]
h = acc[n]/2
j = -1
for i in range(n):
    if acc[i]<=h and h<acc[i+1]:
        j = i
        break
ddprint(acc)
ddprint(j)
if acc[j]==h:
    print(0)
    exit()
x = min(h-acc[j], acc[j+1]-h)
print(int(2*x))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [0] * n
t = [0] * n
s[0] = a[0]
t[-1] = a[-1]
for i in range(1,n):
    s[i] = s[i-1] + a[i]
    t[-i-1] = t[-i] + a[-i-1]

#print(s,t)

m = min([abs(s[i]-t[i+1]) for i in range(n-1)])

print(m)

Submission status: Accepted

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

l = sum(a)
cur_l = 0
pre_l = 0
for i, ai in enumerate(a):
    cur_l += ai
    if l - 2*cur_l < 0:
        res = min(abs(l-2*cur_l), abs(l-2*pre_l))
        break
    pre_l = cur_l
print(res)

Submission status: Accepted

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

ans = 217937901270479417
_sum = sum(a)
sum_1 = 0
for i in range(0,len(a)):
    sum_1 += a[i]
    sum_2 = _sum - sum_1
    ans = min(ans, abs(sum_1 - sum_2))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
alist = list(map(int,input().split()))
length = 0
temp = 0
keypoint = 0

for a in alist:
    length += a

halflength = length/2
    
for i in range(len(alist)):
    temp += alist[i]
    if temp > halflength:
        keypoint = i
        break
        
beforeamari = abs(halflength-temp+alist[i])
afteramari = abs(halflength-temp)

if beforeamari>=afteramari:
    print(int(afteramari*2))
else:
    print(int(beforeamari*2))

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
idx = bisect.bisect(acmA, acmA[-1]/2)
print(min(abs(acmA[idx-1]-(acmA[-1]-acmA[idx-1])),abs(acmA[idx]-(acmA[-1]-acmA[idx]))))

Submission status: Accepted

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

sum_all = sum(A)
sum_front = 0
ans = float('inf')
for a in A:
    sum_front += a
    ans = min(abs(2 * sum_front - sum_all), ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 0
s = sum(a)
if n==2:
    print(a[1]-a[0])
    exit()
i = 0
if s%2==0:
    while b<s//2:
        b+=a[i]
        i+=1
else:
    while b<=s//2:
        b+=a[i]
        i+=1
c =s-b
e = b-c
ans = min(abs(e-2*a[i-1]),abs(e))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a) / 2
i = 0
j=0
while i < n and j < s:
    j += a[i]
    i += 1
ans1 = abs(sum(a[:i]) - sum(a[i:n]))
ans2 = abs(sum(a[:i-1]) - sum(a[i-1:n]))
print(min(ans1, ans2))

Submission status: Accepted

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

a = sum(list1)/2
length = 0
index = 0

#要素数が２のときは特例
if n == 2:
    print(abs(list1[1]-list1[0]))
    exit()

for i in range(n):
    if a <= length:
        index = i
        break
    length += list1[i]

o = sum(list1[:index])
p = sum(list1[index:])

q = sum(list1[:index-1])
r = sum(list1[index-1:])

print(min(abs(o-p),abs(q-r)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate
import numpy as np

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

acc = np.array(list(accumulate(A)))
l = -(acc - sum(A))

print(min(abs(acc-l)))

Submission status: Accepted

Solution code:
a=[]
b=int(input())
n=0
nn=0
nnn=0
mnn=0
hnn=0
hmnn=0
h=0
m=0
for x in input().split(" "):
    a.append(int(x))
for x in a:
    n+=x
for x in a:
    mnn=nn
    nn+=x
    if nn>=n/2:
        break
hnn=nn-n/2
hmnn=n/2-mnn
if hmnn>=hnn:
    h=hnn
else:
    h=hmnn
print(int(h*2))

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect
n = int(input())
als = list(map(int, input().split()))

ac = list(accumulate(als))

asum = sum(als)

trg = asum/2
idx = bisect.bisect_left(ac, asum/2)
if ac[idx] == trg:
    ans = 0
else:
    ans = min(trg - ac[idx-1], ac[idx] - trg)/0.5
print(int(ans))

Submission status: Accepted

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

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

L = S[-1]

pre_s = 0
for s in S:
    if s > L/2:
        break
    pre_s = s

l = L - 2*pre_s
r = 2*s-L
print(min(l,r))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

cumsum = 0
pos=[]
total = sum(A)
for a in A:
    cumsum+=a
    pos.append(abs(total/2-cumsum))
print(int(min(pos)*2))

Submission status: Accepted

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

n = int(input())
a_list = list(map(int, input().split()))
a_half = sum(a_list) / 2

for i in range(n):
  a_half -= a_list[i]
  if a_half <= 0:
    #print(a_half, a_half+a_list[i])
    row_answer = a_half
    high_answer = a_half+a_list[i]
    break

print(int(min(-1 * row_answer, high_answer) * 2))

Submission status: Accepted

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

d = sum(A)
mi = d
for a in A:
    d -= 2*a
    mi = min(mi, abs(d))

print(mi)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import repeat
def main(ab=abs):
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    s = sum(a)
    t = 0
    ans = s
    for i, x in enumerate(a):
        t += x
        s -= x
        c = ab(s - t)
        if ans > c:
            ans = c
    print ans
main()

Submission status: Accepted

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

sum_A = sum(A_list)
acc_A = [0] * N
acc_A[0] = A_list[0]
border = sum_A / 2
for i in range(1, N):
    acc_A[i] += A_list[i] + acc_A[i-1]

for i in range(N):
    if acc_A[i] > border:
        border_i = i
        break

left = abs(2 * acc_A[border_i - 1] - sum_A)
right = abs(2 * acc_A[border_i] - sum_A)

if left > right:
    ans = right
else:
    ans = left

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
all_sum = sum(A)
left_sum = 0
ans = 1<<40
for a in A:
	left_sum += a
	right_sum = all_sum - left_sum
	ans = min(ans, abs(right_sum - left_sum))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = deque(list(map(int,input().split())))
L = 0
R = 0
while len(A) != 0:
    if L < R:
        L += A.popleft()
    else:
        R += A.pop()
print(abs(R-L))

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
A = list(map(int, input().split()))
B = list(accumulate(A))
ans = float('inf')
for i in range(N-1):
    ans = min(ans, abs(B[i]-(B[-1]-B[i])))

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
a=list(map(int,input().split()))
l=[]
c=sum(a)/2

mi=10**12
tmp=0

for i in range(n):
    tmp+=a[i]
    l.append(tmp)
    if mi > abs((c-l[i])):
        mi=abs(c-l[i])
        k=i

print(abs(l[-1]-2*l[k]))

Submission status: Accepted

Solution code:
def main():
    #print = out.append
    ''' Cook your dish here! '''
    n = get_int()
    li  = get_list()
    full_sm = sum(li)
    sm = 0
    res = full_sm
    for i in li:
        sm += i
        right = full_sm - sm
        res = min(res, abs(sm-right))
    print(res)



''' Coded with love at India by Satyam Kumar '''
import sys
#from collections import defaultdict
import math
#input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()
#[main() for _ in range(int(input()))]
print(*out, sep='\n')

Submission status: Accepted

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

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

ans = abs(2 * r[0] - sumA)
for i in range(N):
   ans = min(abs(2 * r[i] - sumA), ans)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=sum(a)
d=c/2
cnt=0
count=0
for i in range(n):
    cnt+=a[i]
    if cnt>=d:
        if abs(cnt-a[i]-d)<abs(cnt-d):
            cnt-=a[i]
            count=i
        else:
            count=i
        break
print(abs(2*cnt-c))
#print(count,cnt)

Submission status: Accepted

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

b=itertools.accumulate(a)
b=list(b)
x=float('inf')
for i in range(n):
    x=min(abs(b[i]-(b[-1]-b[i])),x)

print(x)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    m = sum(A) // 2

    s = 0
    c = 0
    while s < m:
        s += A[c]
        c += 1

    d1 = abs(sum(A[:c]) - sum(A[c:]))
    d2 = abs(sum(A[:c-1]) - sum(A[c-1:]))

    if d1 < d2:
        print(d1)
    else:
        print(d2)


main()

Submission status: Accepted

Solution code:
n = int(input())

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

tot = sum(ls)

p = 0
for i in range(n):
  p += ls[i]
  if p>= tot/2:
    break
tmp = 2*p-tot

p = 0
for i in range(n-1,-1,-1):
  p += ls[i]
  if p>= tot/2:
    break
print(min(tmp,2*p-tot))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

acum = np.cumsum(A)

print(int(min(abs(acum-(sum(A)/2)))*2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
t = 0
i = 0
while True:
  t += a[i]
  if 2*t >= s:
    break
  i += 1
print(min(2*t-s,s-2*t+2*a[i]))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
first = 0
goukei = sum(li)
ans = goukei

for i in range(n):
    first += li[i]
    rest = goukei - first
    if abs(first-rest) < ans:
        ans =  abs(first-rest)
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()
acc = [0]
for x in ls:
    acc.append(acc[-1] + x)
res = float('inf')
for x in acc:
    res = min(res, abs(acc[-1] - 2 * x))
print(res)

Submission status: Accepted

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

B = set()
x = 0
L = sum(A)
for i in A:
    x += i
    B.add(abs(abs(L-x)-x))

print(min(B))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  input()
  v = list(map(int, input().split()))
  prev_value, post_value = 0, 0
  prev_index, post_index = 0, len(v) - 1

  while prev_index <= post_index:
    if prev_value < post_value:
      prev_value += v[prev_index]
      prev_index += 1
    else:
      post_value += v[post_index]
      post_index -= 1

  print(abs(prev_value - post_value))


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
from bisect import bisect

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n=int(input())
    aa=list(map(int, input().split()))
    cs=[aa[0]]
    for i in range(1,n):
        cs.append(cs[i-1]+aa[i])
    #print(cs)
    mid=cs[-1]/2
    mi=bisect(cs,mid)
    ans=min(mid-cs[mi-1],cs[mi]-mid)
    print(int(ans*2))

main()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.fromstring(input(), dtype=np.int64, sep=' ')
b = np.cumsum(A)
c = b[N - 1] - b
d = np.abs(b - c)
ans = np.min(d)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
# S = input()
A_list = list(map(int,input().split()))
# X2_list = [list(map(int,input().split())) for i in range(N)]
full_sum = sum(A_list)
min_haba = full_sum
tmp_sum = 0
for i in range(N):
    tmp_sum += A_list[i]
    min_haba = min(abs((full_sum - tmp_sum)- tmp_sum),min_haba)
    #print(min_haba)

print(min_haba)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
sum = np.sum(A)
cumsum = np.cumsum(A)
diff = float("inf")
for n in range(N):
    left = cumsum[n]
    right = sum-left
    if abs(left-right) < diff:
        diff = abs(left-right)
print(diff)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = list(map(int, input().split()))
B = [0]*N
B[0] = A[0]

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

p = B[-1]/2
q = bisect.bisect_left(B,p)

if q>=1:
  ans = min(abs(B[q-1]-p),abs(B[q]-p))*2
else:
  ans = abs(B[q]-p)*2
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int,input().split()))
yen = 0
length = sum(As)
l = 0
for a in As:
  l += a
  if l == length/2:
    break
  if l > length/2:
    c = l - length/2
    diff = min(abs(c),abs(b))
    yen += int(diff*2)
    break
  b = length/2 - l
print(yen)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
doubledCumSum = 0
total = sum(a)
lower = upper = 0
for v in a:
    doubledCumSum += 2 * v
    if doubledCumSum <= total:
        lower = doubledCumSum 
    else:
        upper = doubledCumSum
        break
ans = min(abs(total - lower), abs(total - upper))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
S=sum(A)
x=0
ans=S
for a in A:
    x+=a
    y=abs(S-2*x)
    ans=min(ans,y)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Sa = sum(A)
h = Sa/2
S = 0

for a in A:
    if S+a > h:
        if abs(h-S) > abs(h-S-a):
            S += a
        break
    S += a

print(abs((Sa-S)-S))

Submission status: Accepted

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

b=[]
sum=0
for i in range(n):
  sum += a[i]
  b.append(sum)

j=0
while b[j] <= (sum//2):
  j += 1

ans1=abs(sum - 2*b[j])
ans2=10**10
if j!=0:
  ans2=abs(sum -2*b[j-1])
  
ans = min(ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = list(map(int,input().split()))
cums = [0]
for a in A:
    cums.append(cums[-1] + a)

half = cums[-1]//2

i = bisect.bisect_left(cums, half)
if cums[i] == half:
    print(0)
    exit()
if cums[-1]%2 and cums[i] == half+1:
    print(0)
    exit()

a,b = cums[i], cums[-1]-cums[i]
ans = abs(a-b)

if i>0:
    a,b = cums[i-1], cums[-1]-cums[i-1]
    ans = min(ans, abs(a-b))

print(ans)

Submission status: Accepted

Solution code:
# 2019-11-23 21:00:35(JST)
import sys
import itertools

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

    S = sum(A)
    l = 0
    for i in range(n):
        l += A[i]
        if l >= S // 2:
            if l == S / 2:
                ans = 0
                break
            else:
                if l == S // 2 - 1:
                    ans = 1
                    break
                else:
                    if l - A[i] >= S - l:
                        ans = A[i] + (S - l) - (l - A[i])
                    else:
                        ans = l - (S - l)
                    break

    print(ans)





if __name__ == '__main__':
    main()

Submission status: Accepted

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

length = 0
L = sum(A)

for i in range(N):
    length += A[i]
    if length >= L / 2:
        cut = i
        break    

sum_left1 = length
sum_right1 = L - length

sum_left2 = length - A[cut]
sum_right2 = sum_right1 + A[cut]

ans = min(abs(sum_left1 - sum_right1),abs(sum_left2-sum_right2))
print(ans)

Submission status: Accepted

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

subsum = 0
for itr, val in enumerate(A):
  subsum += val
  if subsum >= sumA / 2:
    p = itr
    ax = val
    break

ans = min(subsum * 2 -sumA, sumA - (subsum - ax) * 2)
print(ans)

Submission status: Accepted

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

s1 = s - a[i]    
print(min((t - s1 -s1), (s- (t - s) )))

Submission status: Accepted

Solution code:
# coding: utf-8
def main():
    n = int(input())
    A = list(map(int, input().split()))
    ave = sum(A) / 2
    total = 0
    
    b = 0
    a = 0
    
    for i in range(n):
        total += A[i]
        if total >= ave:
            b = sum(A[:i+1])
            a = sum(A[i+1:])
            b2 = sum(A[:i])
            a2 = sum(A[i:])
            
            break
    print(min(max(a,b)-min(a,b),max(a2,b2)-min(a2,b2)))
    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))
ans=9999999999
hidari=sum(a)
migi=0
for i in range(n):
    hidari-=a[i]
    migi+=a[i]
    sa=math.fabs(hidari-migi)
    if sa<ans:
        ans=sa
print(int(ans))

Submission status: Accepted

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

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

id = bisect.bisect_left(s, s[-1] // 2)
if s[id] == s[-1] // 2:
    if s[-1] % 2 == 0:
        print(0)
    else:
        print(1)
else:
    k = 0
    if s[-1] % 2 != 0:
        k = 1
    ans = abs(s[id]*2 - s[-1])
    if id > 0:
        ans = min(ans, abs(s[id-1]*2 - s[-1]))
    print(ans)

Submission status: Accepted

Solution code:

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

b = 0
idx = 0
L = sum(A)
while (b<L/2):
    b += A[idx]
    idx += 1
a = b - A[max(0,idx-1)]
ans = min(L-2*a,2*b-L)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import bisect
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]


# A
def A():
    X, Y = LI()
    ans = 0
    if X == 1:
        ans += 300000
    elif X == 2:
        ans += 200000
    elif X == 3:
        ans += 100000

    if Y == 1:
        ans += 300000
    elif Y == 2:
        ans += 200000
    elif Y == 3:
        ans += 100000

    if X == 1 and Y == 1:
        ans += 400000

    print(ans)
    return


# B
def B():
    N = I()
    A = LI()
    A_sum = sum(A)
    cnt = 0
    ans = sum(A)
    for i in range(N):
        cnt += A[i]
        sa = abs(A_sum - cnt - cnt)
        ans = min(ans, sa)
    print(ans)
    return


# C
def C():
    return


# D
def D():
    return


# E
def E():
    return


# F
def F():
    return


# Unittest
def resolve():
    B()
    return


# Solve
if __name__ == "__main__":
    B()

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()))

mid = sum(a)/2
#print(mid)

x=[0]*(n)
for i in range(n-1):
  x[i+1]=x[i]+a[i]

x=x[1:]
#print(x)
dis = [abs(x[i]-mid) for i in range(n-1)]
ret = int(min(dis)/0.5)

print(ret)

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_a = sum(a)
p = 0
for i in range(n):
    count += a[i]
    if count > sum_a//2:
        p = i
        break
if p == 0:
    ans = abs(sum_a - count*2)
else:
    ans = min(abs(sum_a - count*2), abs(sum_a - (count-a[p])*2))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
kei=sum(hoge)
now=0
ans=-1
for i in range(n):
  now+=hoge[i]
  if(abs(now-kei/2)<=ans or ans==-1):
    ans=abs(now-kei/2)
    
print(int(ans*2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = input()
    aa = map(int, raw_input().split())
    length = sum(aa)
    s = 0
    for i, a in enumerate(aa):
        s += a
        if s >= length / 2.0:
            break
    if s == length / 2.0:
        print 0
        return
    print min(s * 2 - length, length  - (s - aa[i]) * 2)


main()

Submission status: Accepted

Solution code:
import bisect as bi
import numpy
n = int(input())
li = list(map(int,input().split()))

li = list(map(lambda x:2*x,li))
acc = list(numpy.cumsum(li))
m = acc[n-1]//2
ml = acc[bi.bisect_left(acc,m)-1]
mr = acc[bi.bisect_left(acc,m)]
#print("{} {} {}".format(m,ml,mr))
#print(acc)
print(min(m-ml,mr-m))

Submission status: Accepted

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

tot_len = sum(A)
cum = 0
for i in range(N-1):
    cum += A[i]
    if cum >= tot_len/2:
        break
first = sum(A[:i+1])
last = sum(A[i+1:])
diff = abs(first - last)
if i>=1:
    first2 = sum(A[:i])
    last2 = sum(A[i:])
    diff2 = abs(first2 - last2)
    print(min(diff,diff2))
else:
    print(abs(first - last))

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

co = sum(a)
su = 0
for i in range(n):
    su += a[i]
    if su >=co/2:
        k,l = i-1,i
        break
print(min(abs(2*su-co),abs(co-2*(su-a[l]))))

Submission status: Accepted

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

total = sum(A_list)
half = total/2
right_side = 0

for i in range(N):
  if right_side + A_list[i] <= half:
    right_side += A_list[i]
  else:
    left_side = total - right_side
    next_length = A_list[i]
    break

#print(right_side,left_side,abs(right_side - left_side))
#print(right_side+next_length,left_side-next_length,abs(right_side+next_length-left_side+next_length))

if abs(right_side - left_side)<abs(right_side+next_length-left_side+next_length):
  print(abs(right_side - left_side))
else:
  print(abs(right_side+next_length-left_side+next_length))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
A = sum(a)
x = A - 2*a[0]
ll = [x]

for i in range(1,n-1):
    x = x-2*a[i]
    ll.append(x)

for i in range(len(ll)):
    ll[i] = abs(ll[i])
    
print(min(ll))

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:
N = int(input())
A = list(map(int, input().split()))

left = [0] * N
right = [0] * N

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

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



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

Submission status: Accepted

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

tot = sum(A)
rui = [0]*len(A)
cur = 0
for i in range(0,len(A)):
  cur += A[i]
  rui[i] = cur

ans = 0
for i in range(0,len(A)):
  if rui[i] < tot//2 and rui[i+1] > tot//2:
    ans = abs(rui[i] - (tot - rui[i]))
    ans2 = abs(2*rui[i+1]-tot)
    ans = min([ans, ans2])

print(ans)