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

dp = [sum(A)]
for i in range(N):
  dp.append(dp[i]-2*A[i])
import numpy as np
print(min(np.abs(dp)))

Submission status: Accepted

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

forward = 0
backward = N-1

sum = list[forward]
sum2 = list[backward]
while (True):
    if backward - forward == 1:
        break
    if sum < sum2:
        forward += 1
        sum += list[forward]
    else:
        backward -= 1
        sum2 += list[backward]
print(abs(sum - sum2))

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))
length = sum(List)
sum = 0
center = length / 2
centerIdx = 0
for idx in range(len(List)):
    sum += List[idx]
    if center <= sum:
        centerIdx = idx
        break

if center == sum:
    print(0)
else:
    if (centerIdx == 0):
        print(sum * 2 - length)
    elif (sum -center) < (center - (sum - List[centerIdx])):
        print(sum * 2 - length)        
    else:       
        print(length - (sum - List[centerIdx]) * 2)

Submission status: Accepted

Solution code:
import numpy as np
import math
n = int(input())
li = list(map(int,input().split()))
half = sum(li) / 2

res = np.cumsum(li)
idx = np.abs(res - half).argmin()
if res[idx] == half:
    ans = 0
else:
    ans = abs(res[idx] - (res[-1] - res[idx]))
print(int(ans))

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    A=list(map(int,input().split()))
    total=sum(A)
    tmp=0
    for a in A:
        tmp+=a
        if 2*tmp>=total:
            break
    re_A=reversed(A)
    re_tmp=0
    for re_a in re_A:
        re_tmp+=re_a
        if 2*re_tmp>=total:
            break
    if tmp>re_tmp:
        print(2*re_tmp-total)
    else:
        print(2*tmp-total)
        
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from math import *
from collections import *
import sys
sys.setrecursionlimit(10**9)
n = int(input())
a = list(map(int,input().split()))
pre = [0 for i in range(n)]
pre[0] = a[0]
for i in range(1,n):
	pre[i] = pre[i-1] + a[i]
minn = abs(pre[-1]-2*pre[0])
for i in range(1,n):
	minn = min(minn,abs(pre[-1]-2*pre[i]))
print(minn)

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

#import bisect

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def main():
#    h,w,a,b = map(int, input().split())
    n=int(input())
    a=list(map(int,input().split()))
    s=0
    suma=sum(a)
    temp=0
    for i in range(len(a)):
        temp+=a[i]
        if temp>= suma//2:
            p=i
            break
        p=i
    x=temp
    y=suma- temp
#    print(x,y)
    if abs(x-y)>abs(temp-a[p]-abs(suma-temp+a[p])):
        x=temp-a[p]
        y=suma-temp+a[p]
#    print(temp-a[p],suma-temp+a[p])
#    print(x, y)
    s=min(abs(suma//2-x),abs(suma//2-y))
    s*=2
    if suma%2!=0:
        s+=1
    print(s)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
forward = [0]*N
forward[0] = A[0]
total = sum(A)
m = 10**12
for i in range(1, N):
    forward[i] = forward[i-1] + A[i]
for i in range(N-1):
    tmp = total - forward[i]
    diff = abs(tmp - forward[i])
    m = min(diff, m)

print(m)

Submission status: Accepted

Solution code:
import sys
n = int(input())

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

count = 0
cost = 0
temp = 0
temp2 = 0
ans1 = 0
ans2 = 0
nokori = 0
sum = sum(a)
half = int(sum/2)
if(sum%2 == 1):
  cost = 1

for i in (a):
  count += i
  if(count == half):
    print(cost)
    sys.exit()
  elif(count > half):
    temp = count
    nokori = sum - temp
    ans1 = temp - nokori
    #print(temp,nokori,ans1)
    break
    
#print(ans1)

a.reverse()
count = 0
#print(half)
for j in (a):
  count += j
  if(count > half):
    temp2 = count
    nokori = sum - temp2
    ans2 = temp2 - nokori
    if(ans1 > ans2):
      print(ans2)
    else:
      print(ans1)
    break

Submission status: Accepted

Solution code:
N = int(input())
a=list(map(int,input().split()))
ans=2020202020
s=sum(a)
mae=a[0]
for i in range(1,N):
    mae+=a[i]
    ato=s-mae
    if mae-ato>0:
        ans=min(abs(sum(a[:i])-sum(a[i:])),abs(sum(a[:i+1])-sum(a[i+1:])))
        break
    else:
        ans=min(ans,abs(mae-ato))
print(ans)

Submission status: Accepted

Solution code:
import math
cnt = int(input())
data  = list(map(int, input().strip().split()))

avr = int(math.floor(sum(data)/2))
i = 0
sum_ = 0
while sum_ < avr:
    sum_ += data[i]
    i += 1
i = i - 1

left = sum(data[i+1:])
right = sum(data[i+1:])

#if left - right

'''
print("---")
print(i)
print(left)
print(right)
'''
diff = sum(data[:i+1]) - sum(data[i+1:])
diff = abs(diff)
diff2 = sum(data[:i]) - sum(data[i:])
diff2 = abs(diff2)
#print("diff:"+ str(diff))
#print("diff2:"+ str(diff2))
if diff < diff2:
    print(diff)
else:
    print(diff2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
s = sum(a)
al = []

sum1 = 0

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

minDiffIdx = al.index(min(al))

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

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

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

# d1 = diff1
# d2 = diff2

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

Submission status: Accepted

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

all_length = sum(length_list)

half_length = all_length / 2
before_sum = 0
after_sum = 0
for ax in length_list:
    if before_sum+ax < half_length:
        before_sum += ax
        continue
    else:
        after_sum = before_sum + ax
        break
before_diff = all_length - before_sum 
after_diff = all_length - after_sum

if before_diff - before_sum < after_sum - after_diff:
    print(before_diff - before_sum)
else:
    print(after_sum - after_diff)

Submission status: Accepted

Solution code:
a=int(input())
b=list(map(int,input().split()))
money=0
kei=0
cnt1=0
cnt2=0
if a%2==1:
    for i in range(0,a,1):
        kei+=b[i]
    f=0
    while cnt1<=kei/2:
        cnt1+=b[f]
        f+=1
    cnt1-=b[f-1]
    g=a-1
    while cnt2<=kei/2:
        cnt2+=b[g]
        g-=1
    cnt2-=b[g+1]
else:
    for i in range(0,a,1):
        kei+=b[i]
    f=0
    while cnt1<=kei/2:
        cnt1+=b[f]
        f+=1
    cnt1-=b[f-1]
    g=a-1
    while cnt2<=kei/2:
        cnt2+=b[g]
        g-=1
    cnt2-=b[g+1]
if cnt1==kei/2:
    print(0)
else:
    print(abs(b[f-1]-abs(cnt1-cnt2)))

Submission status: Accepted

Solution code:
N = int(input())
length = [int(i) for i in input().split()]
pos = []
s1 = sum(length)
s2 = 0

for a in range(len(length)):
    pos.append(max(s1,s2) - min(s1,s2))
    s1 -= length[a]
    s2 += length[a]
    # print("pos " + str(a)+":",sum(length[:a]), sum(length[a:]))
# print(pos)

print(min(pos))

Submission status: Accepted

Solution code:
n=int(input())

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

if n==2:
  print(abs(r[0]-r[1]))
  exit()
 

#print(r)

temp=sum(r)

goukei=0
for item in r:
  if goukei+item>=temp/2.0:
    
    if abs(temp-goukei-goukei)<=abs((temp-(goukei+item))-(goukei+item)):
      print(abs(temp-goukei-goukei))
    else:
      print(abs((temp-(goukei+item))-(goukei+item)))
    exit()
  goukei=goukei+item
  
goukei=goukei-item
  
print(abs(temp-(goukei-item)))

Submission status: Accepted

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

sum_a = sum(list_a)
mid_sum_a = sum_a / 2

sum_first = 0
i = 0
result = 0

while sum_first < mid_sum_a:
    sum_first += list_a[i]
    i += 1
if sum_first == mid_sum_a:
    result = 0
if sum_first - mid_sum_a <= mid_sum_a - (sum_first - list_a[i - 1]):
    result = (sum_first - mid_sum_a) * 2
else:
    result = (mid_sum_a - (sum_first - list_a[i - 1])) * 2
print(int(result))

Submission status: Accepted

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

Suu=sum(A_list)
S=Suu*0.5
import sys

sys.setrecursionlimit(2000)
def my_bisect(l:list,left_true=False):
    def bis(begin:int,end:int):
        # print("{}:{}".format(begin,end))
        if end-begin<=1:
            if begin==0:
                if A_list[0]>S:
                    return 0
                else:return 1
            # 
            # print(sum(A_list[0:begin+1]))
            # print(sum(A_list[0:end+1]))
            if begin==0:
                B=A_list[begin+1]
            else:
                B=sum(A_list[0:begin+1])
            if B>S==True:
                return begin
            else:
                return end

        v=int((end+begin)/2//1)
        # print("v="+str(v))
        res=sum(A_list[0:v+1])>S
        # print(sum(A_list[0:v+1]))
        # 
        # print(res)
        if not res:
            return bis(v,end)
        elif  res:
            return bis(begin,v)
        else:
            print("what")
    return bis(0,len(l)-1)

m=my_bisect(A_list)

# print(m)
if m==0:
    print(A_list[0])
else:
    if m==1:
        nagasa=A_list[0]
    else:
        nagasa=sum(A_list[:m])
    print(min(abs((nagasa+A_list[m])*2-Suu),abs(nagasa*2-Suu)))

Submission status: Accepted

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

total = sum(a)
mid = int((total + 1) / 2)

temp_sum = 0
for i in range(len(a)):
    temp_sum += a[i]
    if temp_sum >= mid:
        over_idx = i
        break

diff1 = abs(sum(a[:over_idx]) - sum(a[over_idx:]))
diff2 = abs(sum(a[:over_idx+1]) - sum(a[over_idx+1:]))

ans = min(diff1, diff2)

print(ans)

Submission status: Accepted

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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


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

total=sum(a)

mindif=1e30
leftbest=-1

l=0
r=total

for i in range(n):
    l+=a[i]
    r-=a[i]
    if(abs(l-r)<mindif):
        mindif=abs(l-r)
        
print(mindif)

Submission status: Accepted

Solution code:
n = int(input())
ls_bf = list(map(int,input().split()))
ls_af = []
ls_sum = 0
am = 0
for i in range(n):
    ls_af.append(ls_bf[i] + ls_sum)
    ls_sum += ls_bf[i]

for i in ls_af:
    if abs(2*i - ls_af[n-1]) < abs(2 * am - ls_af[n-1]):
        am = i
# print(ls_af)
# print(ls_af[n-1])
print(abs(2*am - ls_af[n-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

sum = 0
for i in range(len(A)):
    sum += A[i]
#print(sum)
check_sum = 0
for i in range(len(A)):
    check_sum += A[i]
    if check_sum == sum/2:
        print(0)
        exit()
    if check_sum > sum / 2:
        if i+ 1 == len(A):
            print(A[i] - (check_sum - A[i]))
            exit()
        else:
            if (abs(2 * (check_sum - A[i]) - sum) < abs(2 * check_sum - sum)):
                print(abs(2 * (check_sum - A[i]) - sum))
                exit()
            #print("chek")
            else:
                print(abs(2 * check_sum - sum))
                exit()

Submission status: Accepted

Solution code:
n=int(input())
bar=list(map(int,input().split()))
hev=sum(bar)
hev_sum=[bar[0]]*n 
for i in range(1,len(bar)):
    hev_sum[i]=hev_sum[i-1]+bar[i]
if (hev/2) in hev_sum:
    print(0)
else:
    for i in range(0,len(bar)-1):
        if hev_sum[i]<(hev/2) and hev_sum[i+1]>(hev/2) :
            ans=i
    first=hev_sum[ans]
    second=hev-first-bar[ans+1]
    ab=abs(first-second)
    print(bar[ans+1]-ab)

Submission status: Accepted

Solution code:
import math

n = int(input())

a = input().split()
#あくまで文字列として格納されている

cnt = 2020202022

length_sum = 0
length_sum_start_left = 0

for i in range(n): #区間の数
  length_sum += int( a[i] )

for i in range(n-1): #切れ目の数
  length_sum_start_left += int( a[i] )
  if( abs( length_sum - length_sum_start_left - length_sum_start_left ) < cnt ):
    cnt = abs( length_sum - length_sum_start_left - length_sum_start_left )
  if( cnt == 0 ):
    break

print(cnt)

Submission status: Accepted

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

sum_a=sum(a)
left_a=0
right_a=sum_a
ans=200002*2020202020

for i in range(n):
    left_a+=a[i]
    right_a-=a[i]
    if(ans>abs(left_a-right_a)):
        ans=abs(left_a-right_a)
print(ans)

Submission status: Accepted

Solution code:
import bisect


def CumulativeSum(l):
    a = [0]
    for i in l:
        a.append(a[-1] + i)
    return a[1:]


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = CumulativeSum(a)
    s = b[-1]
    h = s // 2
    x = bisect.bisect_left(b, h)
    try:
        m = (b[x], b[x + 1], b[x - 1])
    except:
        m = (b[x], b[x - 1])
    if s % 2 == 0 and h in m:
        print(0)
        return

    print(min([abs(x * 2 - s) for x in m]))


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

s = sum(li)
size = s / 2
plus = 0

for i in li:
    plus += i
    if plus >= size:
        mid = i
        break

ans_x = plus * 2 - s
ans_y = s - (plus - mid) * 2

if size == plus:
    ans_x = 0 

print(min(ans_x,ans_y))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sm = sum(a)
l = 0
r = 0
ans = 2020202020 * 200000
for x in a:
    l += x
    r = sm - l
    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)
half = total / 2
current = 0


for i in range(n):
    current += a[i]
    if half < current:
        half_index = i
        break

pos_r = current - half
pos_l = a[half_index] - pos_r

print(int(min(pos_l, pos_r) / 0.5))

Submission status: Accepted

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

A_sum = sum(A)
a_half = 0
for ai in A:
    a_half_next = a_half + ai
    if a_half_next >= A_sum / 2:
        break
    a_half = a_half_next
    
a_half_dif =  abs((A_sum - a_half) - a_half)
a_half_next_dif = abs((A_sum - a_half_next) - a_half_next)
if a_half_next_dif <= a_half_dif:
    a_half_dif = a_half_next_dif

    
print(a_half_dif)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
anslist = []
lefts = 0
rights = sum(A)
for i in range(N):
    lefts += A[i]
    rights -= A[i]
    anslist.append(abs(lefts - rights))
print(min(anslist))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sum = 0
c_1 = 0
c_2 = 0
for i in range(n):
    sum += a[i]

for i in range(n):
    c_1 += a[i]
    if c_1 * 2 >= sum:
        break
for i in range(n):
    c_2 += a[n - i - 1]
    if c_2 * 2 >= sum:
        break
    
if c_1 * 2 - sum >= c_2 * 2 - sum:
    print(c_2 * 2 - sum)
else:
    print(c_1 * 2 - sum)

Submission status: Accepted

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

# bar length
BarLen = sum(A)
BarHalf = BarLen / 2
# print(BarLen, BarHalf)

# culc nearest point from half
c_sum = 0
for a in A:
    if c_sum + a == BarHalf:
        n_point = 0
        break
    elif c_sum + a > BarHalf:
        d_minus = BarHalf - c_sum
        d_plus = c_sum + a - BarHalf
        n_point = min(d_minus, d_plus)
        break
    else:
        c_sum += a

print(int(n_point/0.5))

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
a  = [int(n) for n in input().split()]
y = 0
summ = sum(a)
c = int(len(a) / 2)
t = False
while 1:
    lsum = sum(a[:c])
    rsum = sum(a[c:])
    if not t:
        if lsum > rsum:
            t = True
            c -= 1
        else:
            c += 1
    else:
        if lsum > rsum:
            c -= 1
        else:            
            h = sum(a[c:]) - sum(a[:c]) 
            hh = sum(a[:c + 1]) - sum(a[c + 1:])
            if h < hh:
                y += h
            else:
                y += hh
            break

print(y)

Submission status: Accepted

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

Submission status: Accepted

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

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

L = sum(A)

ac = 0
for i in range(N):
  ac += A[i]
  if(ac >= L/2):
    key = i
    break

cost = 0
if(L%2 == 1):
  A[key] -= 1
  L -= 1
  cost = 1

ac = 0
for i in range(N):
  ac += A[i]
  if(ac >= int(L/2)):
    key = i
    break

if(ac == int(L/2)):
  print(cost)
else:
  right = ac - int(L/2)
  left = int(L/2) - (ac-A[key])
  cost += min(right, left)*2
  print(cost)

Submission status: Accepted

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

mx = sum(A)

count_a = 0
count_b = 0
a_Flag = False
b_Flag = False
for i in range(len(A)):
    if not a_Flag:
        count_a += A[i]
    if not b_Flag:
        count_b += A[N - i - 1]
    if mx / 2 <= count_a:
        a_Flag = True
    if mx / 2 <= count_b:
        b_Flag = True
    if a_Flag and b_Flag:
        break

other_a = mx - count_a
other_b = mx - count_b
if count_a - other_a < count_b - other_b:
    print(abs(count_a - other_a))
else:
    print(abs(count_b - other_b))

Submission status: Accepted

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


n=int(input())
l=list(map(int,input().split()))
m=[l[0]]
for i in range(1,n):
    m.append(m[-1]+l[i])
check=0
if m[-1]%2==1:
    check=1
if m[-1]%2==0:
    sa=m[n-1]
    base=m[-1]/2
    for k in range(n):
        if abs(base-m[k])<sa:
            sa=abs(base-m[k])
    print(int(sa*2+check))
else:
    m1=m
    for _ in range(n):
        m1[_]+=1
    sa=m1[n-1]
    base=m1[-1]/2
    for k in range(n):
        if abs(base-m1[k])<sa:
            sa=abs(base-m1[k])
    ans1=sa*2+1
    for v in range(n-1):
        m[v]-=1
    m[-1]+=1
    sa=m[n-1]
    base=(m[n-1]/2)//1
    for k in range(n):
        if abs(base-m[k])<sa:
            sa=abs(base-m[k])
    ans2=sa*2+1
    print(int(min(ans1,ans2)))

Submission status: Accepted

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

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

h = sum(a) / 2
ans = 0
for i in range(n):
    if t[i] >= h and t[i] - h >= h - t[i-1]:
        print()
        ans = sum(a) - t[i-1] * 2
        break
    elif t[i] >= h and t[i] - h < h - t[i-1]:
        ans = t[i] * 2 - sum(a)
        break
    

print(ans)

Submission status: Accepted

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

cost = 0
length = 0
for a in listA:
    length += a

padding = 0
if (length % 2 != 0):
    cost += 1
    length += 1
#    padding = 1

half = int(length / 2)

i = 0
sum = padding
while sum < half:
    sum += listA[i]
    i += 1

if (sum != half):
    rightSum = 0
    for j in range(i - 1, N):
        rightSum += listA[j]

    if (sum > rightSum):
        sum -= listA[i - 1]
        print(int(rightSum - sum))
    else:
        rightSum -= listA[i - 1]
        print(int(sum - rightSum))
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
mlmis = lambda: [-int(x) for x in input().split()]
INF = float('inf')
#

def main():
    N = ii()
    A = lmis()
    S = sum(A)
    ans = INF
    s = 0
    for a in A:
        s += a
        ans = min(ans, abs((S-s)-s))
    print(ans)


main()

Submission status: Accepted

Solution code:
import numpy as np
N = list(map(int, input().split()))[0]
A = np.array(list(map(int, input().split())))
def get_result(ary):
    cumsum_left = np.cumsum(ary)
    cumsum_right = np.cumsum(ary[::-1])
    d = np.abs(cumsum_left[:-1] - cumsum_right[::-1][1:])
    return np.min(d)
print(get_result(A))

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
A = [int(a) for a in A]
sum_ = sum(A)
sum_2 = sum_/2

X = 0
s = 0
for i in range(N):
    s += A[i]
    X += A[i]
    if(s>=sum_2):
        break

l1 = X-A[i]
l2 = sum_-X

data = [abs(l1+A[i]-l2),abs(l2+A[i]-l1)]
print(min(data))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    accum = [0] * (N-1)
    accum[0] = A[0]
    for i in range(1, N-1):
        accum[i] = A[i] + accum[i-1]
    mini = -1
    s = sum(A)
    for i in range(N-1):
        if mini == -1:
            mini = abs(s - 2 * accum[i])
        else:
            mini = min(mini, abs(s - 2 * accum[i]))
    print(mini)
main()

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
A=list(map(int,input().split()))
A=deque(A)
l,r=0,0
for i in range(n-1):
    num_o,num_t=A[0],A[-1]
    if l+num_o>=r+num_t:
        r +=num_t
        A.pop()
    else:
        l +=num_o
        A.popleft()
print(min(r+A[0]-l,l+A[0]-r))

Submission status: Accepted

Solution code:
N = int(input())
P = [int(_) for _ in input().split()]

d = sum(P)
L = sum(P)
S_left = 0
S_right= 0

for i in range(N):
    S_left += P[i]
    S_right = L - S_left
    d_temp = S_left - S_right
    
    if d >= abs(d_temp):
        d = abs(d_temp)
    
    else:
        break

    if d == 0:
        break

print(abs(d))

Submission status: Accepted

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

S = sum(A)
i = 0
S1 = 0
while(S1 < S/2):
  S1 += A[i]
  i += 1
if i == N:
  ans = abs(sum(A[:i-1])-sum(A[i-1:]))
else:
  ans = min(abs(sum(A[:i-1])-sum(A[i-1:])),abs(sum(A[:i])-sum(A[i:])))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
num_list = list(map(int,input().split()))
min_diff = 10 ** 18
sum_list = sum(num_list)
count = 0
for i in range(n):
    count += num_list[i]
    diffs = abs(count*2 - sum_list)
    if min_diff < diffs:
        print(min_diff)
        exit()
    else:
        min_diff = diffs

Submission status: Accepted

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

_n = int(input())
_alist = list(map(int, input().split()))


def chker(n, alist):
    length = sum(alist)
    ls, rs = 0, length

    cost = length
    for index, ai in enumerate(alist):
        ls += ai
        rs -= ai
        precost = cost
        cost = min(abs(rs - ls), cost)

        if cost >= precost:
            print(precost)
            return


chker(_n, _alist)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
all_a = sum(a)
half_a = all_a / 2
a_sum = 0
for j,i in enumerate(a):
    if a_sum + i < half_a:
        a_sum += i
    else:
        if abs(a_sum -  half_a) >= abs((a_sum + i) -  half_a):
            print(abs((all_a - 2*(a_sum + i)) ))
            break
        else:
            print(abs(all_a - 2*(a_sum)))
            break

Submission status: Accepted

Solution code:
import numpy as np

n=int(input())
a=list(int(x) for x in input().split())
bou_length=0
l=[]
for i in range(len(a)):
  bou_length += a[i]
  l.append(bou_length)
#print(bou_length)
#print(l)

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

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

diff=getNearestValue(l, (bou_length/2))
#print(diff)
print(abs(l[-1]-2*diff))

Submission status: Accepted

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

if N == 2:
  print(abs(a[0]-a[1]))
else:
  s = sum(a)
  i = 0
  b = 0
  for i in range(N):
    if b<s/2:
      b += a[i]
    else:
      b = b-a[i-1]
      c = a[i-1]
      break
  h = s-b-c
  if h>b:
    print(b+c-h)
  elif b>h:
    print(h+c-b)
  else:
    if N%2==0:
      print(0)
    else:
      print(c)

Submission status: Accepted

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

total = sum(L)
mid = total / 2
accum = 0
for i, elm in enumerate(L):
    accum += elm
    if accum == mid:
        print(0)
        break
    elif accum > mid:
        right = accum - mid
        left = elm - right
        print(int(min(left, right) / 0.5))
        break

Submission status: Accepted

Solution code:
N = int(input())

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

Asum = 0
A_sum = []

for i in range(N):
    Asum += A[i]
    A_sum.append(Asum)

for i in range(N):
    A_sum[i] = 2*A_sum[i] - Asum
    if A_sum[i] < 0:
        A_sum[i] = -1 * A_sum[i]

print(min(A_sum))

Submission status: Accepted

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

half_len = sum(A) / 2.0
now_len = 0
most_near_len = sum(A)+1
for i in A:
    now_len += i
    if abs(half_len - now_len) <= abs(half_len - most_near_len):
        most_near_len = now_len

cnt = abs(most_near_len - half_len) / 0.5
print(int(cnt))

Submission status: Accepted

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

Asum = sum(A)
sumdiflist = []

temp = 0
for i in range(N-1):
  temp += A[i]
  sumdif = 2*temp - Asum
  if(sumdif<0):
    sumdif = -sumdif
  sumdiflist.append(sumdif)

print(min(sumdiflist))

Submission status: Accepted

Solution code:
n= int(input())
a = list(int(i) for i in input().split())
import math
import sys

sta=sum(a)/2
sum=0
ans=0
for i in range(0,n-1):
    if sum==sta:
        print(0)
        sys.exit()
    elif sum<sta and sum+a[i]>sta:
        ans=min(sta-sum,sum+a[i]-sta)/0.5
        print(int(ans))
        sys.exit()
    else:
        sum+=a[i]

ans=min(sta-sum,sum+a[n-1]-sta)/0.5
print(abs(int(ans)))

Submission status: Accepted

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

sl = [0] * (N+1)
for i in range(N):
    sl[i+1] = a[i] + sl[i]

half = sl[-1]/2
diff = []
for i in range(N):
    diff.append(abs(half - sl[i]))
l = sl[diff.index(min(diff))]
r = sl[-1] - l
print(abs(l-r))

Submission status: Accepted

Solution code:
n=int(input())
a=[0]+list(map(int,input().split()))
ave=sum(a)/2
num=0
for i in a:
    num+=i
    if num>=ave:
        xx=min(abs(ave-num),abs(ave-(num-i)))
        print(int(xx/0.5))
        exit()

Submission status: Accepted

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

cost = 0
right = 0
old_right = 0
length = 0
for i in range(N):
    length += A_list[i]
pivot = length/2
for i in range(N):
    right += A_list[i]
    if pivot == right:
        break
    if pivot < right:
        if pivot - old_right <= right - pivot:
            cost = pivot - old_right
            break
        else:
            cost = right - pivot
            break
    old_right = right

print(int(2*cost))

Submission status: Accepted

Solution code:
import itertools
import numpy as np

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

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

sum = ac[-1]
half = sum/2
near = getNearestValue(ac,half)
dif = abs(ac[near]-half)
print(int(dif * 2))

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
totl=0
idx=0
dif1=0
for i in range(n):
  totl+=a[i]
  idx=i
  if (s/2)==totl:
    print(0)
    sys.exit()
  elif (s/2)<totl:
    dif1=totl-(s/2)
    break
dif2=a[idx]-dif1
if dif1-(dif1//1)==0:
  print(int(2*min(dif1,dif2)))
else:
  print(int(2*min(dif1,dif2)))

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
inputs = sys.stdin.readlines()
n = int(inputs[0])
A = list(map(int, inputs[1].split()))

total = sum(A)
B = list(accumulate(A))
C = [total - 2*b for b in B]
d = min((abs(c) for c in C))
print(d)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
l = A.pop(0)
idxl = 0
r = A.pop(-1)
idxr = -1

for i in range(n-2):
  if r<l:
    r+=A[idxr]
    idxr -= 1
  else:
    l+=A[idxl]
    idxl += 1
  
print(abs(r-l))