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())
A=[int(x) for x in input().split()]
result=float('inf')
for i in range(1,N-1):
  result=min(result,abs(sum(A[:i+1])-sum(A[i+1:])))
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
l = sum(a)
b = list()
t = 0
ans = 10**10
for i in a:
    t += i
    b.append(t)
for j in b:
    x = abs(l-(2*j))
    if x < ans:
        ans = x
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

total = sum(A)

myid = 0
mysum = 0
while mysum < total//2:
    mysum += A[myid]
    myid += 1


print(abs(sum(A[myid:]) - mysum))

Submission status: Wrong Answer

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

total_length = sum(A)
left_length = 0
for i in range(len(A) - 1):
    left_length += A[i]
    if left_length * 2 >= total_length:
        break
cost_from_left = left_length * 2 - total_length

right_length = 0
for j in reversed(range(len(A) - 1)):
    right_length += A[j]
    if right_length * 2 >= total_length:
        break
cost_from_right = right_length * 2 - total_length

print(min(cost_from_left, cost_from_right))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import  numpy as np

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    L = R = 0
    c = 0
    M = sum(A)/2
    for a in A:
        if L+a <= M:
            L += a
        elif not c and L+a > M:
            if L == M:
                c = -1
                R += a
            else:
                c = a
        elif c:
            R += a
    if c == -1:
        res = 0
    else:
        res = min(L+c-R, R+c-L)
    return str(res)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

PA = [0]
for i, a in enumerate(A):
    PA.append(a + PA[-1])

res = float("inf")
for i in range(1, len(PA) - 1):
    if PA[i] == PA[-1] - PA[i]:
        res = 0
    res = min(res, PA[i + 1] - PA[i])

print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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:
        a_half_next_dif = A_sum - a_half_next
        a_half_dif = A_sum - a_half
        break
    a_half = a_half_next

if a_half_next_dif <= a_half_dif:
    a_half_dif = a_half_next_dif
    a_half = a_half_next
dif = abs(a_half - a_half_dif)
print(dif)

Submission status: Wrong Answer

Solution code:

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

S=sum(A)
T=0

for i in range(N):
    if S<=T:
        break
    T+=A[i]
    S-=A[i]

print(abs(S-T))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))
cumsum = np.cumsum(A)
sumA = sum(A)
ans = 0
if sumA%2 != 0:
    sumA -= 1
    ans += 1
for i in range(0,N,2):
    if sumA - i in cumsum:
        print(ans+i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
if int(s/n)!=s/n:
    print(0)
    exit()
s2=s/2
cnt=0
for i,k in zip(a,range(len(a))):
    cnt+=i
    if cnt>s/2:
        break

if cnt-a[k]<s-cnt:#小さいほう
    print(s-cnt)
else:
    print(s-(s-cnt+a[k]))

Submission status: Wrong Answer

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

ans = 0
cut_point = N // 2
if N % 2 == 0:
    ans = abs(sum(A_list[0:cut_point]) - sum(A_list[cut_point:N]))
else:
    ans = abs(sum(A_list[0:cut_point]) - sum(A_list[cut_point + 1:N]))
    ans = abs(ans - A_list[cut_point])
print(ans)

Submission status: Wrong Answer

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

L = sum(A)

sum = 0

for i in range(N):
    sum += A[i]
    if sum >= L / 2:
        index = i
        break

print(2 * sum - L)

Submission status: Wrong Answer

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

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

left = 0
right = 0
i = 0
for _ in range(N // 2):
    left += A[i]
    right += A[-1 - i]
    i += 1

# print(left)
# print(right)
if N % 2 == 1:
    if right > left:
        left += A[i]
        i += 1
    else:
        right += A[-1 - i]

# print(left)
# print(right)

print(abs(left-right))

Submission status: Wrong Answer

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

c= 0

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

print(c-a)

Submission status: Wrong Answer

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

sum = 0
for i in range(len(A)):
    sum += A[i]

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:
        print(sum - check_sum)
        exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
l = 0
for i, ai in enumerate(a):
    l += ai
    if l >= total // 2:
        break
print(min(abs(l - (total - l)), abs(total - (l - ai))))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	manaka = 0
	tmp = 0
	for i,val in enumerate(A):
		tmp += i
		if tmp >= to:
			manaka = i

	to1 = 0
	to11 = 0
	to2 = 0
	to22 = 0

	to111 = 0
	to222 = 0

	for i,val in enumerate(A):
		if i >= manaka+1:
			to2 += val
		else:
			to1 += val

		if i >= manaka-1:
			to22 += val
		else:
			to11 += val

		if i >= manaka:
			to222 += val
		else:
			to111 += val

	ans = min(abs(to1-to2),abs(to11-to22),abs(to111-to222))
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

    total = sum(As)
    half = total//2

    sum_left = 0
    idx = 0

    for i, l in enumerate(As[:-1]):
        sum_left += l
        if sum_left >= half:
            idx = i
            break

    sum_right = sum(As[idx+1:])

    print(abs(sum_right - sum_left))


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

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

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

cost = 0
sum = sum(a)
half = int(sum/2)
if(sum%2 == 1):
  count = 1
else:
  count = 0

for i in (a):
  count += i
  if(count == half):
    print(cost)
    sys.exit()
  elif(count > half):
    print(cost + (count - half))
    sys.exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
rA = reversed(A)
r = 0
l = 0
turn = 1 # 1:l , -1:r
while len(A)>0:
  a = 0
  if turn == 1:
    a = A.pop(0)
  else:
    a = A.pop(-1)
  if r > l:
    l += a
  else:
    r += a
  turn *= -1

print(abs(r-l))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	manaka = 0
	tmp = 0
	for i,val in enumerate(A):
		tmp += i
		if tmp >= to:
			manaka = i

	to1 = 0
	to11 = 0
	to2 = 0
	to22 = 0

	to111 = 0
	to222 = 0

	for i,val in enumerate(A):
		if to1 >= to:
			to2 += val
		else:
			to1 += val

		if i >= manaka-1:
			to22 += val
		else:
			to11 += val

		if i >= manaka:
			to222 += val
		else:
			to111 += val

	ans = min(abs(to1-to2),abs(to11-to22),abs(to111-to222))
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
if x%2==0:
  X=x/2
else:
  X=(x-1)/2

S=0
for i in range(n):
  S+=a[i]
  if S >=X:
    break
print(abs(S-(x-S)))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
all_sum = sum(A)
left_sum = 0
ans = 1<<30
for a in A:
	left_sum += a
	right_sum = all_sum - left_sum
	ans = min(ans, abs(right_sum - left_sum))
print(ans)

Submission status: Wrong Answer

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

sum_tmp = 1e9

for i in range(len(a)):
    for j in range(i,len(a)):
        tmp = [sum(a[0:i]),sum(a[i:len(a)])]
        if sum_tmp >= abs(tmp[0] - tmp[1]):
            sum_tmp = abs(tmp[0] - tmp[1])

print(sum_tmp)

Submission status: Wrong Answer

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

bar = [0]
bar_tmp = 0
for Ai in A:
    bar_tmp += Ai
    bar.append(bar_tmp)

A_sum = sum(A)
targ = A_sum / 2

for i_r in range(N):
    if bar[i_r] > targ: break
i_l = i_r - 1

# pen_l = A_sum // 2 - bar[i_l]
# pen_r = bar[i_r] - (A_sum + 1) // 2

# if pen_l < pen_r:
#     print(A_sum - 2 * bar[i_l])
# else:
#     print(2 * bar[i_r] - A_sum)

print(min(A_sum - 2 * bar[i_l], 2 * bar[i_r] - A_sum))

Submission status: Wrong Answer

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 = 0
    for i in range(N):
        a = A[i]
        A[i] += s
        s += a
    import bisect
    bl = bisect.bisect_left(A, A[-1]/2)
    br = bisect.bisect_right(A, A[-1]/2)
    b = bl if (A[-1]/2 - A[bl]) < (A[br] - A[-1]/2) else br
    print(abs((A[-1] - A[b]) - A[b]))


main()

Submission status: Wrong Answer

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

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

id = bisect.bisect_left(s, s[-1] // 2)
if s[id] == s[-1] // 2:
    if s[-1] % 2 == 0:
        print(0)
    else:
        print(1)
else:
    k = 0
    if s[-1] % 2 != 0:
        k = 1
    ans = abs(s[id]*2 - s[-1])
    if id > 0:
        ans = min(ans, abs(s[id-1] - s[-1]))
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
sum = np.sum(A)
cumsum = np.cumsum(A)
diff = float("inf")
for n in range(1,N):
    left = cumsum[n]
    right = sum-left
    if abs(left-right) < diff:
        diff = abs(left-right)
print(diff)

Submission status: Wrong Answer

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

s = sum(a)
i = 0
b = 0
while b<int(s/2):
  b += a[i]
  i += 1
h = s-b
print(abs(b-h))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 0
f = 0
for i in range(n):
  k += a[i]
  if k >= s//2:
    f = i
    break
k = max(k, s - k)
print(2*k - s)

Submission status: Wrong Answer

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

    now=0
    for a in AL:
        now+=a
        if now>target:
            bef=target-(now-a)
            if int(bef*2)%3==0:
                return int(bef/1.5)
            elif int((bef-0.5)*2)%3==0:
                return int((bef-0.5)/1.5+1)
            else:
                return int((bef-1)/1.5+2)
print(solve())

Submission status: Wrong Answer

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

tmp1 = sum(a[0:n//2])
tmp2 = sum(a[n//2:])
ans1 = abs(tmp1 - tmp2)

tmp3 = sum(a[0:n//2 + 1])
tmp4 = sum(a[n//2 + 1:])
ans2 = abs(tmp3 - tmp4)

print(min(ans1, ans2))

Submission status: Wrong Answer

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

len = sum(list)
nlen = 0
d = 10000000
for i in list:
    nlen += i
    d = min(d,abs(len//2-nlen),abs(nlen-(len+1)//2))

if len%2==1:
    print(d+1)
else:
    print(d)

Submission status: Wrong Answer

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

for i in range(n):
  if sum(a[i:]) - sum(a[:i]) > a[i+1]:
    continue
  else:
    print(abs(sum(a[i:]) - sum(a[:i])))
    break

Submission status: Wrong Answer

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


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

    print(int(abs((M-l)*2)))

    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
l = 0
for i, ai in enumerate(a):
    l += ai
    if l >= total // 2:
        break
print(min(l - (total - l), total - (l - ai)))

Submission status: Wrong Answer

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

    total = sum(As)
    half = total//2

    sum_left = 0
    idx = 0

    for i, l in enumerate(As):
        sum_left += l
        if sum_left >= half:
            idx = i
            break

    sum_right = sum(As[idx+1:])

    print(abs(sum_right - sum_left))


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 10**9+7
for i in range(N):
    l = sum(A[:i])
    #print(l)
    r = sum(A[i:])
    #print(r)
    diff = abs(l-r)
    if diff < ans:
        ans = diff
print(ans)

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n = INT()
l = MAP()

s = sum(l)
tmp=0
for i in range(n):
    tmp+=l[i]
    if tmp >= s/2:
        idx = i
        break

left = sum(l[:idx+1])
right = sum(l[idx+1:])
print(abs(right-left))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = 0
t = sum(a)
for i in range(n-1):
  s += a[i]
  if s - t / 2 > 0:
    break

s1 = s - a[i]    
print(min((t - s1 -s1), (s- (t - s) )))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

N = int(input())
A = list(map(int, input().split()))
B = list(accumulate(A))
ans = float('inf')
for i in range(1, N):
    ans = min(ans, abs(B[i]-(B[-1]-B[i])))

print(ans)

Submission status: Wrong Answer

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

def readInts():
  return [int(s) for s in input().split(" ")]

def solve(N, A):
  s = sum(A)

  sp = 0
  for a in A:
    sp += a
    if sp == s:
      sp -= a
      break
    if sp >= s / 2:
      break

  return abs(2 * sp - s)

def main():
  N = readInts()
  A = readInts()

  print(solve(N, A))

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	to1 = 0
	to11 = 0
	to2 = 0
	to22 = 0

	to111 = 0
	to222 = 0

	for i in A:
		if to1 >= to:
			to2 += i
		else:
			to1 += i

		if to11 >= to-1:
			to22 += i
		else:
			to11 += i

		if to111 >= to+1:
			to222 += i
		else:
			to111 += i

	ans = min(abs(to1-to2),abs(to11-to22),abs(to111-to222))
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from bisect import bisect_left

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

    B = list(accumulate(A))
    B_index = bisect_left(B, sum(A) / 2)
    ans = sum(A[:B_index+1]) - sum(A[B_index+1:])

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

len = sum(list)
nlen = 0
d = 10000000
for i in list:
    nlen += i
    d = min(d,abs(len//2-nlen))

if len%2==1:
    print(d+1)
else:
    print(d)

Submission status: Wrong Answer

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

sum_all = sum(A)
sum_front = 0
for a in A:
    sum_front += a
    if sum_front >= sum_all / 2:
        print(sum_front * 2 - sum_all)
        exit()

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
d_A = sum(A)

d1 = 0
d2 = d_A
delta = 10000000
for i in range(0,N):
    d1 += A[i]
    d2 -= A[i]
    if abs(d1 - d2) < delta:
        delta = abs(d1-d2)
print(delta)

Submission status: Wrong Answer

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

length = sum(a)
if length %2 ==0:
    check = 0
    half = length//2
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    if check - half > half - (check-a[idx]):
        check -=a[idx]
    
    ans = (half-check)*2
else:
    check = 0
    half = length//2+1
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    if check - half > half - (check-a[idx]):
        check -=a[idx]
    
    ans = abs((half-check))*2+1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 0
s = sum(a)
if n==2:
    print(a[1]-a[0])
    exit()
i = 0
if s%2==0:
    while b<s//2:
        b+=a[i]
        i+=1
else:
    while b<=s//2:
        b+=a[i]
        i+=1
c =2*b -s
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
S=sum(A)//2
cnt=0
i=0
while cnt<S:
    cnt+=A[i]
    i+=1
if sum(A[i:])>cnt:
    print(sum(A[i:])-cnt)
elif sum(A[i:])<cnt:
    print(cnt-sum(A[i:]))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
cost = (abs(sum(A[:N // 2]) - sum(A[N // 2:])),
        abs(sum(A[:N // 2 + 1]) - sum(A[N // 2 + 1:])))
print(min(cost))

Submission status: Wrong Answer

Solution code:

n = input().split(' ')
third = 100000
second = 200000
first = 300000
sum = 0

for i in n:
    if int(i) == 3:
        sum = sum + third
    elif  int(i) == 2:
        sum = sum + second
    elif  int(i) == 1:
        sum = sum + first

if int(n[0]) == 1 and int(n[1]) == 1:
    sum = sum + 400000

print(sum)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

co = sum(a)
su = 0
for i in range(n):
    su += a[i]
    if su >=co/2:
        k,l = i-1,i
        break
print(abs(2*su-co))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 0
f = 0
for i in range(n):
  k += a[i]
  if k >= s//2:
    f = i
    break
print(2*k - s)

Submission status: Wrong Answer

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

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

id = bisect.bisect_left(s, s[-1] // 2)
if s[id] == s[-1] // 2:
    if s[-1] % 2 == 0:
        print(0)
    else:
        print(1)
else:
    print(abs(s[id] - s[-1]))

Submission status: Wrong Answer

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

now_length = sum(A)
half = now_length / 2
low_price = now_length - A[1]

for i in range(2,N) :
  var = now_length - sum(A[:i])
  if low_price > var :
    low_price = var
  if low_price == half :
    low_price = 0
    break

print(low_price)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	to1 = 0
	to11 = 0
	to2 = 0
	to22 = 0

	for i in A:
		if to1 >= to:
			to2 += i
		else:
			to1 += i

		if to11 >= to-1:
			to22 += i
		else:
			to11 += i

	ans = min(abs(to1-to2),abs(to11-to22))
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
def main():
    n = int(input())
    A = list(map(int, input().split()))
    ave = sum(A) / 2
    total = 0
    
    b = 0
    a = 0
    
    for i in range(n):
        total += A[i]
        if total >= ave:
            b = sum(A[:i+1])
            a = sum(A[i+1:])
            break
    print(max(a,b)-min(a,b))
    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
ans = 2000000
for i in range(1, N):
    ans = min(ans, abs(sum(A[:i]) - sum(A[i:])))
print(ans)

Submission status: Wrong Answer

Solution code:
n, *a = map(int, open(0).read().split())
l, r = a[0], sum(a[1::])
ans = 1e20
for i in range(1, n):
    l += a[i]
    r -= a[i]
    ans = min(ans, abs(l-r))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
alen = len(A)

if alen % 2 == 0:
    l = sum(A[:alen//2])
    r = sum(A[alen//2:])
else:

    l1 = sum(A[:alen//2])
    r1 = sum(A[alen//2:])
    l2 = sum(A[:alen//2+1])
    r2 = sum(A[alen//2+1:])

    if abs(l1-r1) >= abs(l2-r1):
        l, r = l2, r2
    else:
        l, r = l1, r1


print(abs(l-r))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=sum(a)
d=c/2
cnt=0
count=0
for i in range(n):
    cnt+=a[i]
    if cnt>=d:
        count=i
        break
print(2*cnt-c)

Submission status: Wrong Answer

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
s = sum(Alist)
min_sbn = 10**9
for i in range(N - 1):
    left = sum(Alist[:i + 1])
    right = s - left
    min_sbn = min(min_sbn, abs(left - right))
print(min_sbn)

Submission status: Wrong Answer

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

import numpy as np

A=np.array(A)

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

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

Submission status: Wrong Answer

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

total_A = sum(A)
half_A = total_A /2.0

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

min = 9999999999
for i in range(n):
  tmp = math.ceil(abs(A[i] - half_A))
  if tmp < min:
    min = tmp
print(min)