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 copy
from bisect import bisect_left
N=int(input())
la=list(map(int,input().split()))
ll=copy.copy(la)
#lr=copy.copy(la)
for i in range(1,N):
    ll[i]+=ll[i-1]
#for i in range(N-2,-1,-1):
#    lr[i]+=lr[i+1]
print(ll)
inl=bisect_left(ll,ll[-1]/2)
ds=ll[-1]/2-ll[inl-1]
db=ll[inl]-ll[-1]/2
if ds<=db:
    print(ll[-1]-ll[inl-1]*2)
else:
    print(ll[inl]*2-ll[-1])

Submission status: Wrong Answer

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

suma=sum(a)
cost=10**10
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:
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 = 2*i+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)) + c))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
len_list = list(map(int, input().split()))
length = sum(len_list)
index = 0
left = 0
for i, l in enumerate(len_list):
  left += l
  if left > length / 2:
    index = i
    break
  
# 出力
print("{}".format(abs(len_list[index]-len_list[index-1])))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
s = sum(a)
cnt = 0
i = 0
j = 0
while j < s//2:
    i = j
    j +=  a[cnt]
    cnt += 1

res = 0
if s % 2 == 1:
    res += 1
    i += 1
    j -= 1
if j == s//2:
    pass
else:
    tmp = min(s//2 - i, j - s//2)
    res += 2 * tmp

print(res)

Submission status: Wrong Answer

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


if N%2 == 1:
    if (s - sum(A[0:n+1])) < (s - sum(A[n:N])):
        r = (s - sum(A[n:N]))

    elif (s - sum(A[0:n+1])) > (s - sum(A[n:N])):
        r = (s - sum(A[0:n+1]))
    else:
        pass

else:
    if (s - sum(A[0:n])) < (s - sum(A[n:N])):
        r = (s - sum(A[n:N])) - (s - sum(A[0:n]))

    elif (s - sum(A[0:n])) > (s - sum(A[n:N])):
        r = (s - sum(A[0:n])) - (s - sum(A[n:N]))

    else:
        pass

print(r)

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]
mydiff = 0

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:
n = int(input())
la = list(map(int, input().split()))
ld = []
for j in range(1, n):
    ld.append(abs(sum(la[0:j+1])-sum(la[j+1:n])))
print(min(ld))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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(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(min(d-c+a[i],c-d-1))
            exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
len_list = list(map(int, input().split()))
length = sum(len_list)
index = 0
left = 0
for i, l in enumerate(len_list):
  left += l
  if left > length / 2:
    index = i
    break
  
# 出力
print("{}".format(abs(len_list[index]-len_list[index])))

Submission status: Wrong Answer

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

mx = sum(A)

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

mx_d = mx
count_d = count
while True:
    if mx_d % 2:
        mx_d -= 1
        count_d -= 1
        continue
    elif (mx_d // 2) == count_d:
        d = count - count_d
        break
    else:
        mx_d -= 1
        count_d -= 1

mx_u = mx
count_u = mx - count
while True:
    if mx_u % 2:
        mx_u += 1
        count_u += 1
        continue
    elif (mx_u // 2) == count_u:
        u = count_u - (mx - count)
        break
    else:
        mx_u += 1
        count_u += 1

if d > u:
    print(u)
else:
    print(d)

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

if li[n] % 2 == 1:
    li[n] += 1
    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
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

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

Submission status: Wrong Answer

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


if n%2 == 0:
    ans = abs(sum(a_l[:int(n//2)]) - sum(a_l[int(n//2):]))
else:
    tmp = abs(sum(a_l[:int((n-1)//2)]) - sum(a_l[int((n-1)//2)+1:]))
    ans = abs(a_l[int((n-1)//2)]-tmp)
    
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
import heapq

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

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

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    


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

    
def main(): 
    S = [0]*N
    S[0] = A[0]
    for n in range(1,N):
        S[n] += S[n-1]+A[n]
    S2 = S[n]/2
    li = bisect.bisect_left(S, S2)
    if S2 == S[li]:
        return print(0)
    if abs(S2 - S[li-1]) < abs(S[li]-S2):
        res = abs(S2//1-S[li-1])
    else:
        res = abs(S2//1-S[li])
    if (S[n]+res) % 2 == 1:
        res += 1
        
    print(int(res))
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()



"""
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())


all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
# itemgetterには何番目の配列をキーにしたいか渡します
A, B, totAB = zip(*ABT)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

#Distance Transformation
    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)

"""

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
    if p!=len(a)-1:
        s=min(temp-suma//2,temp)
        if suma%2!=0:
            s+=1
    else:
        s=min(a[p]-suma//2,suma//2-temp+a[p])
        s*=2
        if suma%2!=0:
            s+=1
    print(s)
if __name__ == "__main__":
    main()

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 - sum(A[:int(N/2)])

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

if low_price == half :
  print(0)
else :
  print(low_price)

Submission status: Wrong Answer

Solution code:
from math import *
n=int(input())
a=list(map(int,input().split()))
ans=100000000000000000000000000000
sum=0
c=0
b=[]
cnt=[]
for x in a:
    sum+=x
    if(x==1):
        c+=1
    b.append(sum)
    cnt.append(c)
for i in range(0,n):
    one=b[i]
    two=sum-b[i]
    res=(one+two)//2
    diff1=abs(one-res)
    diff2=abs(two-res)
    if(one>res):
        if(i+1-cnt[i]>=diff1):
            ans=min(ans,diff1+diff2)
    elif(two>res) :
            if(n-i-(c-cnt[i])>=diff2):
                ans=min(ans,diff1+diff2)
    else:
        ans=0
print(ans)

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) / 0.5
print(int(cnt))

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

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

bar_len = sum(A)

target = bar_len / 2

value = 0
index = 0
flag = 0
for i in range(len(A)):
    value += A[i]
    if value >= target:
        error1 = value - target
        error2 = target + A[i] - value
        if error1 < error2:
            index = i
            break
        else:
            index = i - 1
            value -= A[i]
            break
if bar_len % 2 == 0:
    print(int(target - value))
else:
    target = (bar_len + 1) // 2
    print(2*value - bar_len)

Submission status: Wrong Answer

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

sumh = sum(A)
h = sumh// 2
preS = 0
S= A[0]
for ii in range(1, N):
    # print ("aa", ii, S)
    if (S >= h and preS >= h):
        print (S - h)
        # print (1)
        break
    elif (preS <= h and S >= h):
        if S - preS != 1:
            print (min(2 * S - sumh, sumh- 2 * preS))
        else:
            print (sumh  - 2 * preS)
            # print (2)
        break
    preS = S
    S += A[ii]

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))
else:
    print(half)

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 x in A:
        if a >= s // 2:
            b += x
        else:
            a += x
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans=3*(10**9)
for i in range(N-1):
  l=sum(A[0:i])
  D=abs(l-(sum(A)-l))
  if ans > D:
    ans=D

print(ans)

Submission status: Wrong Answer

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

length = 0
lengths = []
for a in aaa:
  length += a
  lengths.append(length)
  
diff = 9999999999
for l in lengths:
  if abs(l - (length - l)) < diff:
    diff = abs(l - (length - l))
  else:
    break
    
print(diff)

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 = abs(agg-mid) / 0.5
    shrink = abs(agg-A[idx]-mid)/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()))
mindif = 1e9
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:
import sys

def solve(n, a):
    all_s = sum(a)
    half = all_s / 2
    s = 0
    for a_i in a:
        s += a_i
        if s >= half:
            print(2*s - all_s)
            return

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    a = list(map(int, sys.stdin.readline().strip().split(" ")))
    solve(n, a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])
    A = tuple(map(int, args[1].split()))

    left = 0
    right = sum(A)

    ret = 0
    for i in range(0, n-1):
        r_g = right - left
        left += A[i]
        right -= A[i]
        if left == right:
            break
        elif left > right:
            l_g = left - right
            ret = min(r_g, l_g)
            break

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = np.array(a)
    sa = sum(a)
    half = int(sa/2)
    tmp = 0
    ans = 0
    flag = 0

    for i in range(n):
        tmp = tmp + a[i]
        if tmp >= half:
            if 2*tmp - sa < sa - 2*(tmp-a[i]):
                break
            else:
                tmp = tmp - a[i]
                flag = 1
                break
    
    if flag == 0:
        if sa % 2 == 0:
            ans = tmp - (sa/2)
        else:
            ans = tmp - int(sa/2) + 1
    else:
        if sa % 2 == 0:
            ans = (sa/2) - tmp
        else:
            ans = int(sa/2) - tmp + 1
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[0]+list(map(int,input().split()))
sum_A=sum(A)//2
for i in range(n):
    A[i+1]=A[i]+A[i+1]
add=10**8
point=0
for i in range(n):
    if add>=abs(A[i]-sum_A):
        add=abs(A[i]-sum_A)
        point=i
if add==0:print(0);exit()
print(min(A[point]-A[point-1],A[point+1]-A[point]))

Submission status: Wrong Answer

Solution code:
import sys
n = int(sys.stdin.readline())
a  = [int(n) for n in input().split()]
y = 0
summ = sum(a)

for nn, _ in enumerate(a):
    if summ / 2 <= sum(a[:nn]):
        h = sum(a[:nn]) - sum(a[nn:])
        hh = sum(a[:nn -1]) - sum(a[nn - 1:])
        if h > hh:
            y += h
        else:
            y += hh

        break

print(y)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))	
if sum(A) % N == 0:
    print(int(sum(A)/N))
else:
    print(0)

Submission status: Wrong Answer

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

if left == s/2:
    print(0)
else:
    print(abs(left-right))

Submission status: Wrong Answer

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

all_length = sum(A)
sum_length = 0
num = 0

for i in range(N):
    sum_length += A[i]
    if sum_length >= all_length//2:
        num = i + 1
        break

half_l = [A[i] for i in range(num)]
sum_half_l = sum(half_l)
rest = all_length - sum_half_l

need = abs(sum_half_l - rest)
print(need)

Submission status: Wrong Answer

Solution code:
N=int(input())
l=list(map(int,input().split()))
ans=sum(l)-1
x,y=0,ans+1
if N==2:
  ans=abs(x-y)
else:
  for i in range(N//2+2):
    x+=l[i]
    y-=l[i]
    ans=min(abs(x-y),ans)
print(ans)

Submission status: Wrong Answer

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


S = sum(A)
mp = S/2

i = 1

while True:
    L = A[0:i]
    R = A[i:N]
    SL = sum(L)
    SR = sum(R)
    # print(L, end = "\t\t")
    # print(R)
    if SL >= SR:
        break
    i += 1

print(abs(SL - SR))

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
ans1=abs(cnt-sum(A[i:]))
if i+1<n:
    ans2=abs(cnt+A[i]-sum(A[i+1:]))
    ans3=float('inf')
elif i-1>0:
    ans3=abs(cnt-A[i-1]-sum(A[i-1:]))
    ans2=float('inf')
else:
    ans2=float('inf')
    ans3=float('inf')

print(min(ans1,ans2,ans3))

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N = int(input())
    A = list(map(int,input().split()))
    p =list(accumulate(A))
    hp = p[-1]/2
    l1 = bisect.bisect_left(p,hp)
    r1 = bisect.bisect_right(p,hp)
    print(min(int(abs(2*(p[l1]-hp))),int(abs(2*(hp-p[r1])))))
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

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

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

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

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

    if A_inf == A_sup:
        return 0
    x = sum(A) - 2*A_inf
    y = 2*A_sup - sum(A)
    result = min(x,y)
    return result

print(func(A))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_b


def find_cost_cutting(n, a):
    s = sum(a)

    first_half_sum = 0
    i = 0
    while first_half_sum < s // 2:
        first_half_sum += a[i]
        i += 1

    if first_half_sum + first_half_sum == s:
        return 0

    second_half_sum = s - first_half_sum
    if second_half_sum > 0:
        return abs(first_half_sum - second_half_sum)
    else:
        first_half_sum = first_half_sum - a[i-1]
        second_half_sum = s - first_half_sum
        return abs(first_half_sum - second_half_sum)


def solve():
    n = int(input())
    a = list(map(int, input().split()))
    print(find_cost_cutting(n, a))


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
seq = input().split()
seq = [int(l) for l in seq]
#i = 1
#j = n-2
i = 0
j = n-1
right = left = 0 
#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:
n = int(input())
num_list = list(map(int,input().split()))
min_diff = 10 ** 12
sum_list = sum(num_list)
count = 0
for i in range(n-1):
    count += num_list[i]
    diffs = abs(count*2 - sum_list)
    if min_diff < diffs:
        print(min_diff)
        exit()
    else:
        min_diff = diffs
print(min_diff)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
chk=sum(a)
k=chk//2
k1=k+1
k2=k+2
k3=k-1
k4=k-2
x=y=z=0
for i,j in enumerate(a):
    z=i
    x+=j
    if x>=k:
        break
if x==k and chk%2==0:
    print(0)
else:
    print(min(abs(x-k)+abs((chk-x)-k),abs(x-k1)+abs((chk-x)-k1),abs(x-k2)+abs((chk-x)-k2),abs(x-k3)+abs((chk-x)-k3),abs(x-k4)+abs((chk-x)-k4)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
if N % 2 == 0:
  print(abs(sum(A[: N // 2]) - sum(A[N // 2 :])))
elif abs(sum(A[: N // 2]) - sum(A[N // 2 :])) > abs(sum(A[: N // 2 + 1]) - sum(A[N // 2 + 1 :])):
  print(abs(sum(A[: N // 2 + 1]) - sum(A[N // 2 + 1 :])))
else:
  print(abs(sum(A[: N // 2]) - sum(A[N // 2 :])))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from bisect import bisect_left


n = int(input())
a = list(map(int, input().split()))
a_acc = list(accumulate(a))
if a_acc[-1] % 2 == 0 and a_acc[-1] // 2 in a_acc:
    print(0)
else:
    b_acc = [0] * len(a_acc)
    for i in range(len(a_acc)):
        b_acc[i] = 2 * a_acc[i]
    idx = bisect_left(b_acc, a_acc[-1] / 2)
    print(min(abs(a_acc[-1] - b_acc[idx-1]),
              abs(a_acc[-1] - b_acc[idx])))

Submission status: Wrong Answer

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

all_length = sum(A)
sum_length = 0
num = 0

for i in range(N):
    sum_length += A[i]
    if sum_length >= all_length//2:
        num = i + 1
        break

half_l = [A[i] for i in range(num)]
sum_half_l = sum(half_l)
rest = all_length - sum_half_l

need = sum_half_l - rest
print(need)

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
if sum(a)%2==1:
    a[0]=a[0]+1
    ans+=1
b = list(accumulate(a))
half = sum(a)//2
ans += abs(half - b[bisect.bisect_left(b,half)])
print(ans)

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]
tmp = math.ceil(half_A - A[0])
if tmp >= 0:
  min = tmp
elif tmp < 0 and half_A > 1:
  min = -tmp
else:
  pass
  
min = abs(min)
for i in range(1, n-1):
  tmp = math.ceil(half_A - A[i])
  if tmp >= 0 and tmp < min:
    min = tmp
  elif tmp < 0 and half_A > (i+1) and -tmp < min:
    min = -tmp
  else:
    pass
print(min)

Submission status: Wrong Answer

Solution code:
import numpy as np

# A = input()
# A = int(input())
# A = map(int, input().split())
# A = list(map(int, input().split()))
# A = [int(input()) for i in range(N)]
#
# c = collections.Counter()

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

ans = 0
bou_len = sum(A) // 2
Q = abs(np.cumsum(A) - bou_len)
print(min(Q) + (0 if N % 2 == 0 else 1))

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]
    delta = abs(sum(F) - all_sum/2)
    if delta <= mini:
        mini = int(delta*2)

print(mini)

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
    
z=sum(a)-c

print(c*2-s)

Submission status: Wrong Answer

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

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

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

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

    if A_inf == A_sup:
        return 0
    x = sum(A) - 2*A_sup
    y = 2*A_sup - sum(A)
    result = min(x,y)
    return result

print(func(A))