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:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

#import bisect

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

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

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

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

    return result

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

    print(s)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_sekisan=0
a=[]
for i in range(N):
    A_sekisan = A_sekisan + A[i]
    a.append(A_sekisan)
dif_list = []
for i in range(N):
    dif = abs(float(sum(A))/2 - a[i])
    dif_list.append(dif)
cutnum = dif_list.index(min(dif_list))
money=sum(A[:cutnum+1])-sum(A[cutnum+1:])
print(money)

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 abs(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(abs(sum(A[idx:]) - sum(A[:idx])))
else:
    print(abs(sum(A[idx:]) - sum(A[:idx])))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []*n
a = list(map(int,input().split()))
from itertools import accumulate
import bisect
ans = 0
half = sum(a)//2
if sum(a)%2==1:
    ans+=1
b = list(accumulate(a))
ans += min((half - b[bisect.bisect_left(b,half)-1]), (b[bisect.bisect_left(b,half)] -half)) 
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

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

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

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

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
            c = 1
            break
    for i in range(N):
        SUM += A[i]
        if SUM == Length / 2:
            print(1)
            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)) + 1))

Submission status: Wrong Answer

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

avr = int(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()))
sum = 0
c_1 = 0
c_2 = 0
for i in range(n):
    sum += a[i]

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

Submission status: Wrong Answer

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

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

else:
    for i in range(n//2):
        count += a[i]
    for i in range(n//2+1):
        count_2 += a[i]
    if count > count_2:
        ans = abs(L - count*2)
    else:
        ans = abs(L - count_2*2)

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# Submit as pypy2


from __future__ import print_function, division, unicode_literals

__metaclass__ = type

try:
    input = raw_input
    range = xrange
except NameError:
    pass


import sys


# Test code requires python3
# from hypothesis import given
# from hypothesis import strategies as st
#
#
# def debug(*args, **kwargs):
#     print(*args, *('{}={}'.format(k, v) for k, v in kwargs.items()),
#           sep='; ', file=sys.stderr)


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    tot = sum(A)
    i = 0
    psum = 0
    while i < N and 2*psum < tot:
        psum += A[i]
        i += 1
    print(2*psum - tot)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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 < len(A)-1:
        shrink_cnt += A[idx] - 1
        
        agg += A[idx]
        idx += 1
    
    expand = (agg-mid) / 0.5
    shrink = (mid - (agg-A[idx]))/0.5

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

if __name__ == '__main__':
    solve()

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 = [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
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 = list(map(int,input().split()))

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:

N = int(input())
A = [int(i) for i in input().split()]
B = [sum(A[:i]) for i in range(N+1)]
ans = 0
if B[-1]%2 == 1:
    mid = min(range(N),key=lambda x: abs(B[x]-B[-1]//2))
    A[mid-1] += 1
    ans += 1
    B = [sum(A[:i]) for i in range(N+1)]
d = 10**11
for i in range(N):
    d = min(d,abs(B[i]-B[-1]/2))
print(int(d)+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
print(int(abs(tmp-(sum(A)-tmp))))

Submission status: Wrong Answer

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

d = 1000
min_i = 0
S_left = 0
S_right= 0

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

    if d == 0:
        break

print(abs(d))

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 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())
A=list(map(int,input().split(" ")))

s=N//2+int(N%2==1) # 分割の番号、右側の一番左
mind=2020202020 # これまでの最初値、これから更新してゆく

# 棒の分け方がありえなくなるまで続行
while s>0 and s<N:
    l=sum(A[:s]) # 左側の合計
    r=sum(A[s:]) # 右側の合計
    d=abs(l-r) # 差

    # print("s, left, right, diff = ",s,l,r,d)

    if d<=mind: # もし最小値を下回ったら続行
        mind=d # 最小値を更新

        if d==0: # 差が0の場合は終了
            break
        else: # 差が1以上の場合は続行
            if l<r: # もし右側の方が大きいとき
                s+=1 # 右側に移動する
            elif l>r: # 左側の方が大きいとき
                s-=1 # 左側に移動する
    elif d>mind: # もし最小値が更新されなかったら終了
        break

print(mind)

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 = abs((A_sum // 2) - a_half_next)
        a_half_dif = abs((A_sum // 2) - 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
    
if A_sum % 2 == 0:
    dif = a_half_dif
else:
    dif = a_half_dif
print(dif)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n=int(input().rstrip())
a_list=list(map(int,input().split()))

x = abs(sum(a_list[:len(a_list)//2]) - sum(a_list[len(a_list)//2:]))
y = abs(sum(a_list[:len(a_list)//2+1]) - sum(a_list[len(a_list)//2+1:]))

print(min(x,y))

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
for a in aa:
    total += a
    if total == half:
        print(0)
        exit()
    if total > half:
        cnt += (total-half)*2
        break

print(cnt)

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
            c = 1
            break
    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)) + 1))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

S = sum(Nlist)
L = len(Nlist)
if S % 2 == 0:
    H_S = S // 2
else:
    H_S = S // 2 + 1

A = 0
count = 0
for i in Nlist:
    A += i
    count += 1
    if A >= H_S:
        break
if S % 2 == 0:
    D = S - A
    print(abs(D - H_S))
    
if S % 2 != 0:    
    D = S - A
    print(abs(H_S - D + 1))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

sum_first_half = 0
sum_second_half = 0
if N % 2 == 0: # even number
    for n in range(N):
        if n < N/2:
            sum_first_half += notches[n]
        else:
            sum_second_half += notches[n]
        
    print(abs(sum_first_half - sum_second_half))

else:
    for n in range(N):
        if n < N//2:
            sum_first_half += notches[n]
        elif n > N//2:
            sum_second_half += notches[n]

    diff1 = abs((sum_first_half + notches[N//2]) - sum_second_half)
    diff2 = abs((sum_second_half + notches[N//2]) - sum_first_half)
    print(min(diff1, diff2))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
mindif = 1e11
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(mindif)

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])
	print(ans)

Submission status: Wrong Answer

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

ave = 0
for a in A:
    ave += a
ave = ave/2

anslist = []
lefts = 0
for a in A:
    lefts += a 
    anslist.append(abs(ave-lefts))

if N%2 == 1:
    anslist = list(map(lambda x: x*2, anslist))
print(int(min(anslist)))

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
print(int(abs(tmp-std)))

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 = abs((A_sum // 2) - a_half_next)
        a_half_dif = abs((A_sum // 2) - 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
    
if A_sum % 2 == 0:
    dif = a_half_dif
else:
    dif = a_half_dif + 1
print(dif)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_sum = sum(a)
a_mid = a_sum//2

for i in range(n):
  a_num = sum(a[:i])*2
  if a_num < a_mid:
      continue
  else:
    if a_sum - a_num - a[i-1] >= 0:
      print(min(a_sum - a_num, a_sum - a_num - a[i-1]))
      break
    else:
      print(a_sum - a_num)
      break

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

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

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

Submission status: Wrong Answer

Solution code:

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

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

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
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)
else:
    print(1+tmp*2)

Submission status: Wrong Answer

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

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

mx = sum(A)

count = 0
for i in range(len(A)):
    count += A[i]
    if mx / 2 <= count:
        break

other = mx - count

print(abs(other - count))

Submission status: Wrong Answer

Solution code:
n=int(input())

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

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

#print(r)

temp=sum(r)

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

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]) == x:
            flag = True
            break
        elif sum(list_[:i]) > x:
            r = i
        else:
            l = i+1
    # return flag
    return (r+l)//2, flag

idx, flag = binary_search(A, half)
if flag:
    print(abs(sum(A[idx:]) - sum(A[:idx])))
else:
    print(abs(sum(A[idx:]) - sum(A[:idx])))

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():
    for i in range(N):
        if sum(A[:i]) == sum(A[i:]):
            return True
    return False


def check_distance():
    ret = []

    for i in range(1, N):
        tmp_a = abs(sum(A[:i]) - sum(A[i:]))
        ret.append(tmp_a)
        # print(f"at {i}, abs:{tmp_a}")
    return min(ret)

def check_distance_2():
    ret = []
    tmp_a = abs(sum(A[:1]) - sum(A[1:]))
    # print(tmp_a)
    for i in range(1, N):
        tmp_a = abs(tmp_a - (A[i]*2))
        ret.append(tmp_a)
        # print(tmp_a)
        # print(f"at {i}, abs:{tmp_a}")
    return min(ret)


if check():
    print(0)
else:
    print(check_distance_2())

# print("N:", N)
# print("A:", A)

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
print(int(tmp-std))

Submission status: Wrong Answer

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


# def f(sum_n):
#     temp = 0
#     if(sum_n % 2 != 0):
#         return False
#     sum_n = sum_n / 2
#     for i in a:
#         temp += i
#         if (temp == sum_n):
#             return True

#     return False


if (sum(a) % 2 == 0):
    result = 0
else:
    count = 0

    sum_n = sum(a) // 2

    temp = 0

    temp_b = 0

    pos = 0

    for i in a:
        temp += i
        if (temp > sum_n):
            pos = count
            break
        temp_b = temp
        count += 1

    result = temp - temp_b - 1

print(result)

Submission status: Wrong Answer

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

mini = 2020202020 * 200000

all_sum = sum(A_list)

for j in range(1,N):
    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum

    delta = abs(B_sum - F_sum)
    print(sum(F), B_sum)
    if delta <= mini:
        mini = delta

print(mini)

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 < len(A)-1:
        shrink_cnt += A[idx] - 1
        
        agg += A[idx]
        idx += 1
    
    expand = (agg-mid) / 0.5
    shrink = (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()))
half=sum(a)/2
count=0
mid=0
tag=0
for i in range(len(a)):
  count+=a[i]
  if count>=half:
    mid=a[i]
    tag=i
    break
right=sum(a[:(tag+1)])-half
left=mid-right
if right==0:
  print(0)
else:
  print(int(right*2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

over_idx = len(a)
for i in range(1,len(a)):
    if sum(a[:i]) >= mid:
        over_idx = i - 1
        break

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

ans = min(diff1, diff2)

print(ans)

Submission status: Wrong Answer

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

a.sort(reverse=True)
for i in range(n):
    tmp += a[i]
    if tmp >= suma - tmp:
        ret = tmp  - (suma - tmp)
        cost = min(cost, ret)

print(cost)

Submission status: Wrong Answer

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


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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
seq = input().split()
seq = [int(l) for l in seq]
i = 1
j = n-2
right = seq[0]
left  = seq[-1]

for k in range(n):
  if right < left and i <= j:
    right += seq[i]
    i += 1
  elif left < right and i <= j:
    left += seq[j]
    j -= 1

print(abs(right-left))

Submission status: Wrong Answer

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

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


def chker(n, alist):
    length = sum(alist)
    half = length / 2
    i, ls = 0, 0

    for index, ai in enumerate(alist):
        ls += ai
        i = index
        if ls >= half:
            break

    rlist = alist[i + 1:]
    rs = sum(rlist)
    print(ls - rs)


chker(_n, _alist)

Submission status: Wrong Answer

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

half_len = sum(A) / 2
now_len = 0
most_near_len = abs(half_len - A[0])
for i in A:
    now_len += i
    if abs(half_len - now_len) <= abs(half_len - most_near_len):
        most_near_len = now_len

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

Submission status: Wrong Answer

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

harf = sum(A)//2

for i in range(N):
  if sum(A[:i]) >= harf:
    break

print(sum(A[:i])-sum(A[i:]))

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]) == x:
            flag = True
            break
        elif sum(list_[:i]) > x:
            r = i
        else:
            l = i+1
    # return flag
    return i if flag else r-1, flag

idx, flag = binary_search(A, half)
if flag:
    print(abs(sum(A[idx:]) - sum(A[:idx])))
else:
    print(abs(sum(A[idx+1:]) - sum(A[:idx+1])))

Submission status: Wrong Answer

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

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

else:
    for i in range(n//2):
        count += a[i]
    for i in range(n//2+1):
        count_2 += a[i]
    if count > count_2:
        ans = abs(L - count)
    else:
        ans = abs(L - count_2)

print(ans)

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(len(A)):
        a = A[i]
        A[i] += s
        s += a
    import bisect
    b = bisect.bisect_left(A, A[-1]/2)
    b = min(b, N-2)
    print(abs((A[-1] - A[b]) - A[b]))


main()

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
    x = sum(A) - 2*A_inf
    y = 2*A_sup - sum(A)
    result = min(x,y)

    if A_sup - y < N_sup:
        return x
    else:
        return result

print(func(A))

Submission status: Wrong Answer

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

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

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

half = int(length / 2)

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

if (sum != half):
    sum -= listA[i - 1]

print(int(half - sum))