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:
def A():
    temp = [int(i) for i in input().split()]
    ans = 0
    for n in temp:
        if n<4:
            ans += (4-n)*100000
    if temp[0] == 1 and temp[1] == 1:
        ans += 400000
    print(ans)

def B():
    N = int(input())
    lengths = [int(i) for i in input().split()]
    total = sum(lengths)
    temp_sum = lengths[0]
    ans = float('inf')
    for i in range(1, N):
        ans = min(ans, abs(total-2*temp_sum))
        #print(temp_sum)
        temp_sum += lengths[i]
    
    print(ans)
        
B()

Submission status: Accepted

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



sumA = [0]
buf = 0
for i in A:
    buf += i
    sumA.append(buf)

ans = []
for i in sumA:
    if i >= sumA[-1] / 2:
        ans.append(temp)
        ans.append(i)
        break
    temp = i

print(int(min(abs(sumA[-1] / 2 - ans[0]), abs(sumA[-1] / 2 - ans[1])) * 2))

Submission status: Accepted

Solution code:
from itertools import accumulate
 
n = int(input())
A = list(map(int, input().split()))
 
prefix = list(accumulate(A))
total = prefix[-1]
seq = min(range(n), key=lambda x: abs(prefix[x]-total/2))
alt = prefix[seq]
print(abs(total - 2*alt))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = sum(a)
h = l/2
b, c = 0, 0
ans = 0
for i in a:
    b = c
    c += i
    if c == h:
        print(0)
        break
    if h < c:
        ans = c-l+c if c-h<h-b else l-b-b
        print(ans)
        break

Submission status: Accepted

Solution code:
import bisect 
n = int(input())
a = list(map(int, input().split()))
if n == 2:
  print(abs(a[0]-a[1]))
  exit()
com_a = [0 for _ in range(n)]
com_a[0] = a[0]
for i in range(1, n):
  com_a[i] = a[i] + com_a[i-1]

sum_a = sum(a)
middle  = 0
left = 0
for i in range(n):
  if com_a[i] > sum_a/2:
    if com_a[i] - sum_a/2 > sum_a/2 - com_a[i-1]:
      middle = i-1 
    else:
      middle = i
      left += a[i]
    break
  left += a[i]
#middle = bisect.bisect_left(com_a, sum_a/2)
right = sum_a - left
print(abs(left-right))

Submission status: Accepted

Solution code:
import numpy as np

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = np.array(a)
    sa = sum(a)
    half = int(sa/2)
    tmp = 0
    ans = 0
    flag = 0

    for i in range(n):
        tmp = tmp + a[i]
        if tmp >= half:
            if 2*tmp - sa < sa - 2*(tmp-a[i]):
                break
            else:
                tmp = tmp - a[i]
                flag = 1
                break
    
    if flag == 0:
        ans = 2*tmp - sa
    else:
        ans = sa - 2*tmp
        
    print(int(ans))



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = sum(a)
c = 0
for i in range(len(a)):
  c += a[i]
  if c >= b/2:
    d = i
    sum_b = c
    sumsum_b = c - a[i]
    break
if d == 0:
  ans = 2*a[0] - b
elif d == n-1:
  ans = b - 2*sumsum_b
else:
  e = sum_b
  f = sumsum_b
  if e + f >= b:
    ans = b - 2*f
  else:
    if sum_b - d - 1 < 2*e - b:
      ans = b - 2*f
    else:
      ans = 2*e - b
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

acc_A = [0]
for i in range(N):
    acc_A.append(acc_A[-1] + A[i])
acc_A.append(acc_A[-1])
half = acc_A[-1] / 2
mid = bisect.bisect_left(acc_A, half)

if acc_A[mid] == half:
    ans = 0
else:
    ans = min(abs((acc_A[mid] - acc_A[0]) - (acc_A[-1] - acc_A[mid])),
              abs((acc_A[mid - 1] - acc_A[0]) - (acc_A[-1] - acc_A[mid - 1])))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
cost=float('inf')
tmp=0
ret = 0
for i in range(n):
    tmp+=a[i]
    ret=abs(tmp-(suma-tmp))
    cost=min(cost,ret)

print(cost)

Submission status: Accepted

Solution code:
N=int(input())

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

arr = []

a = 0
total = sum(List)

for i in range(N):
  a = a + List[i]
  b = total - a
  
  if a == b:
    ans = 0
  elif a > b:
    ans = a - b
  elif b > a:
    ans = b - a
  
  arr.append(ans)


print(min(arr))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
costs = [0 for x in range(N-1)]
mem = [0 for x in range(N-1)]

for i in range(1, N):
  if i == 1:
    mem[i-1] = A[i-1] * 2
  else:
    mem[i-1] = mem[i-2] + A[i-1] * 2
  costs[i-1] = abs(total - mem[i-1])
  
print(min(costs))

Submission status: Accepted

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

bar_len = sum(A)

target = bar_len / 2

value = 0
index = 0
flag = 0
for i in range(len(A)):
    value += A[i]
    if value >= target:
        error1 = value - target
        error2 = target + A[i] - value
        if error1 < error2:
            top_list = A[: i+1]
            back_list = A[i+1 :]
            break
        else:
            top_list = A[: i]
            back_list = A[i :]
            break

top = sum(top_list)
back = sum(back_list)

result = max(top - back, back - top)
print(result)

Submission status: Accepted

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


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

sum2 = 0
for i in range(n):
    sum2 += a[i]
    x = sum2*2 - sum
    if x >= 0:
        y = sum - (sum2-a[i])*2
        if x <= y:
            print(x)
            break
        else:
            print(y)
            break

Submission status: Accepted

Solution code:
import sys
from pprint import pprint

def solve(n, a):
    all_s = sum(a)
    half = all_s / 2
    s = 0
    for a_i in a:
        s += a_i
        if s >= half:
            if s - half >= half - (s - a_i):
                print(all_s - 2*s + 2*a_i)
            else:
                print(2*s - all_s)
            return

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    a = list(map(int, sys.stdin.readline().strip().split(" ")))
    solve(n, a)

Submission status: Accepted

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

total_length = sum(A)
left_length = 0
for i in range(N):
    left_length += A[i]
    if left_length == total_length / 2:
        print(0)
        break
    elif left_length > total_length / 2:
        right_length = total_length - left_length
        left_length -= A[i]
        if left_length < right_length:
            print(left_length + A[i] - right_length)
        else:
            print(right_length + A[i] - left_length)
        break

Submission status: Accepted

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])
    A = tuple(map(int, args[1].split()))

    left = 0
    right = sum(A)

    ret = 0
    for i in range(0, n):
        r_g = right - left
        left += A[i]
        right -= A[i]
        if left == right:
            break
        elif left > right:
            l_g = left - right
            ret = min(r_g, l_g)
            break

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

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

s = 0
S = 0
half = sum(ls_a)/2

for i in range(n):
    s = S
    S += ls_a[i]
    if S > half:
        break

print(int(min(abs(half - s)*2, abs(half - S)*2)))

Submission status: Accepted

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

l = sum(a)

sumation = 0
for i in range(n):
    sumation += a[i]
    if sumation >= l/2:
        print(min(int((sumation - l/2) * 2), int((l/2 - sumation + a[i]) * 2)))
        break

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
total = sum(A_list)
half = total // 2
l = 0
r = len(A_list) - 1
l_len = 0
r_len = 0
while True:
	new_len = l_len + A_list[l]
	if new_len <= half:
		l_len = new_len
		l += 1
	else:
		break

while True:
	new_len = r_len + A_list[r]
	if new_len <= half:
		r_len = new_len
		r -= 1
	else:
		break

if l == r:
	cut_l = r_len + A_list[r] - l_len
	cut_r = l_len + A_list[l] - r_len
	print(min(cut_l, cut_r))
else:
	print(0)

Submission status: Accepted

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

bou_half = sum(List)/2

bou_sum = 0
for i in List:
    bou_sum += i
    if bou_sum == bou_half:
        print(0)
        exit()
    if bou_sum > bou_half:
        if bou_sum - bou_half < bou_half - bou_sum + i: # 中心が右より
            print(int((bou_sum - bou_half) // 0.5))
            exit()
        else: 
            print(int((bou_half - bou_sum +i) // 0.5))
            exit()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
t = sum(a)
tmp = 0
m = 0
for item in a:
    if (tmp + item) >= t // 2:
        m = item
        break
    tmp += item

ans1 = abs(t-2*tmp)
tmp += m
ans2 = abs(t-2*tmp)
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(j) for j in input().split()]

su1 = 0
su2 = sum(arr)
abc = []
for i in range(n):
	su1 += arr[i]
	su2 -= arr[i]
	abc += [abs(su1-su2)]

print(min(abc))

# print(abc)

Submission status: Accepted

Solution code:
from collections import deque

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

left = a.popleft()
right = a.pop()

while a:
    if left < right:
        left += a.popleft()
    else:
        right += a.pop()

ans = abs(left - right)
print(ans)

Submission status: Accepted

Solution code:
import numpy
n=int(input())
l=list(map(int,input().split()))
res=numpy.cumsum(l)
ans=float("inf")
su=res[-1]
for i in res:
  ans=min(ans,abs(2*i-su))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
AS = sum(A)
H = AS/2

wa = 0
for i in range(N):
    wa+=A[i]
    if wa>H:
        num = i
        R = wa
        L = wa-A[i]
        break

d = min(abs(H-R),abs(H-L))

print(int(d*2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cntL = 0
s = sum(a) // 2
cntR = 0
for i in range(len(a)):
    if cntL + a[i] < s:
        cntL += a[i]
    else:
        cntR = cntL
        cntL += a[i]
        break
# print(cntL)
print(min(abs((sum(a) - cntL) - cntL),abs(sum(a) - cntR) - cntR))
# print(abs(sum(a) - cntR) - cntR)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
s=sum(a)
i=0
while s>0:
  s-=2*a[i]
  i+=1
d=min(abs(s),s+2*a[i-1])
print(d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate, count
from bisect import bisect_right

input()
a = [0] + list(accumulate(map(int, input().split())))
i = bisect_right(a, a[-1] // 2)
print(min(2 * a[i] - a[-1], a[-1] - 2 * a[i - 1]))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
half=sum(a)/2
count=0
mid=0
tag=0
for i in range(len(a)):
  count+=a[i]
  if count>=half:
    mid=a[i]
    tag=i
    break
right=sum(a[:(tag+1)])-half
left=mid-right
alpha=min(right,left)
if alpha==0:
  print(0)
else:
  print(int(alpha*2))

Submission status: Accepted

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

length = 0
lengths = []
for a in aaa:
  length += a
  lengths.append(length)
  
diffs = []
for l in lengths:
  diff = abs(l - (length - l))
  diffs.append(diff)
    
print(min(diffs))

Submission status: Accepted

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

a = sum(A)
b = a//2

s = 0

for i in range(N):
  s+=A[i]
  if b<=s:
    s_h = s - A[i]
    break

t = a - s
t_h = a - s_h
if s_h>=t_h:
  ans_h = s_h-t_h
  
else:
  ans_h = t_h-s_h
if s>=t:
  ans = s-t
  ans = min(ans,ans_h)
  print(ans)
  
else:
  ans = t-s
  ans = min(ans,ans_h)
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(x) for x in input().split())
key = sum(a)

value = a[0]
for i in range(1, n):
    judge = value + a[i]
    if judge <= key/2:
        value = judge
    else:
        ans = min([abs(key-value*2), abs(key-judge*2)])
        break
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
all_sum=sum(A)
tmp=0
tmp1=0
for i in A:
  if all_sum//2>tmp:
    tmp+=i
    tmp1=i
  else:
    break
if all_sum-tmp<(tmp-tmp1):
  print(abs(tmp1+all_sum-tmp-(tmp-tmp1)))
else:
  print(abs(tmp-abs(all_sum-tmp)))

Submission status: Accepted

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

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

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

x = 100000000000
for i in range(N):
    y = abs(L1[i] - L2[-i-1])
    x = min(x, y)

print(x)

Submission status: Accepted

Solution code:
import copy
from bisect import bisect_left
N=int(input())
la=list(map(int,input().split()))
ll=copy.copy(la)
#lr=copy.copy(la)
for i in range(1,N):
    ll[i]+=ll[i-1]
#for i in range(N-2,-1,-1):
#    lr[i]+=lr[i+1]
inl=bisect_left(ll,ll[-1]/2)
ds=ll[-1]/2-ll[inl-1]
db=ll[inl]-ll[-1]/2
if ds<=db:
    print(ll[-1]-ll[inl-1]*2)
else:
    print(ll[inl]*2-ll[-1])

Submission status: Accepted

Solution code:
num=int(input())
mm=[int(s) for s in input().split()]
total=sum(mm)


count=0
for j,i in enumerate(mm):
  count+=i
  if count>=total//2:
    flag=j
    break

q=sum(mm[flag+1:])

p=count-q


mm.reverse()

count=0
for j,i in enumerate(mm):
  count+=i
  if count>=total//2:
    flag=j
    break

q=sum(mm[flag+1:])
p2=count-q

if p>=p2:
  print(p2)
else:
  print(p)

Submission status: Accepted

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

Submission status: Accepted

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

import numpy as np

A=np.array(A)

from bisect import bisect_left
s = bisect_left(A.cumsum(), A.sum()/2)

if A.sum()/2 in A.cumsum():
    print(0)
else:
    print(int(min(abs(A.sum()/2 - A[0:s].sum()) * 2, abs(A.sum()/2 - A[0:s+1].sum()) * 2)))

Submission status: Accepted

Solution code:
n=int(input())
S=list(map(int, input().split()))
Sum=sum(S)
#print(Sum)
wa=0
count=0
for i in range(n):
    if wa<Sum/2:
        wa=wa+S[i]
        count=count+1
x=int(abs(Sum-2*wa))
wa=wa-S[count-1]
        #print(wa,Sum-wa)
y=int(abs(Sum-2*wa))
print(min(x,y))

Submission status: Accepted

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

Submission status: Accepted

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

lf = 0
lb = 0
front = 0
back = n-1
for i in range(n):
    if lf <= lb:
        lf += a[front]
        front += 1
    else:
        lb += a[back]
        back += -1
if lf <= lb:
    h = lb-lf
else:
    h = lf-lb
print(h)

Submission status: Accepted

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

lr = 0
ll = sum(a)
index = 0

while lr + a[index] < ll - a[index]:
    lr = lr + a[index]
    ll = ll - a[index]
    index += 1

diff = min(ll - lr, lr + 2 * a[index] - ll)
print(diff)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = sum(a)
l = 0
r = 0
x = 0
ans = 0
for i in a:
  l += i
  r = b - l
  if l==r:
    break;
  elif l>r:
    ans = min(l-r,y-x)
    break;
  x = l
  y = r
print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
b=sum(a)
c=0
d=2020202020
for i in a:
    c+=i
    d=min(d,abs(c-b/2))
print(int(d*2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np
import bisect
N = int(input())
A = np.array(readline().split(),np.int64)
S = sum(A)
#print(S)
AA = np.cumsum(A)
half = S // 2
std = bisect.bisect_left(AA,half)
#print(std)
#print(AA)
if S % 2 == 0 and S // 2 in AA:
  print(0)
  exit()
#if N % 2 == 1:
#  a = AA[N // 2]
#  b = AA[-1] - a
#else:
#  a = AA[N // 2 - 1]
#  b = AA[-1] - a
a = AA[std-1]
b = AA[-1] - a
aa = AA[std]
bb = AA[-1] - aa
print(min(abs(a - b),abs(aa - bb)))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    *a, = map(int, input().split())
    cs = [a[0]]*n
    for i in range(1, n):
        cs[i] = a[i]+cs[i-1]
    o = float("inf")
    for i in range(n):
        l, r = cs[i], cs[-1]-cs[i]
        tmp = max(l, r) - min(l, r)
        o = tmp if tmp<o else o
    print(o)

if __name__ == "__main__":
    main()

Submission status: Accepted

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


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = tuple(map(int, input().split()))

    a = tuple(accumulate(A))
    n = sum(A) // 2
    i = bisect(a, n)
    case1 = abs(sum(A[:i]) - sum(A[i:]))
    case2 = abs(sum(A[:i+1]) - sum(A[i+1:]))
    print(min(case1, case2))


main()

Submission status: Accepted

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = float("inf")
    su = sum(a)
    s = 0
    for i in range(n):
        s += a[i]
        su -= a[i]
        ans = min(ans, abs(s - su))
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

pref = list(accumulate(a))

tab = [0] * n
for i in range(n):
	tab[i] = abs(pref[i] - (pref[-1] - pref[i])) 
print(min(tab))

Submission status: Accepted

Solution code:
n = int(input())
a_lis = list(map(int, input().split()))
all_sum = sum(a_lis)
half_sum = all_sum // 2
tmp_sum = 0
i = 0
while tmp_sum < half_sum:
    tmp_sum += a_lis[i]
    i += 1    
sep = i - 1
if sep == n:
    print(a_lis[-1] - sum(a_lis[:-1]))
else:
    print(min(tmp_sum - (all_sum - tmp_sum), all_sum - (tmp_sum - a_lis[sep])- (tmp_sum - a_lis[sep])))

Submission status: Accepted

Solution code:
import numpy as np

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

A = np.array(A)
L = np.sum(A)

l=0
ans = -1

for i in range(N):
    l += A[i]
    if l == L/2:
        ans = 0
        break
    elif l > L/2:
        Am = A[i]
        break

if ans < 0:
    L2 = L - l
    L1 = l - Am
    ans = int(min(L1+Am-L2, L2+Am-L1))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = raw_input().split()
A = [0]*N

for i in range(N):
    A[i] = int(s[i])

#print N, A

h = 0
t = N-1
lh = 0
lt = 0

for i in range(N):
    if (lh > lt):
        lt += A[t]
        t -= 1
    else:
        lh += A[h]
        h += 1
    #print h, t, lh, lt, A[h+1:t-1]

print abs(lh-lt)

Submission status: Accepted

Solution code:
import sys
x = int(input())
a = list(map(int,input().split()))

sum=sum(a)
k=0
for i in range(x-1):
  if sum/2-k-a[i]<=a[i+1]:
    print(int(2*min(sum/2-k-a[i],k+a[i]+a[i+1]-sum/2)))
    sys.exit()
  else:
    k+=a[i]

Submission status: Accepted

Solution code:
from math import *
n=int(input())
a=list(map(int,input().split()))
ans=100000000000000000000000000000
sum=0
c=0
b=[]
cnt=[]
for x in a:
    sum+=x
    if(x==1):
        c+=1
    b.append(sum)
    cnt.append(c)
for i in range(0,n):
    one=b[i]
    two=sum-b[i]
    res=(one+two)//2
    diff1=abs(one-res)
    diff2=abs(two-res)
    ans=min(ans,diff1+diff2)

print(ans)

Submission status: Accepted

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

over=False
all_su=sum(nums)
su=0
for i in range(N):
  nsu=su+nums[i]
  if nsu>all_su/2:
    break
  else:
    su=nsu
if all_su/2-su > nsu-all_su/2:
  print(2*nsu-all_su)
else:
  print(all_su-2*su)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
chk=sum(a)
k=chk//2
k1=k+1
k2=k+2
k3=k-1
k4=k-2
x=y=z=0
for i,j in enumerate(a):
    z=i
    x+=j
    if x>=k:
        break
if x==k and chk%2==0:
    print(0)
else:
    ans=min(abs(x-k)+abs((chk-x)-k),abs(x-k1)+abs((chk-x)-k1),abs(x-k2)+abs((chk-x)-k2),abs(x-k3)+abs((chk-x)-k3),abs(x-k4)+abs((chk-x)-k4))
    x=y=z=0
    for i,j in enumerate(a):
        z=i
        if x+j>=k:
            break
        x+=j
    ans2=min(abs(x-k)+abs((chk-x)-k),abs(x-k1)+abs((chk-x)-k1),abs(x-k2)+abs((chk-x)-k2),abs(x-k3)+abs((chk-x)-k3),abs(x-k4)+abs((chk-x)-k4))
    print(min(ans,ans2))

Submission status: Accepted

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

h = sum(a)/2#4.5
ans1 = 0
ans2 = 0
ls = [a[0]]

for i in range(1,n):
    ls.append(ls[i-1]+a[i])
        
        
for i in range(len(ls)):
    if ls[i] > h:
        ans1 = abs(ls[i]-sum(a[i+1:]))
        ans2 = abs(ls[i-1]-sum(a[i:]))
        print(min(ans1,ans2))
        break

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = list(map(int, input().split()))
a = [0] + a
lacc = list(itertools.accumulate(a))
a.reverse()
racc = list(itertools.accumulate(a))
racc = [0] + racc[:-1] 
racc.reverse()
ans = racc[0]
for i in range(n+1):
    ans = min(ans, abs(racc[i] - lacc[i]))
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N=int(input())
    AL=[int(x) for x in input().split()]
    total=sum(AL)
    target=total/2

    now=0
    for i,a in enumerate(AL):
        now+=a
        if now>target:
            ans1=(now-target)*2
            ans2=(target-now+a)*2
            return int(min(ans1,ans2))
print(solve())

Submission status: Accepted

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

sum_a_list = sum(A_list)
middle_num = sum_a_list/2

temp_sum = 0

middle_block = -1

for j in range(len(A_list)):
    temp_sum += A_list[j]
    if temp_sum == middle_num:
        print(0)
        exit(0)
    if temp_sum > middle_num:
        middle_block = j
        break


diff_go_right = abs(sum(A_list[:middle_block]) - sum(A_list[middle_block:]))
diff_go_left = abs(sum(A_list[:middle_block+1]) - sum(A_list[middle_block+1:]))

print(min(diff_go_right, diff_go_left))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate, count

input()
a = [0] + list(accumulate(map(int, input().split())))
i = next(i for i in count() if 2 * a[i] > a[-1])
print(min(2 * a[i] - a[-1], a[-1] - 2 * a[i - 1]))

Submission status: Accepted

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

A_len = sum(A)

tyuuou = float(A_len) / 2


index = 0
s = 0
flag = 0 # 0 left, 1 right
f = 0
nokori = 0
for i in range(N-1):
    s += A[i]
    if s >= tyuuou:
        index = i
        nokori = A_len - s
        if s-A[i] > nokori:
            index -= 1
            s -= A[i]
            nokori += A[i]
        f = 1
        break
if f==0:
    index = N-2
    nokori = A_len - s

print(abs(nokori-s))

Submission status: Accepted

Solution code:
import numpy as np

inf = float("inf")

n = int(input())
a = np.array(input().split()).astype(float)
b = np.zeros(n).astype(float)
b[0] = a[0]
for i in range(1, n):
    b[i] = b[i - 1] + a[i]
sum = b[len(b) - 1]
mini = inf
split = 0
for i in range(n - 1):
    if mini > abs(b[i] - (sum - b[i])):
        split = b[i]
        mini = abs(b[i] - (sum - b[i]))
if abs(split - (sum - split)) * 2 % 2 == 1:
    print(int(abs(split - (sum - split)) * 2))
else:
    print(int(abs(split - (sum - split))))

Submission status: Accepted

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

Submission status: Accepted

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

result = [[a[0], sum(a[1:])]]
for i in range(1, n):
    result.append([result[-1][0]+a[i], result[-1][1]-a[i]])
h = []
for i in range(len(result)):
    h.append(abs(result[i][0]-result[i][1]))
print(min(h))

Submission status: Accepted

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

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

cnt=0
sum_r=0
sum_l=sum(a)
sa=[]
for i in range(n):
    sum_r+=a[i]
    sum_l-=a[i]
    sa.append(abs(sum_r-sum_l))
print(min(sa))

Submission status: Accepted

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

leftSum = 0
rightSum = 0
K = N
L = 0

for i in range(0,N):
    if(leftSum < rightSum):
        leftSum = leftSum + Ai[L]
        L = L+1
    else:
        rightSum = rightSum + Ai[K-1]
        K = K-1
print(abs(leftSum-rightSum))

Submission status: Accepted

Solution code:

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

S=sum(A)
T=0

for i in range(N):
    SS=S-A[i]
    TT=T+A[i]
    if SS-TT<=0:
        if abs(SS-TT)<abs(S-T):
            ans=abs(SS-TT)
            #print(SS,TT)
            #print("a")
        else:
            ans=abs(S-T)
            #print("b")
        break
    T+=A[i]
    S-=A[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

l = 0
r = N - 1

sum_l = A[l]
sum_r = A[r]

while r - l > 1:
    #print(l, r, sum_l, sum_r)
    if sum_l < sum_r:
        l += 1
        sum_l += A[l]
    else:
        r -= 1
        sum_r += A[r]
        
print(abs(sum_l-sum_r))

Submission status: Accepted

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

sumA = sum(A)/2

i = 0
j = 0

while i < sumA:
    i = i+A[j]
    j += 1

ans1 = abs(sum(A[0:j]) - sum(A[j:N]))
ans2 = abs(sum(A[0:j-1]) - sum(A[j-1:N]))
ans3 = abs(sum(A[0:j+1]) - sum(A[j+1:N]))
print(min(ans1,ans2,ans3))

Submission status: Accepted

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

Submission status: Accepted

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

total_length = 0
for i in range(n):
    total_length += a[i]

dis = 10000000000000000000000000000000000000000
sum_length = 0
for i in range(n):
    sum_length += a[i]
    if abs(total_length/2 - sum_length) < dis:
        dis = abs(total_length/2 - sum_length)
    else:
        sum_length -= a[i]
        break
print(abs(total_length - 2 * sum_length))

Submission status: Accepted

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

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    L = 0
    R = sum(A)
    for a in A:
        if L+a <= R-a:
            L += a
            R -= a
        elif L == R:
            c = 0
            break
        else:
            c = a
            R -= a
            break

    res = min(L+c-R, R+c-L)
    return str(res)

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    M=sum(A)/2
    l=0
    for i in range(N):
        o=l
        l+=A[i]
        if l>=M:
            break
    if (l-M) > (M-o):
        l=o
    print(int(abs(l-M)*2))

    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:
import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
INF = float('inf')

N = int(input())
Alist = inpl()

i = 0
j = 1
x = 0
y = 0
while i + j != N+1 :
    if y >= x:
        x += Alist[i]
        i += 1
    elif y < x:
        y += Alist[-j]
        j += 1

print(abs(x-y))