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:
N = int(input().strip())
array = list(map(int, input().strip().split(' ')))
s = sum(array)
half = s//2

count = 0
cc = 0
ans = 0
mm = True
for i in range(N):
    if count == half:
        ans = 0
        mm = False
        break
    elif count > half:
        break
    else:
        count = count + array[i]
        cc = cc + 1
if cc == N and mm:
    ans = array[N-1] - (count - array[N-1])
    mm = False

count2 = 0
array.reverse()
for i in range(N - cc):
    count2 = count2 + array[i]
if mm:
    ans = count - count2
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a_l = list(map(int,input().split()))


tmp = 0

if a_l[-1] > (sum(a_l))//2:
    print(a_l[-1]-sum(a_l[:n-1]))
    sys.exit(0)

for i in range(n-1):
    tmp += a_l[i]
    if tmp >= (sum(a_l))/2:
        print(tmp-sum(a_l[i+1:]))
        sys.exit(0)

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),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 math

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

left = 0
right = len(a)-1
sum_num = []
position_log = []

while(right - left != 0):
    position = math.ceil((right - left) / 2)
    
    if position in position_log:
        break

    left_sum = sum(a[0:position])
    right_sum = sum(a[position : len(a)])
    sum_num.append(abs(right_sum - left_sum))
    position_log.append(position)
    if left_sum == right_sum:
        break
    elif (left_sum <= right_sum):
        left -= math.ceil((right - left)/2)
    else:
        left += math.ceil((right - left)/2)

print(min(sum_num))

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  ((s-a[i-1])*2-s)>(c*2-s):
  print(c*2-s)
else:
  print((s-a[i-1])*2-s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
tmp = 0
for i in range(N):
    tmp += A[i]
    if tmp >= sum(A)/2:
        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().strip())
array = list(map(int, input().strip().split(' ')))
s = sum(array)
half = s//2

count = 0
cc = 0
ans = 0
mm = True
for i in range(N):
    if count == half:
        ans = 0
        mm = False
        break
    elif count > half:
        break
    else:
        count = count + array[i]
        cc = cc + 1
if cc == N:
    ans = array[N-1] - (count - array[N-1])
    mm = False

count2 = 0
array.reverse()
for i in range(N - cc):
    count2 = count2 + array[i]
if mm:
    ans = count - count2
print(ans)

Submission status: Wrong Answer

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

minidx=0
for i in range(1,N):
    dif = abs(sum(a[:i])-sum(a[i:]))
    if dif<mindif:
        mindif = dif
        minidx = i
    else:
        break
print(int(mindif))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(a) for a in input().split()]
ans=0
count=0
for i in range(1,N-1):
    x=A[0:i]
    y=A[i:N]
    diff=sum(y)-sum(x)
    if diff <= 0:
        break
    count+=1
diff1=sum(A[0:count+1])-sum(A[count+1:N])
diff2=sum(A[count:N])-sum(A[0:count])
ans=min(diff1,diff2)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

c = a[0]
d = sum(a)
if d % 2 == 0:
    d == d // 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()
else:
    d == d// 2
    for i in range(1,n):
        c += a[i]
        if c == d or c == d+1:
            print(0)
            exit()
        elif c > d:
            print(2*min(d-c+a[i],c-d-1) +1)
            exit()

Submission status: Wrong Answer

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

s = sum(A)
i = 0
half = s / 2
neibor = 0
for i in range(n):
	if neibor >= half:
		break
	else:
		neibor += A[i]

print(int(neibor-(s-neibor)))

Submission status: Wrong Answer

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

for i in range(n):
    if sum(a[:i])>sum(a[i:]):
        d1 = abs(sum(a[:i])-sum(a[i:]))
        d2 = abs(sum(a[:i-1])-sum(a[i-1:]))
        if d1 > d2:
            print(d2)
        elif d1 < d2:
            print(d1)
        break

Submission status: Wrong Answer

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

for a in A:
    A[A.index(a)] = int(a)
# print(sum(A))

left = 0
right = N - 1
tmp = 10**9
while True:
    # print(left)
    # print(right)
    mid = (left + right) // 2
    # print("mid: " + str(mid))
    lnum = A[:mid+1]
    rnum = A[mid+1:]
    # print(lnum)
    # print(rnum)
    new = abs(sum(lnum) - sum(rnum))
    if tmp == new:
        break
    if tmp > new:
        tmp = new
    if lnum > rnum:
        right = mid

    # print("tmp: "+str(tmp))
    # input()
    if left == right:
        break

print(tmp)



# left = A[0]
# right = A[-1]

# front = 1
# back = 1
# for _ in range(N-2):
#     if left > right:
#         right += A[-1 - back]
#         back += 1
#     else:
#         left += A[front]
#         front += 1

# print(abs(left-right))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))

calculator = 0
pluc_index = 0
minus_index = -1
for i in range(N):
    if calculator <= 0:
        calculator += A_list[pluc_index]
        pluc_index += 1
    else:
        calculator -= A_list[minus_index]
        minus_index -= 1
print(calculator)

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=min(abs(sum(A[:H+1])-sum(A[H+1:])),abs(sum(A[:H])-sum(A[H:])))
print(result)

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(2*A_lrmin)

print(func(A))

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
array = list(map(int, input().strip().split(' ')))
s = sum(array)
half = s//2

count = 0
cc = 0
ans = 0
mm = True
for i in range(N):
    if count == half:
        ans = 0
        mm = False
        break
    elif count > half:
        break
    else:
        count = count + array[i]
        cc = cc + 1
if cc == N:
    ans = count - array[N-1]
    mm = False
count2 = 0
array.reverse()
for i in range(N - cc):
    count2 = count2 + array[i]

if mm:
    ans = count - count2
print(ans)

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
for i in range(n-1):
	left[i] = sum(a[:i+1])
	right[i] = sum(a[i+1:])
	ABS[i] = abs(int(left[i]) -int(right[i]))
abs_min = min(ABS)
print(abs_min)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    s = sum(A)
    a = 0
    b = 0
    for i in range(len(A)):
        if a >= (s // 2) - 1 or i == N-1:
            b += A[i]
        else:
            a += A[i]

    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 < after_diff:
    print(before_diff - before_sum)
else:
    print(after_sum - after_diff)

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
for i in range(n-1):
	left[i] = sum(a[:i+1])
	right[i] = sum(a[i+1:])
	ABS[i] = abs(int(left[i]) -int(right[i]))
print(ABS)
print(left)
print(right)
abs_min = min(ABS)
print(abs_min)

Submission status: Wrong Answer

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

s = sum(A)

oe = s % 2
h = s // 2

p = 0
p0 = None
p1 = None
rest = None
for i in A:
    p += i
    if p == h:
        if oe == 0:
            print(0)
            exit()
        print(1)
        exit()
    if p > h:
        p1 = p
        rest = s - p1
        break
    p0 = p

if oe == 1:
    h += 1

p = p0 if abs(p0 - h) < abs(p1 - h) else p1

print(abs(p*2 - s))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
l = []
for i in range(N - 1):
    if sum(A[:i]) == sum(A[i:]):
        print(0)
        exit()
for j in range(N - 1):
    d = abs(sum(A[:j]) - sum(A[i:]))
    l.append(d)
print(min(l))

Submission status: Wrong Answer

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

harf = sum(A)//2

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:]))
print(min(a,b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

s = sum(A)
i = 0
half = s / 2
neibor = 0
for i in range(n):
	if neibor >= half:
		break
	else:
		neibor += A[i]

print(int(neibor - half))

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
print((A[:left+1].sum()-A[left+1:].sum()))

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
array = list(map(int, input().strip().split(' ')))
s = sum(array)
half = s//2

count = 0
cc = 0
ans = 0
mm = True
for i in range(N):
    if count == half:
        ans = 0
        mm = False
        break
    elif count > half:
        break
    else:
        count = count + array[i]
        cc = cc + 1
if cc == N:
    ans = count - array[N-1]
count2 = 0
array.reverse()
for i in range(N - cc):
    count2 = count2 + array[i]

if mm:
    ans = count - count2
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

sum_of_an = sum(An)

if sum_of_an % 2 == 0:
    half_of_an = sum_of_an / 2
    sum_left_len = 0
    for l in An:
        sum_left_len += l
        if sum_left_len == half_of_an:
            print(0)
            exit(0)
        elif sum_left_len > half_of_an:
            break

middle = sum_of_an / 2

sum_left_len = 0
diff = sum_of_an
min_diff = sum_of_an

last_left = -1
left_diff = 0

first_right = 0
right_diff = 0

for (i, l) in zip(range(0, N), An):
    sum_left_len += l
    if middle - sum_left_len >= 0:
        last_left = i
        left_diff = middle - sum_left_len
        continue

    if middle - sum_left_len < 0:
        first_right = i
        right_diff = middle - sum_left_len
        break

min_diff = min(left_diff, right_diff)

middle_index = -1
if min_diff == left_diff:
    middle_index = last_left
else:
    middle_index = first_right

if middle_index == N -1:
    middle_index -= 1

sum_middle = sum(An[:middle_index + 1])

print(abs(sum_middle - (sum_of_an - sum_middle)))

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


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

Submission status: Wrong Answer

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
'''
print("---")
print(i)
print(sum(data[:i+1]))
print(sum(data[i+1:]))
'''
diff = sum(data[:i+1]) - sum(data[i+1:])
diff = abs(diff)
print(int(math.ceil(diff/2)))

Submission status: Wrong Answer

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:
            print(2*tmp-total)
            return
    
if __name__=='__main__':
    main()

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 = (sum1-sum3) + abs((sum4-sum2) - sum1)
        break
    sum3 += li[i]
print(ans)

Submission status: Wrong Answer

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-1]) * 2)

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()))
l = []
for i in range(N - 1):
    if int(sum(A[:i])) == sum(A) // 2:
        print(0)
        exit()
for j in range(N - 1):
    d = abs(sum(A[:j]) - sum(A) // 2)
    l.append(d)
print(min(l))

Submission status: Wrong Answer

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
'''
print("---")
print(i)
print(sum(data[:i+1]))
print(sum(data[i+1:]))
'''
diff = sum(data[:i+1]) - sum(data[i+1:])
diff = abs(diff)
print(int(diff/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):
        tmp_b = abs(tmp_a - (A[i]*2))
        
        if tmp_a > tmp_b:
            tmp_a = tmp_b
        else:
            return tmp_a
    # return tmp_a


print(check_distance_2())

Submission status: Wrong Answer

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

if n % 2 == 0:
    for i in range(n//2):
        ans += a[i] - a[n - i - 1]
    print(ans)
else:
    for i in range(n // 2):
        ans += a[i] - a[n - i - 1]
    print(abs(abs(ans) - a[n//2]))

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a_l = list(map(int,input().split()))


tmp = 0
for i in range(n-1):
    tmp += a_l[i]
    if tmp == (sum(a_l))/2:
        print(0)
        sys.exit(0)
    elif tmp > (sum(a_l))/2:
        print(tmp-sum(a_l[i+1:]))
        sys.exit(0)
print(a_l[-1]-tmp)

Submission status: Wrong Answer

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):
    print(left)
    left += A[i]
    ans = min(ans, abs(sum_A -  left - left))
print(ans)

Submission status: Wrong Answer

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

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

count = 0
cost = 0
temp = 0
temp2 = 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 = count - i
    break

a.reverse()
count = 0
#print(half)
for j in (a):
  count += j
  if(count > half):
    temp2 = count
    #print(temp,temp2,nokori)
    if(temp > temp2):
      print(temp2 - nokori)
    else:
      print(temp - nokori - cost)
    break

Submission status: Wrong Answer

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
    ans=min(ans,abs(mae-ato))
print(ans)

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(2*A_lrmin+1)

print(func(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
l = []
for i in range(N - 1):
    if sum(A[:i]) == sum(A) // 2:
        print(0)
        exit()
for j in range(N - 1):
    d = abs(sum(A[:j]) - sum(A) // 2)
    l.append(d)
print(min(l))

Submission status: Wrong Answer

Solution code:
n=int(input())
alist=list(map(int,input().split()))
blist=[alist[0]]
for i in range(1,n):
	blist.append(blist[-1]+alist[i]) #累積和

hanbun=blist[-1]/2
shirushi=0
l=0
u=n-1
while u>l+1:
	aida=(l+u)//2
	if blist[aida]==hanbun:
		index=aida
		shirushi=1
		break
	elif blist[aida]>hanbun:
		l=aida
	elif blist[aida]<hanbun:
		u=aida

if shirushi==1 and blist[-1]%2==0:
	print(0)
else:
	if shirushi==0:
		if blist[u]-hanbun>hanbun-blist[l]:
			index=l
		else:
			index=u

	ans=int(2*blist[index]-blist[-1])
	if ans<0:
		ans=int(ans*(-1))
	print(ans)

Submission status: Wrong Answer

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

sum_a=sum(a)
left_a=0
right_a=sum_a
ans=999999999

for i in range(n-1):
    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: Wrong Answer

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

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:
        if odd:
            ans = 2
        else:
            ans = 0
        break    
    if cnt > half:
        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
            
if odd:
    ans -= 1
    
print(ans)

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 2*A_lrmin
    else:
        return 2*A_lrmin+1

print(func(A))

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
A = list(map(int,input().split()))
Length = sum(A)
SUM = 0
ans = 0
if Length%2 == 0:
    for i in range(N):
        SUM += A[i]
        if SUM == Length / 2:
            print(0)
            sys.exit()
        elif SUM > Length / 2:
            if SUM - Length / 2 <= abs(A[i - 1] - A[i] - Length / 2):
                TT = A[i]
            else:
                TT = A[i - 1]
                SUM -= A[i]
            break
    print(int(2*(abs(SUM - Length/2))))
else:
    Length -= 1
    for i in range(N):
        if A[i] >= 2:
            A[i] -= 1
            break
    for i in range(N):
        SUM += A[i]
        if SUM == Length / 2:
            print(1)
            sys.exit()
        elif SUM > Length / 2:
            if A[i] == 2:
                c = 2
            else:
                c = 0
            if SUM - Length / 2 <= abs(A[i - 1] - A[i] - Length / 2):
                TT = A[i]
            else:
                TT = A[i - 1]
                SUM -= A[i]
            break
    print(int(2*(abs(SUM - Length/2)) + 1 - c))

Submission status: Wrong Answer

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(abs(sum(a[:index+1]) - sum(a[index+1:])))

Submission status: Wrong Answer

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

li = [0]*(n+1)

for i in range(n):
    li[i+1] = li[i] + a[i]
    
#print(li[n]%2)
tmp = 10**18
lin = li[n]

if li[n] % 2 == 1:
    lin += 1
    for i in range(n+1):
        #print(abs(li[i]-li[n]//2)<tmp)
        if abs(li[i]-lin//2) < tmp:
            tmp = abs(li[i]-lin//2)
        else:
            break
    lin -= 2
    for i in range(n+1):
        #print(abs(li[i]-li[n]//2)<tmp)
        if abs(li[i]-lin//2) < tmp:
            tmp = abs(li[i]-lin//2)
        else:
            break
elif li[n] % 2 == 0:
    for i in range(n+1):
        #print(abs(li[i]-li[n]//2)<tmp)
        if abs(li[i]-li[n]//2) < tmp:
            tmp = abs(li[i]-li[n]//2)
        else:
            break

#print(lin,lin)

if li[n] % 2 == 0:
    print(tmp*2)
else:
    print(1+tmp*2)

Submission status: Wrong Answer

Solution code:
n=int(input())

# a=[0 for i in range(n)]
a=input().split()
# print(a)
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:
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
'''
print("---")
print(i)
print(sum(data[:i+1]))
print(sum(data[i+1:]))
'''
diff = sum(data[:i+1]) - sum(data[i+1:])
diff = abs(diff)
print(diff)

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

harf = sum(A)//2

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
tmp = 0
std = sum(A)/2
index = 0
while tmp < std:
    tmp += A[index]
    index += 1
a = A[:index]
b = A[index:]
print(abs(sum(a)-sum(b)))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
array = list(map(int, input().strip().split(' ')))
s = sum(array)
half = s//2

count = 0
cc = 0
ans = 0
mm = True
for i in range(N):
    if count == half:
        ans = 0
        mm = False
        break
    elif count > half:
        break
    else:
        count = count + array[i]
        cc = cc + 1
count2 = 0
array.reverse()
for i in range(N - cc):
    count2 = count2 + array[i]

if mm:
    ans = count - count2

print(ans)

Submission status: Wrong Answer

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

#print(sum(a[:(n//2)]))
#print(sum(a[(n//2):]))

if sum(a[:(n//2)])>=sum(a[(n//2):]):
    for i in range(n):
        if sum(a[:i])>sum(a[i:]):
            print(min(abs(sum(a[0:i-1])-sum(a[i-1:])),abs(sum(a[0:i])-sum(a[i:]))))
            break

if sum(a[:(n//2)])<sum(a[(n//2):]):
    for i in range(0,n)[::-1]:
        if sum(a[:i]) > sum(a[i:]):
            print(min(abs(sum(a[0:i - 1]) - sum(a[i - 1:])), abs(sum(a[0:i]) - sum(a[i:]))))
            break

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
cost = 0
a_sum = A.sum()
a_diff = 10 ** 9
a_left = 0
a_right = a_sum

for i in range(N):
#     print(a_sum, a_left, a_right, A[i])
    a_left = a_left + A[i]
    a_right = a_right - A[i]
    if abs(a_left - a_right) < a_diff:
        a_diff = abs(a_left - a_right)
#     print(a_diff)
print(a_diff)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
cost_1 = 0
cost_2 = 0
S = sum(A)
S_1 = sum(A)
S_2 = sum(A)
half = S / 2
l = 0
i = 0
while l < half:
    l += A[i]
    i += 1
if l == S - l:
    print(0)
else:
    l_1 = l
    l_2 = l
    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))

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(map(int, input().split()))
a = 0
b = sum(li)
while a < b//2:
  s = li.pop()
  a += s
c = sum(li)
print(a-c)

Submission status: Wrong Answer

Solution code:
input()
section_length = [int(s) for s in input().split(" ")]

sum_len = sum(section_length)
half_len = sum_len / 2
left_sum = 0
right_sum = 0

left_index, right_index = -1, -1
is_over_left, is_over_right = False, False

for i in range(len(section_length)):
  if is_over_left and is_over_right:
    break
    
  if not is_over_left:
	  left_sum += section_length[i]
  if not is_over_right:
	  right_sum += section_length[-i - 1]
	  
    
  if left_sum >= half_len and not is_over_left:
    left_index = i
    is_over_left = True
    
  if right_sum >= half_len and not is_over_right:
    right_index = i
    is_over_right = True 
    

if left_sum - right_sum == 0:
  print(0)
elif left_sum - right_sum > 0:
  left_sum -= section_length[left_index]
  print(right_sum - left_sum)
else:
  right_sum -= section_length[-right_index-1]
  print(left_sum - right_sum)