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()))
l = A[0]
r = sum(A[1:])
res = abs(l-r)
for i in range(1,N):
    l += A[i]
    r -= A[i]
    res = min(res, abs(l - r))

print(res)

Submission status: Accepted

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

total = sum(a)

pre = 0
for i in range(n):
    if pre + a[i] >= total // 2:
        x = abs((total - pre * 2))
        y = abs((total - (pre + a[i])*2))
        print(min(x, y))
        break
    pre += a[i]

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
from bisect import bisect_left
read = sys.stdin.read

N, *A = map(int, read().split())
A = list(accumulate([0] + A))
a = A[-1] // 2
n = bisect_left(A, a) - 1

print(min(abs(2 * A[n] - A[-1]), abs(2 * A[n + 1] - A[-1])))

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))






def main():
    n = ii()
    a = li()
    s = sum(a)

    stmp = 0
    point = 0
    for i in range(len(a)):
        stmp += a[i]
        if stmp*2 >= s:
            point = i
            break
    
    stmptmp = stmp-a[i]

    ans=0
    #if stmptmp+stmp>s:
    ans = min(abs(stmptmp*2-s),abs(stmp*2-s))
    print(ans)


    





main()

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]
i=0
while a[i]<=a[n-1]//2:
    i+=1
left=a[i]
right=a[n-1]-a[i]
ans=abs(left-right)
left=a[i-1]
right=a[n-1]-a[i-1]
ans=min(ans,abs(left-right))
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
B = deque(A)
left = 0
left += B.popleft()
right = 0
while B != deque():
    if right < left:
        right += B.pop()
    else:
        left += B.popleft()
print(abs(left - right))

Submission status: Accepted

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


total = sum(A)

half = total / 2

v1 = 0
v2 = total
min_d = None

for i in range(N):
    v1 += A[i]
    v2 = total - v1

    d = abs(v2 - v1)

    if min_d is None:
        min_d = d
    elif min_d > d:
        min_d = d

print(min_d)

Submission status: Accepted

Solution code:
def main():

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

    ans = float('inf')
    l, r = 0, tot
    for i in range(N):
        l += A[i]
        r -= A[i]
        ans = min(ans, abs(l-r))
    return ans

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n=int(input())
alist = list(int(i) for i in input().split())

gokei=sum(alist)
now=0

for i in range(n):
    now+=alist[i]
    if now>=gokei//2:
        break
    mae=now

#print(gokei)
#print(mae, now)
ans=min(gokei-mae*2,now*2-gokei)
print(ans)

Submission status: Accepted

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

length = sum(a)

x = 0
for i in range(n):
  if x+a[i] > length /2:
    y = length - (x + a[i])
    break
  x += a[i]

z = min(y+a[i]-x,x+a[i]-y)
print(z)

Submission status: Accepted

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

ans=float("inf")
for i in range(n):
    ans=min(ans,abs(s-wa[i]*2))
print(ans)

Submission status: Accepted

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

sum_start = 0
sum_end = 0
index_start = 0
index_end = n - 1

while True:
    if index_start > index_end:
        break
    if sum_start < sum_end:
        sum_start += a[index_start]
        index_start += 1
    else:
        sum_end += a[index_end]
        index_end -= 1

print(abs(sum_start - sum_end))

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    la = rli()
    s = sum(la)
    half = s/2
    l = 0
    m = s
    for a in la:
        l += a
        m = min(m, abs(l-half))
    print(int(m*2))



if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = sum(A)
plc = 0
l = sum(A)
S = 0

for i in range(N):
    S += A[i]
    if abs(S-l/2) < m:
        m = abs(S-l/2)
        plc = i

print(abs(sum(A[:(plc+1)])-sum(A[(plc+1):])))

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
cnt=0
i=0
while(cnt<suma/2):
    cnt+=a[i]
    i+=1
cnt2=cnt-a[i-1]
half=float(suma)/2
# print(half)
nerai=min(abs(suma-cnt-half),abs(cnt-half),abs(cnt2-half),abs(suma-cnt2-half))
print(int(nerai*2))

Submission status: Accepted

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

suma = 0
for i in a:
  suma += i

diff = float('INF')
part = 0
for i in a:
  part += i
  if abs(part * 2 - suma) < diff:
    diff = abs(part * 2 - suma)

print (diff)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#DISCO A
import sys
import math
import collections
#sys.setrecursionlimit(100000)

n=int(input())

tmp = input().split()
a = list(map(lambda a: int(a), tmp))

total=0
for i in range(0,n):
	total+=a[i]
tmp=0
for i in range(0,n):
	tmp+=a[i]*2
	if(tmp>total):
		if(tmp-total<total-tmp+a[i]*2):
			print(tmp-total)
		else:
			print(total-tmp+a[i]*2)
		sys.exit()

Submission status: Accepted

Solution code:
def cumsum(s):
    n = len(s)
    cs = [0] * (n+1)
    for i in range(n):
        cs[i+1] = cs[i] + s[i]
    return cs

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

cs = cumsum(a)
ans = float("inf")
for i in range(n+1):
    ans = min(ans, abs(cs[i] - (cs[n] - cs[i])))
print(ans)

Submission status: Accepted

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

ans = float('inf')
left = 0
right = 0
allA = sum(A)


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

Submission status: Accepted

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

S = sum(a_list)
from itertools import accumulate
# a_list.index(0, 0)
acc = list(accumulate(a_list))

ans = float("inf")
for i in range(N):
    ans = min(ans, abs((S - acc[i]) - acc[i]))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

bar = [int(i) for i in input().split()]

s = sum(bar)
ans = s

left = 0
right = s
for i in range(n):
    left += bar[i]
    right -= bar[i]
    ans = min(ans, abs(right - left))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=0
y=sum(a)
mini=float('inf')
for i in a:
    x+=i
    y-=i
    mini=min(mini,abs(x-y))
print(mini)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [0]
for i in a:
    s.append(s[-1]+i)
l = s[-1]
from bisect import bisect_left
j = bisect_left(s, l/2)
ans = float("inf")
for i in range(2):
    ans = min(ans, abs(2*s[j-i]-l))
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import accumulate
from itertools import groupby
import itertools

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

lin = list(accumulate(li)) 

ma = sum(li)
sa = 10**10
for i in range(n):
    hidari = lin[i]
    migi = ma-hidari
    sa = min(sa,abs(hidari-migi))

print(sa)

Submission status: Accepted

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

asum = sum(a)
num = 0
ai = 0
for i in range(n):
    num += a[i]
    ai = i
    if num * 2 >= asum:
        break

f1 = asum - num
b1 = num

num -= a[ai]
f2 = asum - num
b2 = num

print(min(abs(f1 - b1), abs(b2 - f2)))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split(' ')]
all_s = 0
for x in A:
    all_s += x

half_size = all_s / 2.

acc = 0
for x in A:
    prev = acc
    acc += x
    if acc == half_size:
        print(0)
        break
    if acc > half_size:
        s1 = all_s - 2 * prev
        s2 = 2 * acc - all_s
        print(min(s1,s2))
        break

Submission status: Accepted

Solution code:
import queue
import math
import copy
"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""
N = int(input())
A = [int(i) for i in input().split()]
T = sum(A)/2

b = 0
c = 0
m = 2020202021
for a in A:
	if b <= T:
		if m > (T-b)//0.5:
			m = (T-b)//0.5
	else:
		if m > (b-T)//0.5:
			m = (b-T)//0.5
	b += a
if b <= T:
	if m > (T-b)//0.5:
		m = (T-b)//0.5
else:
	if m > (b-T)//0.5:
		m = (b-T)//0.5
print(int(m))

Submission status: Accepted

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

Submission status: Accepted

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

aa = [0]*(n+1)

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

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

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
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)]
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    s = [0]
    for i in a:
        s.append(s[-1]+i)
    ans = float("inf")
    for i in range(n):
        k = abs(s[-1]-s[i]-s[i])
        ans = min(ans,k)
    print(ans)
    return

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

Submission status: Accepted

Solution code:
import sys
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()

num = sum(a)
ans = MAX_INT
l = 0
for i in a:
  l += i
  ans = min(ans, abs(l - (num - l)))
print(ans)

Submission status: Accepted

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

L = sum(As)
L2 = L * 2
acc = 0
minD = float('inf')
for a in As:
    acc += a * 2
    d = abs(acc - L)
    minD = minD if minD < d else d
print(minD)

Submission status: Accepted

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

Submission status: Accepted

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

left = 0
right = sum(A)
ans = 202020202020 * 200000

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
A=sum(a_list)
ha=0
for i in a_list:
    ha+=i
    if ha>=A/2:
        hb=ha-i
        break
ans=min(ha+ha-A,A-2*hb)
print(ans)

Submission status: Accepted

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

print(min(ans))

Submission status: Accepted

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

S = sum(A)

Sb = 0
for i,a in enumerate(A):
    Sb += a
    if Sb>=S/2:
        break

ans = abs(S-Sb*2)
Sb -= A[i]
ans = min(ans,abs(S-Sb*2))
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
l = list(map(int, input().split()))
suf = list(accumulate(l[::-1]))[::-1] + [0]
res = min(abs(p - suf[i + 1]) for i, p in enumerate(accumulate(l)))
print(res)

Submission status: Accepted

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

left = 0
right = sum(A)
for i in range(N):
    left_new = left + A[i]
    right_new = right - A[i]
    if left_new == right_new:
        print(0)
        exit()
    elif left_new > right_new:
        print(min(left_new - right_new, right - left))
        exit()
    left, right = left_new, right_new

Submission status: Accepted

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

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

s = abs(left-right)

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

print(s)

Submission status: Accepted

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

left = 0
right = sum(A)

ans = float('inf')

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

print(ans)

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    total = sum(A)
    tmp = 0
    ans = float('inf')

    for a in A:
        tmp += a
        #print(abs((total-tmp)-tmp))
        ans = min(abs((total-tmp)-tmp), ans)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

l = [0]*(N-1)
r = [0]*(N-1)

l[0] = A[0]
r[N-2] = A[-1]

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

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

ans = float('inf')
for i, j in zip(l,r):
    ans = min(ans, abs(i-j))
print(ans)

Submission status: Accepted

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

N = int(input())
A = [int(x) for x in input().strip().split()]
length = sum(A)
l = 0
r = length
half = length
for a in A:
    l += a
    r -= a
    half = min(half, abs(r - l))

print(half)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))
num = float("inf")
B = np.cumsum(A)
sA = sum(A)
for i in range(N):
    num = min(num, abs(sA-2*B[i]))
print(num)

Submission status: Accepted

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

a=[A[0]]
for x in A[1:]:
    a.append(a[-1]+x)
sumA=sum(A)
b=[sumA-x for x in a]
print(min([abs(x-y) for x,y in zip(a,b)]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
a = 0
ans = float('inf')
for ai in A:
    a += ai
    ans = min(abs(sumA-a-a), ans)
print(ans)

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    sa = sum(a)
    t = 0
    mt = 10 ** 10
    for i in range(n-1):
        t += a[i]
        sa -= a[i]
        mt = min(mt, abs(t - sa))
    print(mt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
s = sum(A)
left_sum = 0
min_diff = s
for i in range(N):
    left_sum += A[i]
    diff = abs((s-left_sum)-left_sum)
    if min_diff > diff:
        min_diff = diff
print(min_diff)

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    S = string_to_int(inputs[0])
    s_sum = deque([0])
    for s in S:
        s_sum.append(s_sum[-1] + s)
    s_sum.popleft()

    min_diff = None
    for i in range(len(s_sum)):
        front = s_sum[i]
        back = s_sum[-1] - front
        d = abs(front - back)
        if min_diff is None or min_diff > d:
            min_diff = d
    return min_diff


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int, input().split()))
L = sum(A)
S = 0
for i in range(N):
    S+=A[i]
    if S>=math.ceil(L/2):
        break
ans = min(S*2-L, L-(S-A[i])*2)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(i) for i in input().split()]
B=[0]
for a in A:
  B.append(B[-1]+a)
m=B[-1]//2
import bisect
p=bisect.bisect_left(B,m)
l,r=B[p-1],B[p]
ans=min(abs(B[-1]-2*l),abs(B[-1]-2*r))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
# import numpy as np
# import scipy
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])
    
def main():
    n=inp()
    a=inpl()
    S=sum(a)
    pre = 0
    for i in range(n):
        pre+=a[i]
        if pre == S/2:
            print(0)
            return
        if pre > S/2:
            x=pre
            y=pre-a[i]
            break
    x = abs(S-2*x)
    y= abs(S-2*y)
    print(min(x,y))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

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

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

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

min_cost = 1000000000000000000000
for i in range(N + 1):
	min_cost = min(min_cost, abs(sum_A[-1] - sum_A[i] - sum_A[i]))

print(min_cost)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
cs = [0] * N
cs[0] = a[0]
res = float("inf")
for i in range(N - 1):
  cs[i + 1] = cs[i] + a[i + 1]
for i in range(N - 1):
  #print(abs((cs[-1] - cs[i]) - cs[i]))
  res = min(abs((cs[-1] - cs[i]) - cs[i]), res)
print(res)
#print(cs)

Submission status: Accepted

Solution code:
import math
n = input()
nums =list( map(int, input().split(" ")))
total = sum(nums)
part = 0
ans = 10 ** 20
for num in nums:
    part += num
    total -= num
    ans = min(ans,abs(total - part))
print(ans)

Submission status: Accepted

Solution code:
import sys
import math

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

m=sum(a)
tmp=0
ans=m
for i in range(N-1):
    tmp+=a[i]
    ans=min(ans,abs(m-tmp*2))

print(ans)

Submission status: Accepted

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

d = 10**20
l = 0
r = sum(A)
for i in range(N):
    l += A[i]
    r -= A[i]
    d = min(d, abs(l-r))
    
print(d)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a = list(map(int, input().split( )))
s_a = []
sm = 0
for i in range(n):
    sm+= a[i]
    s_a.append(sm)
    
al = s_a[-1]
half = al//2

i = bisect.bisect_left(s_a,half)
ans1 = s_a[i-1]
ans2 = s_a[i]
#print(s_a)
#print(al,ans1,ans2)
print(min(al-2*ans1,2*ans2-al))

Submission status: Accepted

Solution code:

def read_tokens():
    return input().strip().split(' ')

def read_ints():
    return [int(token) for token in read_tokens()]

n, = read_ints()
a = read_ints()


s = sum(a)
p = a[0]
ans = float('inf')

for i in range(1, len(a)):
    r = s - p
    ans = min(ans, abs(p - r))
    p += a[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

accAs = list(accumulate(As))
sumA = accAs[-1]

ans = float('inf')
for i in range(N-1):
    len1 = accAs[i]
    len2 = sumA - len1
    ans = min(ans, abs(len1-len2))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
r = 0
l = sum(a)
mini = l
s = 0
for i in a:
    r += i
    l -= i
    if abs(r-l) < mini:
        mini = abs(r-l)
print(mini)

Submission status: Accepted

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

M = sum(A)
mid = M // 2

t = 0
for i in range(N):
  t += A[i]
  if t >= mid:
    l = t - A[i]
    r = t
    break

ans = abs((M - r) - r)
ans = min(ans, abs((M - l) - l))

print(ans)

Submission status: Accepted

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

    r = sum(A)
    l = 0
    min_diff = r
    for ai in A:
        r -= ai
        l += ai
        min_diff = min(min_diff, abs(r - l))

    print(min_diff)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
ss = [0]
for A in As:
  ss.append(ss[-1]+A)
ds = []
for i in range(N):
  l = ss[i]
  d = abs(2*l-ss[-1])
  ds.append(d)
r = min(ds)
print(r)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

sm = sum(a)
acm = accumulate(a)

ans = float("inf")
for l in acm:
    r = sm - l
    ans = min(ans, abs(r - l))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(raw_input())
m = map(int, raw_input().split())

for i in range(1, n):
  m[i] += m[i - 1]

mini = 1e10
for i in range(0, n - 1):
  diff = abs(m[i] - (m[n - 1] - m[i]))
  if diff < mini:
    mini = diff

print(mini)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, itertools


def solve(N: int, A: "List[int]"):
    B = list(itertools.accumulate(A))
    S = sum(A)
    mn = 1e12
    for i in range(N-1):
        d = abs(B[i] - (S - B[i]))
        mn = min(d, mn)
    print(mn)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
sA = sum(A)
ans = sum(A)
L = 0
for i in A:
    L += i
    R = sA-L
    ans = min(ans,abs(R-L))

print(ans)

Submission status: Accepted

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

s_left = [0]
s_right = [sum(a)]
for i in range(n):
    s_left.append(s_left[-1] + a[i])
    s_right.append(s_right[-1] - a[i])

df = []
for i in range(1, len(s_left) - 1):
    df.append(abs(s_left[i] - s_right[i]))

print(min(df))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
cumsum = itertools.accumulate(a)
ans = 10 ** 12
for i in cumsum:
  ans = min(ans,abs(sum_a - i * 2) )
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a)
tmp = 0
for i in range(n):
    tmp += a[i]
    s -= a[i]
    if tmp >= s:
        t = a[i]
        break

ans = min(abs(tmp-s), abs(tmp-s-a[i]*2))
print(ans)

Submission status: Accepted

Solution code:
#B
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
f = 0
b = S
M = S
for i in range(N-1):
    f+=A[i]
    b-=A[i]
    #print(f,b)
    M = min(M,abs(f-b))
    
print(M)

Submission status: Accepted

Solution code:
def accmulate(arr):
    N = len(arr)
    cs = [0]*(N+1)
    for i in range(N):
        cs[i+1] = cs[i] + arr[i]
    return cs

# [l,r)
def query(cs, l, r):
    return cs[r] - cs[l]

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

left = accmulate(A)
right = accmulate(A[::-1])[::-1]
ans = min(abs(l-r) for l, r in zip(left, right))
print(ans)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    l=sum(a)
    cnt=0
    j=n
    for i in range(n):
        cnt+=a[i]
        if cnt>l/2:
            j=i
            break
    aa=sum(a[:j])
    b=sum(a[j:])
    c=sum(a[:j+1])
    d=sum(a[j+1:])
    print(min(abs(aa-b),abs(c-d)))
main()

Submission status: Accepted

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

l = nums[0]
r = sum(nums[1:])
min_dif = abs(l - r)
for i in range(1, len(nums)):
	l += nums[i]
	r -= nums[i]
	min_dif = min(min_dif, abs(l - r))

print(min_dif)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    S = sum(A)
    cumsum = accumulate(A)
    mini = min(cumsum,key=lambda x: abs(x-S/2))
    ans = abs(mini-S/2)*2
    print(int(ans))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    total = sum(A)
    left = 0
    ans = float("inf")
    for a in A:
        left += a
        ans = min(ans, abs(left * 2 - total))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from bisect import bisect
from itertools import accumulate

n = int(input())
aaa = list(map(int, input().split()))
acc = list(accumulate(aaa))
mid = acc[-1] // 2
i = bisect(acc, mid)
print(min(abs(2 * acc[i] - acc[-1]), abs(2 * acc[i - 1] - acc[-1])))