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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 23 20:43:13 2019
DISCO2020preA
@author: maezawa
"""

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

l = sum(a)
half = l // 2
odd = True
if l % 2 == 0:
    odd = False

cnt = 0
for i in range(n):
    cnt += a[i]
    if cnt > half and i == 0:
        ans = (half - (cnt-a[i]))*2
        break
    if cnt > half and i != n-1:
        if cnt - half > half - (cnt-a[i]):
            #print(1, half - (cnt-a[i]))
            ans = (half - (cnt-a[i]))*2
            break
        else:
            #print(2, (cnt - half))
            ans = (cnt - half)*2
            break
    elif cnt > half and i == n-1:
        if odd:
            ans = (cnt - half-1)*2
        else:
            ans = (cnt - half)*2
        break
            
if odd:
    ans -= 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
cost_1 = 0
cost_2 = 0
cost_3 = 0
cost_4 = 0
S = sum(A)
S_1 = sum(A)
S_2 = sum(A)
S_3 = sum(A)
S_4 = sum(A)
half = S / 2
l = 0
i = 0
while l < half:
    l += A[i]
    i += 1
l_big = l
l_small = l - A[i-1]
if l_big == S - l:
    print(0)
if l_big - half < half - l_small:
    l_1 = l_big
    l_2 = l_big
    while l_1 > S_1 - l:
        l_1 -= 1
        cost_1 += 1
    while l > S_2 - l_2:
        l_2 -= 1
        cost_2 += 1
    print(min(cost_1, cost_2))
else:
    l_3 = l_small
    l_4 = l_small
    while l_3 < S_3 - l_3:
        l_3 += 1
        cost_3 += 1
    while l_4 < S_4 - l_4:
        l_4 += 1
        cost_4 += 1
    print(min(cost_3, cost_4))

Submission status: Wrong Answer

Solution code:

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

left = A[0]
left_count = 0
right = A[N - 1]
right_count = 0
for i in range(N - 2):
    if left <= right:
        left_count += 1
        left += A[left_count]
    else:
        right_count += 1
        right += A[N - right_count]

print(abs(left- right))

Submission status: Wrong Answer

Solution code:
# Returns the best obtainable price for a rod of length n and
# price[] as prices of different pieces
def cutRod(price, n):
    val = [0 for x in range(n + 1)]
    val[0] = 0

    # Build the table val[] in bottom up manner and return
    # the last entry from the table
    for i in range(1, n + 1):
        max_val = 200000
        for j in range(i):
            max_val = max(max_val, price[j] + val[i - j - 1])
        val[i] = max_val
        return val[n]


# Driver program to test above functions
arr = [1, 5, 8, 9, 10, 17, 17, 20]
size = len(arr)
print("Minimum Money: " + str(cutRod(arr, size)))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=input().split()
a=[int(i) for i in a]
l=0
r=0
s=sum(a)/2
for i in range(n):
	l=a[i]+l
	#r=a[n-i-1]+r
	if l>=s and abs(s-l)>=abs(s-l-a[i]):
		print(int(abs(s-l-a[i])*2))
		exit()
	elif l>=s and abs(s-l)<=abs(s-l-a[i]):
		print(int(abs(s-l)*2))
		exit()

Submission status: Wrong Answer

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

#print(N)
#print(A)

#合計を求める
sum = 0
for i in range(N):
  sum += A[i]

#半分を超える要素pを求める
half1st = 0
half1st2 = 0
half2nd = 0
i = 0
temp = 0
flag = 0
for i in range(N):
  if( half1st <= (int)(sum/2) ):
    half1st += A[i]
    temp += 1
  else:
    half2nd += A[i]
thre = temp-1
half1st2 = half1st - A[thre]
half2nd1 = half2nd + A[thre]
#その要素pを減らす/増やす，あるいは要素p-1を減らす/増やす，どちらが早いか判定
P = abs(half1st2 - half1st)
Q = abs(half1st - half2nd)

#ちょうど半分
if (half1st2 == half2nd1):
  print(0)
#P > Q
elif ( P >= Q ):
  #print('ans = '+str(Q))
  print(Q)
else:
  #print('ans = '+str(P))
  print(P)


#A[i-1] が要素p 
#要素のインクリ数，でクリ数を計算

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
sizesRaw = raw_input().split(" ")
total_length = 0
sizes = []
for i in sizesRaw:
  total_length += int(i)
  sizes.append(int(i))
mid_point = total_length/2


distance_closest = mid_point
c = 0
for i in sizes:
  c +=i
  di = abs(mid_point - c)
  if di < distance_closest:
    distance_closest = di
  
print(int(distance_closest * 2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy
from bisect import bisect_left

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

res = numpy.cumsum(A)
m = res[N-1]
ans = 0

if m % 2 == 0:
    l = m / 2
    b = bisect_left(res, l)
    if b == l:
        print(ans)
    else:
        x = l - res[b]
        y = res[b+1] - l
        if x <= y:
            ans = x
            print(ans)
        else:
            ans = y
            print(ans)

else:
    l1 = (m-1) / 2
    l2 = (m+1) / 2
    b1 = bisect_left(res, l1) - 1
    c1 = b1 + 1
    b2 = bisect_left(res, l2) - 1
    c2 = b2 + 1
    B1 = l1 - res[b1]
    C1 = res[c1] - l1
    B2 = l2 - res[b2]
    C2 = res[c2] - l2
    d = [B1, C1, B2, C2]
    d.sort()
    ans = d[0]*2 + 1
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
r = 0
n = N//2
s = sum(A)

a = s/2
t = a
cnt = 0

for i in range(len(A)):
    if t >= A[i]:
        t = t - A[i]
        cnt = cnt + 1
    else:
        break

if N%2 == 1:
    if (s - sum(A[0:cnt+1])) < (s - sum(A[cnt:N])):
        r = (sum(A[cnt:N])) - (sum(A[0:cnt]))
 
    elif (s - sum(A[0:cnt+1])) > (s - sum(A[cnt:N])):
        r = (sum(A[0:cnt+1])) - (sum(A[cnt + 1:N]))
    else:
        pass
 
else:
    if (s - sum(A[0:cnt])) < (s - sum(A[cnt:N])):
        r = (s - sum(A[cnt:N])) - (s - sum(A[0:cnt]))
 
    elif (s - sum(A[0:cnt])) > (s - sum(A[cnt:N])):
        r = (s - sum(A[0:cnt])) - (s - sum(A[cnt:N]))
 
    else:
        pass

print(r)

Submission status: Wrong Answer

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

base_1=0
base_2=0
count=0

for i in range(N):
    base_1+=A[i]
for j in range(N):
    if base_2<=base_1//2:
        base_2+=A.pop(0)
    else:
        break
            
if base_1==base_2:
         print(count)
else:
    count+=base_2-(base_1-base_2)
    print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
li = list(map(int,input().split()))
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
for i in range(N):
    sum1 += li[i]
    sum4 += li[i]
sum1 //= 2
for i in range(N):
    sum2 += li[i]
    if sum2 >= sum1:
        ans = sum2-sum1 + abs(sum4-sum2-sum1)
        break
    sum3 += li[i]
print(ans)

Submission status: Wrong Answer

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

c= 0
s=sum(a)

for i in range(0,N):
  if c<s/2:
    c+=a[i]
  else:
    break

if  ((c-a[i-1])*2-s)>(c*2-s):
  print(c*2-s)
else:
  print(abs((c-a[i-1])*2-s))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
tmp = 0
std = sum(A)/2
for i in range(N):
    tmp += A[i]
    if tmp >= std:
        index = i
        break
a = sum(A[:index+1])
b = sum(A[index+1:])
if b == 0:
    a = sum(A[:N-1])
    b = A[N-1]
    print(b-a)
else:
    print(a-b)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N=int(input())
A=list(map(int, input().split()))
A=np.array(A)
left=0 
right=N-1
while left<right:
  mid=(left+right)//2
  if A[:mid+1].sum()<A[mid+1:].sum():
    left=mid+1
  else:
    right=mid
if A[:left+1].sum()-A[left+1:].sum()>A[:left].sum()-A[left:].sum():
  print((abs(A[:left].sum()-A[left:].sum())))
else:
  print((abs(A[:left+1].sum()-A[left+1:].sum())))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=input().split()
a=[int(i) for i in a]
l=0
r=0
s=sum(a)/2
for i in range(n):
	l=a[i]+l
	#r=a[n-i-1]+r
	if l>=s and abs(s-l)>abs(s-l-a[i]):
		print(int(abs(s-l-a[i])*2))
		exit()
	elif l>=s and abs(s-l)<=abs(s-l-a[i]):
		print(int(abs(s-l)*2))
		exit()

Submission status: Wrong Answer

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

    sm = sum(A)
    agg = 0
    idx = 0
    mid = sm / 2
    shrink_cnt = sm - N
    while agg < mid and idx < N-1:
        
        agg += A[idx]
        idx += 1
    
    oneleft = agg
    oneright = sm - oneleft 
    twoleft = agg-A[idx]
    tworight = sm - twoleft
    ans = min(abs(oneleft-oneright), abs(twoleft-tworight))

    print(int(ans))

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

diff = []
sam = 0
for i in range(N):
    sam += A[i]
    diff.append(sum(A) - sam)
print(min(diff))

Submission status: Wrong Answer

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

l = sum(a)
half = l // 2
odd = True
if l % 2 == 0:
    odd = False

cnt = 0
for i in range(n):
    cnt += a[i]
    if cnt > half and i == 0:
        ans = (half - (cnt-a[i]))*2
        break
    if cnt > half and i != n-1:
        if cnt - half > half - (cnt-a[i]):
            #print(1, half - (cnt-a[i]))
            ans = (half - (cnt-a[i]))*2
            break
        else:
            #print(2, (cnt - half))
            ans = (cnt - half)*2
            break
    elif cnt > half and i == n-1:
        ans = (cnt - half)*2
        break
            
if odd:
    ans -= 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
for i in range(a):
        if sum(b[0:i-1]) <= sum(b)/2:
            print(0)
            break
        if sum(b[0:i-1]) < sum(b)/2 < sum(b[0:i]):
                v = sum(b[0:i])- (sum(b)-sum(b[0:i]))
                u = (sum(b)-sum(b[0:i - 1]))-sum(b[0:i -1])
                print(min(v,u))

Submission status: Wrong Answer

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

sep = N//2
for i in range(N):
  if sum(A[:sep]) >= sum(A[sep:]):
    sep -= N//((i+2)**2)
  else:
    sep += N//((i+2)**2)
  if N//((i+2)**2) == 1:
    break
#print(sep)


a = abs(sum(A[:sep])-sum(A[sep:]))
b = abs(sum(A[:sep+1])-sum(A[sep+1:]))
c = abs(sum(A[:sep-1])-sum(A[sep-1:]))
d = abs(sum(A[:1])-sum(A[1:]))
e = abs(sum(A[:-1])-sum(A[-1:]))
print(min(a,b,c,d,e))

Submission status: Wrong Answer

Solution code:
n =int(input())
K = [int(e) for e in input().split()]
summation =sum(K)
a = 0
b = 0
for i in range(n):
    if a >= summation/2:
        b += K[i]
    else:
        a += K[i]
x=abs(a-b)
print(x)

Submission status: Wrong Answer

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

A_sum = sum(A)
A_sum_hulf = A_sum / 2

A_sum_rui = 0

for i in range(N):
    A_sum_rui += A[i]
    if A_sum_rui > A_sum / 2:
        if A_sum_rui - (A_sum / 2) < (A_sum / 2) -(A_sum_rui - A[i]):
            print(sum(A) - A_sum_rui)
        else:
            print(sum(A) - 2 * (A_sum_rui - A[i]))
        break

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
b = 0
a = 0
if N == 2:
    print(abs(A[0]-A[1]))
else:
    for i in range(len(A)):
        if S/2-0.5 > a:
            a += A[i]
        else:
            b += a + A[i]
            break
        if i == len(A)-1:
            b = a
            a = a-A[i]
        else:
            None
    print(min(abs(S - a*2), abs(S - b*2)))

Submission status: Wrong Answer

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

l = sum(a)
half = l // 2
odd = True
if l % 2 == 0:
    odd = False

cnt = 0
for i in range(n):
    cnt += a[i]
    if cnt > half and i == 0:
        ans = (cnt - half)*2
        break
    if cnt > half and i != n-1:
        if cnt - half > half - (cnt-a[i]):
            #print(1, half - (cnt-a[i]))
            ans = (half - (cnt-a[i]))*2
            break
        else:
            #print(2, (cnt - half))
            ans = (cnt - half)*2
            break
    elif cnt > half and i == n-1:
        ans = (half - (cnt-a[i]))*2
        break
            
if odd:
    ans -= 1
    
print(ans)

Submission status: Wrong Answer

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

def h(n,A):
    res = float('inf')
    f = []
    b = []
    t = 0
    for i in range(len(A)):
        t+=A[i]
        f.append(t)
    t = 0
    for i in range(len(A)-1,-1,-1):
        t += A[i]
        b.append(t)
    for i in range(len(A)-1):
        temp1 = f[i]
        temp2 = b[i+1]
        res = min(res,abs(temp1-temp2))
        
    #print(f)
    #print(b)
    print(res)
    
h(n,A)

Submission status: Wrong Answer

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

sep = N//2
for i in range(N):
  if sum(A[:sep]) >= sum(A[sep:]):
    sep -= N//((i+2)**2)
  else:
    sep += N//((i+2)**2)
  if N//((i+2)**2) == 1:
    break
#print(sep)


a = abs(sum(A[:sep])-sum(A[sep:]))
b = abs(sum(A[:sep+1])-sum(A[sep+1:]))
#c = abs(sum(A[:sep-1])-sum(A[sep-1:]))
print(min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
#print(n, l)
p = sum(l)
i = 0
while sum(l[:i]) < p / 2:
    i += 1
if abs(sum(l[:i]) - p / 2) < abs(sum(l[:i - 1]) - p / 2):
    q = sum(l[:i])
else:
    q = sum(l[:i - 1])

c = 0
for ii in range(100000):
    if q / p > 1 / 2:
        q -= 1
        p -= 1
        c += 1
    elif q / p < 1 / 2:
        q += 1
        p += 1
        c += 1
    else:
        break
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
from bisect import bisect_left

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

count = 0
s = a.sum()
cs = np.cumsum(a)

# print(a, s, cs)

median = bisect_left(cs, s // 2)

# print(median, cs[median])

while cs[median] * 2 != s:
    s = a.sum()
    cs = np.cumsum(a)
    median = bisect_left(cs, s // 2)
    if a[median] != 1:
        a[median] = a[median] - 1
    else:
        a[median] = a[median] + 1
    count += 1

print(count if count == 0 else count - 1)

Submission status: Wrong Answer

Solution code:
N=int(input())
len=list(map(int,input().split()))
t=N//2
m=0
n=0
ans=0
s=0
g=0
for i in range(t):
  m=m+len[i]
  n=n+len[N-1-i]
s=min(m,n)
g=max(m,n)
if(N%2==0):
  ans=abs(m-n)
else:
  s=s+len[N//2]
  ans=abs(s-g)
print(ans)

Submission status: Wrong Answer

Solution code:
n =int(input())
K = [int(e) for e in input().split()]
summation =sum(K)
a = 0
b = 0
for i in range(n):
    if a > summation/2:
        b += K[i]
    else:
        a += K[i]
x=abs(a-b)
print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
length = 0
for i in A:
    if length + i < sum(A) // 2:
        length += i
    elif length + i == sum(A) // 2:
        print(0)
        exit()
    else:
        print(min([sum(A) // 2 - (length - i), length + i - sum(A) // 2]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
#print(n, l)
p = sum(l)
i = 0
while sum(l[:i]) < p / 2:
    i += 1
if abs(sum(l[:i]) - p / 2) < abs(sum(l[:i - 1]) - p / 2):
    q = sum(l[:i])
else:
    q = sum(l[:i - 1])

c = 0
for ii in range(10000000):
    if q / p > 1 / 2:
        q -= 1
        p -= 1
        c += 1
    elif q / p < 1 / 2:
        q += 1
        p += 1
        c += 1
    else:
        break
print(c)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import math

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

#zen =list_aaa[0:math.ceil(N/2)]
#kou =list_aaa[math.ceil(N/2):]
#
#print(abs(sum(zen)-sum(kou)))

min_aaa =10000000
cnt =0

sum_list_aaa =list(accumulate(list_aaa))
judge_aaa = math.ceil(sum_list_aaa[-1]/2)


for h in range(min_aaa):
    for i in range(N):
        min_aaa =min(min_aaa,abs(judge_aaa-sum_list_aaa[i]))
    if min_aaa ==0:
        print(cnt)
        exit()
    else:
        cnt +=1
        list_aaa[-1] +=1
        sum_list_aaa =list(accumulate(list_aaa))
        if sum_list_aaa[-1]%2 ==1:
            judge_aaa = 100000
        else:
            judge_aaa = math.ceil(sum_list_aaa[-1]/2)

Submission status: Wrong Answer

Solution code:
N=int(input())  #数値入力 「N」だけの入力のとき
A=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

# from functools import lru_cache
# @lru_cache(maxsize=100000)

# 一度差を出してから，どんどんi-1, i+1の和で縮めていく．

def check_distance_2():
    # ret = []
    tmp_a = abs(sum(A[:1]) - sum(A[1:]))
    # print(tmp_a)
    for i in range(1, N-1):
        tmp_b = abs(tmp_a - (A[i]*2))
        
        if tmp_a > tmp_b:
            tmp_a = tmp_b
        else:
            return tmp_a
        # print(tmp_a)
    return tmp_a


print(check_distance_2())

Submission status: Wrong Answer

Solution code:
import numpy as np
#def gNV(list, num):
 #   idx = np.abs(np.asarray(list) - num).argmin()
 #   return list[idx]
def gNV(list, num):
    sortIdx = np.searchsorted(list, num, side='left')
    return list[sortIdx]

N=int(input())
A=[int(x) for x in input().split()]
Asum=[]
for i in range(N):
    Asum.append(sum(A[:i+1]))
Amax=Asum[-1]
Ahalf=Amax/2.0
if Ahalf.is_integer() and int(Ahalf) in Asum:
    ans=0
else:
    ANV=gNV(Asum, Ahalf)
    ans=abs(2*ANV-Amax)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
s = sum(a)
cnt = 0
res = INF
b = [0] * n
for i in range(n-1):
    a[i+1] = a[i+1] + a[i]

for i in range(n):
    res = min(res,s-a[i])
print(res)

Submission status: Wrong Answer

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

    sm = sum(A)
    agg = 0
    idx = 0
    mid = sm / 2
    shrink_cnt = sm - N
    while agg < mid and idx < N-1:
        
        agg += A[idx]
        idx += 1
    
    shrink = (agg-mid) / 0.5
    expand = (mid - (agg-A[idx]))/0.5

    if shrink <= shrink_cnt:
        ans = min(shrink, expand)
    else:
        ans = expand
    
    print(int(ans))

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = sum(a)
cnt = 0
ans = 0

for i in range(n):
    cnt += a[i]
    if cnt >= L / 2:
        break

if cnt == L / 2:
    ans = 0
else:
    ans = abs(L - cnt * 2)

print(ans)

Submission status: Wrong Answer

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

for a in A:
    sum += a
check = sum // 2
if sum % 2 == 0:
    for b in A:
        sum1 += b
        if check < sum1:
            sum2 = sum1 - b
            break
    if (sum1 - check) > (check - sum2):
        print(check - sum2)
    else:
        print(sum1 - check)
   
else:
    for b in A:
        sum1 += b
        if check < sum1:
            sum2 = sum1 - b
            break
    if (sum1 - check+1) > (check - sum2+1):
        d = check - sum2 + 1
    else:
        d = sum1 - check + 1
    print(d)

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = list(map(int, input().split()))
cnt = 0
total = 0

if sum(aa) % 2 == 1:
    for i in range(n):
        if aa[i] != 1:
            aa[i] -= 1
            cnt += 1
            break

half = sum(aa)//2

if sum(aa[:-1]) < aa[-1]:
    print(aa[-1]-sum(aa[:-1]))
    exit()

for i in range(n):
    total += aa[i]
    if total == half:
        print(0)
        exit()
    if total > half:
        cnt += min(abs(half-total), abs(half-total+aa[i+1]))*2
        break

print(cnt)

Submission status: Wrong Answer

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

left_length = sum(A[:int(N/2)])
right_length = sum(A[int(N/2):])
if left_length > right_length :
  price = left_length - right_length
  for i in range(int(N/2)-1,0,-1) :
    var = sum(A[i:]) - sum(A[:i])
    if price > var :
      price = var
    else :
      break
else :
  price = right_length - left_length
  for i in range(int(N/2)+1,N) :
    var = sum(A[:i]) - sum(A[i:])
    if price > var :
      price = var
    else :
      break

print(price)

Submission status: Wrong Answer

Solution code:
import numpy as np
from bisect import bisect_left

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

count = 0
s = a.sum()
cs = np.cumsum(a)

# print(a, s, cs)

median = bisect_left(cs, s // 2)

# print(median, cs[median])

while cs[median] * 2 != s:
    s = a.sum()
    cs = np.cumsum(a)
    median = bisect_left(cs, s // 2)
    a[median] = a[median] - 1
    count += 1

print(count if count == 0 else count - 1)

Submission status: Wrong Answer

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

sep = N//2
for i in range(N):
  if sum(A[:sep]) >= sum(A[sep:]):
    sep -= N//((i+2)**2)
  else:
    sep += N//((i+2)**2)
  if N//((i+2)**2) == 1:
    break
#print(sep)

a = abs(sum(A[:sep])-sum(A[sep:]))
b = abs(sum(A[:sep+1])-sum(A[sep+1:]))
c = abs(sum(A[:sep-1])-sum(A[sep-1:]))
print(min(a,b,c))

Submission status: Wrong Answer

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

dv, md = len(A) // 2, len(A) % 2
deltaA = 0

for h in range(dv):
    deltaA += A[h] - A[-(h+1)]

if md > 0:
    deltaA += A[dv]

print(deltaA)

Submission status: Wrong Answer

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

print(int(N/2))

left_length = sum(A[:int(N/2)])
right_length = sum(A[int(N/2):])
if left_length > right_length :
  price = left_length - right_length
  for i in range(int(N/2)-1,0,-1) :
    var = sum(A[i:]) - sum(A[:i])
    if price > var :
      price = var
    else :
      break
else :
  price = right_length - left_length
  for i in range(int(N/2)+1,N) :
    var = sum(A[:i]) - sum(A[i:])
    if price > var :
      price = var
    else :
      break

print(price)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
 
N=int(input())
A = np.array([int(x) for x in input().split()])
B = np.concatenate([[0],A.cumsum()])
H = np.searchsorted(B,sum(A)//2)
result=float('inf')
for i in range(min(5,len(A)//2)):
  result=min(result,abs(sum(A[:H+i])-sum(A[H+i:])),abs(sum(A[:H-i])-sum(A[H-i:])))
             
print(result)

Submission status: Wrong Answer

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

ABS = [100]*n
left = [100]*n
right = [100]*n
all_l = sum(a)

for i in range(n-1):
	left[i] = sum(a[:i+1])
	right[i] = all_l-left[i]
	ABS[i] = abs(int(left[i]) -int(right[i]))
abs_min = min(ABS)
print(abs_min)

Submission status: Wrong Answer

Solution code:
# B - Iron Bar Cutting
N = int(input())
A = list(map(int, input().split()))
A_1 = sum(A)//2
A_tmp = 0
for i in range(N):
    A_tmp += A[i]
    if A_tmp < A_1:
        pass
    else:
        if i == N-1:
            A_tmp = A_tmp-A[i]
        else:
            pass

        ans = abs(A_tmp - (sum(A) - A_tmp))
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
forward = [0]*N
forward[0] = A[0]
total = sum(A)
m = 10**9
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: Wrong Answer

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

def func(A):
    A_inf = 0
    A_sup = 0
    N_inf = 0
    N_sup = 0
    L = sum(A)

    for a in A:
        if A_inf + a <= L/2:
            A_inf += a
            A_sup += a
            N_inf += 1
            N_sup += 1

        if A_sup <= L/2 and A_sup + a > L/2:
            A_sup += a
            N_sup += 1

    if A_inf == A_sup:
        return 0

    A_mid = A_sup - A_inf
    A_left = L/2 - A_inf
    A_right = A_sup - L/2
    A_lrmin = min(A_left, A_right)
    if A_mid-1 >= 2*A_lrmin:
        return int(2*A_lrmin)
    else:
        return int(A_mid)

print(func(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
r = 0
n = N//2
s = sum(A)

a = s/2
t = a
cnt = 0

for i in range(len(A)):
    if t >= A[i]:
        t = t - A[i]
        cnt = cnt + 1
    else:
        break

if N%2 == 1:
    if (s - sum(A[0:cnt+1])) < (s - sum(A[cnt:N])):
        r = (s - sum(A[cnt:N]))
 
    elif (s - sum(A[0:cnt+1])) > (s - sum(A[cnt:N])):
        r = (s - sum(A[0:cnt+1]))
    else:
        pass
 
else:
    if (s - sum(A[0:cnt])) < (s - sum(A[cnt:N])):
        r = (s - sum(A[cnt:N])) - (s - sum(A[0:cnt]))
 
    elif (s - sum(A[0:cnt])) > (s - sum(A[cnt:N])):
        r = (s - sum(A[0:cnt])) - (s - sum(A[cnt:N]))
 
    else:
        pass

print(r)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
for i in range(a):
        if sum(b[0:i-1]) == sum(b)/2:
            print(0)        
        else:
            if sum(b[0:i-1]) < sum(b)/2 < sum(b[0:i]):
                v = sum(b[0:i])- (sum(b)-sum(b[0:i]))
                u = (sum(b)-sum(b[0:i - 1]))-sum(b[0:i -1])
                print(min(v,u))

Submission status: Wrong Answer

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

    sm = sum(A)
    agg = 0
    idx = 0
    mid = sm / 2
    shrink_cnt = 0
    while agg < mid and idx < N-1:
        shrink_cnt += A[idx] - 1
        
        agg += A[idx]
        idx += 1
    
    shrink = (agg-mid) / 0.5
    expand = (mid - (agg-A[idx]))/0.5

    if shrink <= shrink_cnt:
        ans = min(shrink, expand)
    else:
        ans = expand
    
    print(int(ans))

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
length = 0
for i in A:
    if length + i == sum(A) // 2:
        print(0)
        exit()
    elif length + i < sum(A) // 2:
        length += i
    elif length + i > sum(A) // 2:
        print(min([sum(A) // 2 - (length - i), length + i - sum(A) // 2]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
half = sum(A) // 2
def binary_search(list_, x):
    l = 0
    r = len(list_) - 1
    flag = False
    while r - l >= 1:
        i = (r + l) // 2
        if sum(list_[:i]) - sum(list_[i:]) == x:
            flag = True
            break
        elif sum(list_[:i]) - sum(list_[i:]) > x:
            r = i
        else:
            l = i+1
    # return flag
    return (r+l)//2, flag

idx, flag = binary_search(A, 0)

if flag:
    print(0)
else:
    print(abs(sum(A[idx:]) - sum(A[:idx])))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = 0
for i in a:s+=i
mid = s/2
sa = 1000000000
s = 0
for i in a:
    s+=i
    if(abs(mid-s) < sa):sa = abs(mid-s)
print(int(sa*2))

Submission status: Wrong Answer

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

c = a[0]
d = sum(a)/2

for i in range(1,n):
    c += a[i]
    if c == d:
        print(0)
        exit()
    elif c > d:
        print(2*min(d-c+a[i],c-d))
        exit()

Submission status: Wrong Answer

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 = 10**9
for i in range(n):
	minn = min(minn,abs(pre[-1]-2*pre[i]))
print(minn)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
b = 0
a = 0
if N == 2:
    print(abs(A[0]-A[1]))
else:
    for i in range(len(A)):
        if S/2-0.5 > a:
            a += A[i]
        else:
            b += a + A[i]
            break
    print(min(abs(S - a*2), abs(S - b*2)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = sum(a)
cnt = 0
ans = 0

for i in range(n):
    cnt += a[i]
    if cnt >= L // 2:
        break

if cnt == L // 2:
    ans = 0
else:
    ans = abs(L - cnt * 2)

print(ans)

Submission status: Wrong Answer

Solution code:

x=int(input())
y=[int(x) for x in input().split()]
a=0
for i in range(x):
  if a>sum(y):
    break
  else:
    a+=y[i]
    b=i
if abs(sum(y)/2-a)>abs(sum(y)/2-a-y[b]):
  a-=y[b]
print(sum(y)-a)

Submission status: Wrong Answer

Solution code:

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

def getTargetIndex():
    diffList = []
    for i in range(0,n-1):
        left = 0
        right = 0
        left = list[:i+1]
        right = list[i+1:]
        diffList.append(abs(sum(left)- sum(right)))
    print(diffList)
    return diffList.index(min(diffList)) + 1

div = getTargetIndex()
left = list[:div]
right = list[div:]
print(abs(sum(left) -sum(right)) )

Submission status: Wrong Answer

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

c = a[0]
d = sum(a)

for i in range(1,n):
    c += a[i]
    if c == d:
        print(0)
        exit()
    elif c > d:
        print(2*min(d-c+a[i],c-d))
        exit()