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
n, *a = map(int, open(0).read().split())
s = np.cumsum(np.array(a))
print(min([abs(s[n - 1] - s[i] - s[i]) for i in range(n - 1)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = [0, 0]
ans_abs = 10**200

A_sum = sum(A)
A_1 = 0
A_2 = A_sum

for i in range(N - 1):
    A_1 += A[i]
    A_2 -= A[i]
    ans_abs = min(ans_abs, abs(A_1 - A_2))

print(ans_abs)

Submission status: Accepted

Solution code:

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

left = 0
right = sum(lis)
ans = 1001001001001

for i in range(n):
    left += lis[i]
    right -= lis[i]
    ans = min(ans, abs(left-right))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum_a=sum(a)
x=0
i=0
while x<sum_a/2:
    x+=a[i]
    i+=1
if n%2==0:
    print(min(x-(sum_a-x),sum_a-(x-a[i-1])*2))
else:
    print(x-(sum_a-x))

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N = int(input())
A = list(map(int, input().split()))
acc = [0] * (N + 1)
for i in range(1, N + 1):
    acc[i] = acc[i - 1] + A[i - 1]

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sumlow = 0
sumhigh = 0
i = 0
j = N-1
while i<=j:
    if sumlow <= sumhigh:
        sumlow += A[i]
        i += 1
    else:
        sumhigh += A[j]
        j -= 1
print(abs(sumlow-sumhigh))

Submission status: Accepted

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

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

tmp_x = -1
tmp_x_diff = -1
for i in range(N - 1):
    sum1 = cumsum[i + 1]
    sum2 = cumsum[-1] - cumsum[i + 1]
    diff = abs(sum2 - sum1)
    if tmp_x_diff == -1 or diff < tmp_x_diff:
        tmp_x = i
        tmp_x_diff = diff
print(tmp_x_diff)

Submission status: Accepted

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

    mid = 0
    diff = float("inf")
    total = sum(A)
    p = 0
    # print(total)
    for i in range(n):
        # print("i:",i, "total:", total, "p:", p, "diff:", diff)
        if abs(total-p) < diff:
            diff = abs(total - p)
            mid = i
        p += A[i]
        total -= A[i]
    front = A[:mid] 
    back =  A[mid:]
    print(abs(sum(front)-sum(back)))
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n=int(input())
a=list(map(int,input().split()))
cumsum=[0]+list(itertools.accumulate(a))
sum_num=sum(a)
ans=10**100
for i in range(1,n):
    yen=abs(cumsum[i]-(sum_num-cumsum[i]))
    if yen<ans:
        ans=yen
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
A = list(map(int, input().split()))
S = sum(A)
m = S
tmp = 0
for a in A:
    tmp += a
    m = min(m, abs(tmp-(S-tmp)))
print(m)

Submission status: Accepted

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

import numpy as np

c=np.cumsum(a)
s=sum(a)

ans=float('inf')
index=0
for i in range(n):
    if abs(2*c[i]-s)<ans:
        ans=abs(2*c[i] - s)
        index = i

print(ans)

Submission status: Accepted

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

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

    ans = 10**10
    for i in range(1, N):
        l = s[i] - s[0]
        r = s[-1] - s[i]
        ans = min(ans, abs(l - r))

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin


n = int(stdin.readline().strip())
a_lst = [int(x) for x in stdin.readline().strip().split()]

pos = len(a_lst) // 2
min_diff = 10000000000000000000

while(True):
  left = sum(a_lst[:pos])
  right = sum(a_lst[pos:])
  diff = right - left
  
  if min_diff > abs(diff): min_diff = abs(diff)
  else: break
  
  if diff > 0: pos += 1
  elif diff < 0: pos -= 1
    
print(min_diff)

Submission status: Accepted

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

sumA = sum(A)
half = sumA / 2

r = 0.0

t1 = 0.0
t2 = 0.0
for a in A:
    t2 = t1
    t1 += a

    if t1 >= half:
        r = min(abs(half - t1), abs(half - t2))
        break

print(int(r * 2))

Submission status: Accepted

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

Submission status: Accepted

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

sum_A = sum(A)
sum_now = 0
cost = 99999999999
for a in A:
  sum_now += a
  cost = min(cost, abs(sum_now - (sum_A - sum_now)))
print(cost)

Submission status: Accepted

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

diff = 10**10
sum1 = sum(L)
l_sum = 0
ans = 10**10

for i in range(n-1):
    l_sum+=L[i]
    diff = abs(l_sum- sum1 + l_sum)
    ans = min(ans,diff)
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
import bisect


def main():
    n = int(input())
    AList = list(map(int, input().split()))
    ASum = sum(AList)
    c = 0
    for i in range(n):
        c += AList[i]
        if c >= ASum / 2:
            k = i
            break
    ans = min(2 * c - ASum, ASum - 2 * c + 2 * AList[k])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=float('INF')
sa=[0]
for i,a in enumerate(A):
    sa.append(sa[i]+a)
for i in range(n+1):
    ans=min(ans,abs(sa[i]-(sa[-1]-sa[i])))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
sa = [0]
ans = float("INF")
for i, ai in enumerate(list(map(int, input().split()))):
    sa.append(sa[i] + ai)
for i in range(n + 1):
    ans = min(ans, abs(sa[i] - (sa[-1] - sa[i])))
print(ans)

Submission status: Accepted

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

slist=[0]
for a in alist:
  slist.append(slist[-1]+a)  
#print(slist)

answer=float("inf")
for i in range(N+1):
  s1=slist[i]
  s2=slist[-1]-s1
  answer=min(answer,abs(s1-s2))
  
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(A)
l = 0
for i in range(N):
  l += A[i]
  if l>= s/2:
    ans = min([abs(l-A[i]-sum(A[i:])),abs(l-sum(A[i+1:]))])
    print(round(ans))
    break

Submission status: Accepted

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

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


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

ans=aA_n[-1]
for i in range (N-1):
    _tempans=abs(aA_n[i]-aA_r[i+1])
    if _tempans<ans:
        ans=_tempans

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10**11
sum_A = sum(A)
l = 0

for a in A:
    l += a
    if abs(l - (sum_A - l)) < ans:
        ans = abs(l - (sum_A - l))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
t = [0]
for i in range(n):
    t.append(t[i] + a[i])
t.pop(0)
p = t[-1] // 2
if p in t:
    print(0)
else:
    for i in range(n):
        if p < t[i]:
            print(min(abs(sum(a[0:i+1]) - sum(a[i+1:n])),abs(sum(a[0:i]) - sum(a[i:n]))))
            break

Submission status: Accepted

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

Submission status: Accepted

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

l = 0
r = sum(a)
ans = 10**10

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

print(ans)

Submission status: Accepted

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

total = sum(a)
ans = float("inf")
t = 0
for i in a:
  t += i
  total -= i
  ans = min(ans, abs(total-t))
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect

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

acc = list(accumulate(a))

#idx = bisect.bisect_left(acc,acc[-1]//2)
#print(idx)
result = []
for i in range(n):
    result.append(abs((acc[-1] - acc[i] ) - acc[i]))
print(min(result))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Sum = 0
Temp = 0
for a in A:
    Sum += a
for i in range(N):
    Temp += A[i]
    if Temp > Sum//2:
        index = i
        break

print(min(abs(Sum-2*Temp), abs(Sum-2*(Temp-A[index]))))

Submission status: Accepted

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

for i in range(n):
    b += a[i]
    tmp = abs(2*b-t)
    ans = tmp if i==0 else min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
tot=sum(a)
b=0
c=10**10
d=0
for i in range(n):
  b+=a[i]
  d=abs(tot/2 - b)
  if d<c:
    c=d
print(int(2*c))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect


n = int(input())
la = [int(w) for w in input().split()]
la_sum = [0]*(n)
ans = 10**10

la_sum[0] = la[0]
for i in range(1, n):
    la_sum[i] = la_sum[i-1] + la[i]


for i in range(0, n):
    l = la_sum[i]
    r = la_sum[-1]-la_sum[i]
    d = abs(l-r)
    ans = min(ans, d)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
r = [a[0]]
for i in range(1, n):
    r.append(r[i - 1] + a[i])
print(min(abs(2 * i - r[-1]) for i in r))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
s = sum(a)
r = [a[0]]
for i in range(1, n):
    r.append(r[i - 1] + a[i])
print(min(abs(2 * i - s) for i in r))

Submission status: Accepted

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

L=sum(A)
X=0
M=float("inf")
for i in range(N-1):
    X+=A[i]
    M=min(M,abs(2*X-L))
print(M)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

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

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

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

for i in range(N-1):
  left += A[i]
  right -= A[i]
  sa = abs(left-right)
  if i == 0:
    ans = sa
  elif ans > sa:
    ans = sa

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
num0 = 0
num1 = sum(a)

ans = 10**10
for i in range(0, N):
    num0 += a[i]
    num1 -= a[i]
    if ans > abs(num1-num0):
        ans = abs(num1-num0)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    cumsumA = [0] * (N + 1)
    cumsumB = [0] * (N + 1)
    for i in range(N):
        cumsumA[i + 1] = cumsumA[i] + A[i]
        cumsumB[N - i - 1] = cumsumB[N - i] + A[N - i - 1]
    minA = 10**18
    for i in range(N + 1):
        minA = min(minA, abs(cumsumA[i] - cumsumB[i]))
    print(minA)


resolve()

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
L = [0]
for i in range(N):
    L.append(L[i]+A[i])
TotalLen = L[-1]
Center = TotalLen/2

temp = TotalLen
left = 0
right = 0
for i in range(N + 1):
    if abs(L[i] - Center) < temp:
        temp = abs(L[i] - Center)
        left = L[i]
        right = TotalLen - left
print(abs(left - right))

Submission status: Accepted

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

cum = [0]*(N+1)
ans = 10**12
total = sum(a)
for i in range(N):
  cum[i+1] = cum[i] + a[i]
  ans = min(ans,abs(cum[i+1]-(total-cum[i+1])))
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N = k()
A = l()
S = sum(A)
minx = S
sum = 0

for x in A:
    sum += x
    minx = min(minx, abs(sum-(S-sum)))

print(minx)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    diff = 10 ** 10

    partA = 0
    partB = sum(A)

    for i in range(N):
        diff = min(diff, abs(partA-partB))
        partA += A[i]
        partB -= A[i]
    print(diff)
    return
resolve()

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int, input().split())))
S=sum(A)-A[0]
X=A[0]
ans=abs(S-X)
for i in range(1,N-1):
  X+=A[i]
  S-=A[i]
  ans=min(ans,abs(S-X))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min_S)

Submission status: Accepted

Solution code:
import numpy as np

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

cum_A = np.cumsum(A)
ans = 10 ** 18
for i in range(N):
    left = cum_A[i]
    right = cum_A[-1] - cum_A[i]
    ans = min(ans, abs(left - right))
print(ans)

Submission status: Accepted

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

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

cost = None
for i in range(N):
    left = cum_A[i]
    right = cum_A[-1] - cum_A[i]
    if cost is None:
        cost = abs(left - right)
    else:
        cost = min(abs(left - right), cost)

print(cost)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
import itertools
ACU=itertools.accumulate(A)
B=list(ACU)
MAX=B[-1]
tmp=10**10
for i in B:
  tmp=min(tmp,abs(2*i-MAX))
print(tmp)

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=10007
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
a=list(map(int,input().split()))
lst=ruiseki(a)
asum=sum(a)
# print(asum)
# print(lst)
cnt=bisect.bisect_left(lst,asum/2)
# print(cnt)
if asum//2==lst[cnt]:
    print(0)
else:
    if asum%2==0:
        print(2*min(asum//2-lst[cnt-1],lst[cnt]-asum//2))
    else:
        print(min((asum//2-lst[cnt-1])*2+1,(lst[cnt]-asum//2)*2-1))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

Submission status: Accepted

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

half = sum(lis)
left, right, dif = 0, half, half

for i in range(N):
    dif_before = dif
    left += lis[i]
    right = half - left
    dif = abs(right - left)
    if dif >= dif_before:
        break

print(dif_before)

Submission status: Accepted

Solution code:
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()
sum_a = sum(a)
v = sum(a)/2
ans = float("inf")
t = 0
for i in range(len(a)):
	t+=a[i]
	ans = min(ans,abs(sum_a-2*t))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

v = sum(a)/2
t = 0
for i in range(len(a)):
	t+=a[i]
	if t>v:
		break
z1 = sum(a[:i])
k1 = sum(a[i:])
z2 = sum(a[:i+1])
k2 = sum(a[i+1:])
if abs(z1-k1)<abs(z2-k2):
	z = z1
	k = k1
else:
	z = z2
	k = k2

print(abs(z-k))

Submission status: Accepted

Solution code:
import numpy as np

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

a = np.array(a)
s = a.cumsum()
ans = abs(2*s - sum(a))


print(min(ans))

Submission status: Accepted

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

x = n//2
if sum(a)/2 == sum(a[:(n//2)]):
  print(0)
elif sum(a)/2 >= sum(a[:(n//2)]):
  while sum(a)/2 >= sum(a[:x]):
    x += 1  
  print(min(abs(2*sum(a[:x])-sum(a)), abs(2*sum(a[:x-1])-sum(a))))
else:
  while sum(a)/2 <= sum(a[:x]):
    x -= 1
  print(min(abs(2*sum(a[:x])-sum(a)), abs(2*sum(a[:x+1])-sum(a))))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
al=sum(A)
cent=al/2
now=0
for a in A:
  if abs(cent-al+now)<abs(cent-al+now+a):
    print(abs(al-2*now))
    break
  now+=a

Submission status: Accepted

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

Submission status: Accepted

Solution code:
## B - Iron Bar Cutting
N = int(input())
A = list(map(int, input().split()))
L = 0
R = sum(A)
ans = sum(A)
for n in range(N):
    L += A[n]
    R -= A[n]
    if ans > abs(L-R):
        ans = abs(L-R)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate


n = int(input())
a = list(map(int, input().split()))
a_acc = list(accumulate(a))
ans = 2020202020
for i in range(n):
    ans = min(abs(a_acc[-1] - a_acc[i] - a_acc[i]), ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(a) for a in input().split()]
s = sum(arr)
m = s // 2

tmp = 0
for a in arr:
    if tmp + a >= m:
        ans = min(s-tmp*2, (tmp+a)*2-s)
        break
    tmp += a

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    s = sum(a)
    minx = s
    sum_a = 0

    for i in range(n):
        sum_a += a[i]
        minx = min(minx, abs(sum_a - (s - sum_a)))

    print(minx)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
ruiseki = [0]+list(itertools.accumulate(A))
i = bisect.bisect_left(ruiseki,sum(A)/2)
print(min(abs(sum(A[:i])-sum(A[i:])),abs(sum(A[:i-1])-sum(A[i-1:]))))

Submission status: Accepted

Solution code:
N = int(input())
sticks = list(map(int, input().split()))
sticksSum = sum(sticks)
diffMin = sum(sticks)
plusSticks = 0
for i in range(len(sticks)):
    plusSticks += sticks[i]
    diffMin = min(diffMin, abs(2*plusSticks-sticksSum))
print(diffMin)

Submission status: Accepted

Solution code:
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n = I()
    a = Intl()
    m = sum(a)
    s = list(it.accumulate(a))
    ans = []
    for i in range(n):
        ans.append(abs(s[i]-(m-s[i])))
    print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
for i in range(N):
  S[i+1] = S[i] + A[i]

ans = float("INF")
for i in range(1, N+1):
  lsum = S[i]
  rsum = S[N]-S[i]
  ans = min(ans, abs(rsum-lsum))
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
diff = float("INF")
idx = -1
m = sum(A)/2
for i in range(N):
  S[i+1] = S[i] + A[i]
  if abs(m - S[i+1]) < diff:
    diff = abs(m - S[i+1])
    idx = i+1
lsum = S[idx]
rsum = S[N]-S[idx]
print(abs(lsum-rsum))

Submission status: Accepted

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

total = sum(a)
cum_sum = list(accumulate(a))

ans = float("INF")
for former in cum_sum:
    ans = min(ans, abs(former - (total - former)))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
c = 0
d = 10000000000000000
a = 0
for i in range(N):
  c += 2*A[i]
  if abs(c - s) < d:
    a = i
    d = abs(c - s)
a1 = sum(A[:(a+1)])
a2 = sum(A[(a+1):])
ans = abs(a1-a2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(A)
r = 10**10
n = 0
for i in range(N-1):
    n += A[i]
    r = min(abs(s-n*2), r)

print(r)

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 # (string,3) 3回
#from collections import deque
#from collections import deque,defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
# n = I()
# A = readInts()
# div = sum(A)//2
# nya = 0
# pivot = 0
# for i in range(n):
#     nya += A[i]
#     if nya >= div:
#         pivot = i
#         break
# #print(i)
# print(abs(sum(A[i+1:]) - nya))
n = int(input())
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:
N=int(input())
A=list(map(int, input().split()))
S=M=sum(A)
cnt=0
for a in A:
    cnt+=a
    M=min(M, abs(S-2*cnt))
print(M)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

n = int(input())
a = [int(x) for x in input().split()]

from itertools import accumulate

a_csum = list(accumulate(a))

ans = 10**10

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

print(ans)

Submission status: Accepted

Solution code:
import sys


if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    pref = [a[0]]
    cost = 10**18
    for  i in range(1,n):
        pref.append(pref[i-1] + a[i])
    for i in range(n):
        d1 = pref[i]
        d2 = pref[-1] - pref[i]
        cost = min(cost,abs(d1-d2))
    print(cost)

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=lmp()
x=0
y=sum(a)
ans=abs(x-y)
for i in range(n):
    x+=a[i]
    y-=a[i]
    ans=min(ans,abs(x-y))
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
HALF = sum(As)/2

from itertools import accumulate
from bisect import bisect_left
cum = list(accumulate(As))
idx = bisect_left(cum, HALF)
L = abs(sum(As[:idx])-sum(As[idx:]))
R = abs(sum(As[:idx+1])-sum(As[idx+1:]))
print(min(L, R))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
l = A[0]
r = sum(A[1:])
x = abs(l-r)
for a in A[1:]:
    l += a
    r -= a
    if x > abs(l-r): x = abs(l-r)
print(x)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
SUM = sum(A)
half = SUM//2
acc = 0;dif = SUM
for i in range(N):
  acc += A[i]
  dif = min(dif,abs(SUM-2*acc))

print(dif)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from itertools import accumulate
    acc = [a for a in accumulate([0] + A)]
    ans = acc[-1]
    for i in range(1, N+1):
        a = acc[i]
        b = acc[N] - acc[i]
        v = abs(a-b)
        ans = min(ans, v)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Sum = sum(a) // 2

cand = 0
for i in range(n):
    temp = cand + a[i]
    if abs(cand - Sum) < abs(temp - Sum):break
    cand = temp
cost = abs(Sum - cand) + abs(Sum - (sum(a) - cand))
print(cost)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
temp=sum(a)
ans=0
t=9999999999999999
tsum=0
ta=0
for i in range(n):
  tsum=tsum+a[i]
  if abs(tsum-temp/2)<t:
    t=abs(tsum-temp/2)
    ta=tsum
print(abs(int(ta)*2-temp))