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:
from itertools import accumulate

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

acc = list(accumulate(a))
ans = 10**9

mx = acc.pop()
for b in acc:
    if abs(mx - b - b) < ans:
        ans = abs(mx - 2*b)
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
a = list(map(int, input().split()))
mindif = 1e9
minidx=0
for i in range(1,N):
    dif = np.abs(sum(a[:i])-sum(a[i:]))
    if dif<mindif:
        mindif = dif
        minidx = i
print(mindif)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
i=ans1=0
while True:
    if ans1<x/2 and i<n:
        ans1+=a[i]
        i+=1
    else:
        break
if i==n-1:
    print(2*a[i-1]-x)
else:
    i-=1
    ans2=x-ans1
    ans1-=a[i]
    print(a[i]-abs(ans1-ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
cntL = 0
s = sum(a) // 2

for i in range(len(a)):
    if cntL + a[i] < s:
        cntL += a[i]
    else:
        cntL += a[i]
        break
# print(cntL)
print(abs((sum(a) - cntL) - cntL))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from itertools import accumulate

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

prefix = list(accumulate(A))
total = prefix[-1]
seq = bisect_left(prefix, total/2)
alt = prefix[seq]
print(abs(total - 2*alt))

Submission status: Wrong Answer

Solution code:
n=int(input())

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

#print(r)

temp=sum(r)

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

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

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

ans = min(anslsit)
if N%2 == 1:
    ans = ans * 2
print(int(ans))

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]
#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:
        print(2* check_sum - sum)
        exit()

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_left
s = bisect_left(A.cumsum(), A.sum()/2)

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

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):
        o=l
        l+=A[i]
        if l>=M:
            break
    if (l-M) > (M-o):
        l=o
    print(int((l-M)*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()))

A_len = sum(A)

tyuuou = float(A_len) / 2


index = 0
s = 0
flag = 0 # 0 left, 1 right
f = 0
nokori = 0
for i in range(N-1):
    s += A[i]
    if s >= tyuuou:
        index = i
        nokori = A_len - s
        if s-A[i] > nokori:
            index -= 1
            s -= A[i]
            nokori += A[i]
        f = 1
        break
if f==0:
    index = N-2


print(abs(nokori-s))

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(map(int, input().split()))
a = 0
i = 0
while a < sum(li)//2:
  a += li[i]
  i += 1
b = 0
for j in range(i, n):
  b += li[j]
print(a - b)

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(int((n+1)/2)):
  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())
S=list(map(int, input().split()))
Sum=sum(S)
#print(Sum)
wa=0
count=0
for i in range(n):
    if wa<Sum/2:
        wa=wa+S[i]
        count=count+1
if count==n:
    wa=wa-S[n-1]
        #print(wa,Sum-wa)
print(int(abs(Sum-2*wa)))

Submission status: Wrong Answer

Solution code:
import bisect 
n = int(input())
a = list(map(int, input().split()))
com_a = [0 for _ in range(n)]
com_a[0] = a[0]
for i in range(1, n):
  com_a[i] = a[i] + com_a[i-1]

sum_a = sum(a)
middle = bisect.bisect_left(com_a, sum_a/2)
left = sum(a[:middle+1])
right = sum(a[middle+1:])
print(left-right)

Submission status: Wrong Answer

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


n=int(input())
l=list(map(int,input().split()))
m=[l[0]]
for i in range(1,n):
    m.append(m[-1]+l[i])
check=0
if m[-1]%2==1:
    m[-1]+=1
    check=1
sa=m[n-1]
base=m[-1]/2
for k in range(n):
    if abs(base-m[k])<sa:
        sa=abs(base-m[k])
print(int(sa*2+check))

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
a = list(map(int, input().split()))
b=[]
for i in a:
    if i!=1:
        b.append(i-1)

a =b
mindif = 1e9
minidx=0
for i in range(1,N):
    dif = np.abs(sum(a[:i])-sum(a[i:]))
    if dif<mindif:
        mindif = dif
        minidx = i
print(minidx)

Submission status: Wrong Answer

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

bou_half = sum(List)/2

bou_sum = 0
for i in List:
    bou_sum += i
    if bou_sum == bou_half:
        print(0)
        exit()
    if bou_sum > bou_half:
        if bou_sum - bou_half < bou_half - bou_sum -i: # 中心がより
            print(int((bou_half - bou_sum -i) // 0.5))
            exit()
        else:
            print(int((bou_sum - bou_half) // 0.5))
            exit()

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

a = sum(A)
b = a//2

s = 0
for i in range(N):
  s+=A[i]
  if b<=s:
    break

t = a - s
if s>=t:
  ans = s-t
  print(ans)
  
else:
  ans = t-s
  print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_l = list(map(float,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(int(ans))

Submission status: Wrong Answer

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

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

cnt=0
sum_a=sum(a)

half_sum_a_1=(sum_a-1)//2
tmp_sum_1=0
for i in range(n):
    tmp_sum_1+=a[i]
    if tmp_sum_1>=half_sum_a_1:
        break
nokori_1=sum_a-tmp_sum_1
ans1=abs(tmp_sum_1-nokori_1)
half_sum_a_2=(sum_a+1)//2
tmp_sum_2=0
for i in range(n):
    tmp_sum_2+=a[i]
    if tmp_sum_2>=half_sum_a_2:
        break
nokori_2=sum_a-tmp_sum_2
ans2=abs(tmp_sum_2-nokori_2)

print(min(ans1,ans2))

Submission status: Wrong Answer

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

sumA = sum(A)/2

i = 0
j = 0

while i < sumA:
    i = i+A[j]
    j += 1

ans = abs(sum(A[0:j]) - sum(A[j:N]))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect
#input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
cumsum = np.cumsum(A)
sumA1 = sum(A)
sumA2 = sumA1
ans = 0
if sumA1%2 != 0:
    sumA1 -= 1
    sumA2 += 1
    ans += 1
for i in range(0,N,2):
    id1 = bisect.bisect_left(cumsum,sumA1-i)
    if cumsum[id1] == sumA1-i:
        print(ans+i)
        exit()
    id2 = bisect.bisect_left(cumsum,sumA2-i)
    if id2 < len(cumsum) and cumsum[id2] == sumA2-i:
        print(ans+i)
        exit()

Submission status: Wrong Answer

Solution code:
def Solve (N, A):
    l = []
    for i in range(1, N//2+1):
        l.append(abs(sum(A[:i]) - sum(A[i:])))
        l.append(abs(sum(A[N-i:]) - sum(A[:N-i])))
    print(l)
    return min(l)

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    start_len = sum(A)
    temp_len = sum(A)
    sousa = 0
    while(1):
        if(temp_len % 2 == 1):
            sousa += 1
            temp_len += 1
        else:
            seikou1 = kazoe(N, A, start_len + sousa, sousa)
            seikou2 = kazoe(N, A, start_len - sousa, sousa)
            if(seikou1 == True or seikou2 == True):
                break
            sousa += 1
    print(sousa)
            
            
def kazoe(N,A,temp_len, sousa):
    if(temp_len == 0):
        return False
    mokuteki = temp_len/2
    temp = 0
    for i in range(N):
        temp += A[i]
        if(temp > mokuteki):
            break
    i1 = i-1
    kouho1 = temp - A[i]
    i2 = i
    kouho2 = temp
    ok_list = [mokuteki]
    for j in range(1,sousa):
        ok_list.append(mokuteki+j)
        ok_list.append(mokuteki-j)
    ok_set = set(ok_list)
    if(kouho1 in ok_set or kouho2 in ok_set):
        return True
    else:
        return False


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
SUM=sum(a)
hlf=SUM//2
cnt=0
res=10**9
for i in range(n):
    cnt+=a[i]
    rst=SUM-cnt
    if cnt==hlf:
        print(0)
        exit()
    res=min(abs(rst-cnt),res)
print(res)

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:#小さいほう
    res=0
    my=True
    b=cnt
    while my:
        b-=1
        s-=1
        res+=1
        if b==s/2:
            my=False
    print(res)
else:
    res=0
    my=True
    b=s-cnt+a[k]
    while my:
        b-=1
        s-=1
        res+=1
        if b==s/2:
            my=False
    print(res)

Submission status: Wrong Answer

Solution code:
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:
import bisect

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

sammary = 0
A_sammary = []
for i in range(N):
    sammary += A[i]
    A_sammary.append(sammary)

center = (sum(A) + 1)  // 2
A_center_index = bisect.bisect_left(A_sammary, center)

if center == A_sammary[A_center_index]:
    print(0)
    exit()
else:
    left = sum(A[0:A_center_index + 1])
    right = sum(A[A_center_index + 1: N])
    out = abs(left - right)

print(out)

"""
3
2 4 3

"""

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
from sys import exit
from bisect import bisect_left

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

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

def f(p, l):
  return abs(p + p - l)

l = a[-1]
i = bisect_left(a, l // 2)
print(min(f(p, l) for p in a[i:i+2]))

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
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    check -=a[idx]
    afte = sum(a[idx:])
    ans = half-check+afte-half
    
    check +=a[idx]
    afte = sum(a[idx+1:])
    ans = min(ans,check-half+abs(afte-half))
    
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 = 99999999
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:
# 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 x if flag else r-1

x = binary_search(A, half)
print(abs(half - x))

Submission status: Wrong Answer

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

s = sum(a)
m = s/2

x = 0
p = 0
for i in range(n):
	if x < m:
		x += a[i]
	else:
		y = i-1
		break

y = s - x	

ans = x - y

print(ans)

Submission status: Wrong Answer

Solution code:
def Solve (N, A):
    l = []
    for i in range(1, N//2+1):
        l.append(abs(sum(A[:i]) - sum(A[i:])))
        l.append(abs(sum(A[i:]) - sum(A[:i])))
    return min(l)

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

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))
#print(ac)

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

cnt = 0
while True:
  m = ac[-1]
  half = m/2
  if int(half) in ac:
    print(cnt)
    break
  else:
    cnt += 1
    near = getNearestValue(ac,half)
    if ac[near]-half > 0:
      if a[near] > 1:
        a[near] -= 1
      else:
        a[near] += 1
    else:
      if a[near] > 1:
        a[near] += 1
      else:
        a[near] -= 1
    ac = list(itertools.accumulate(a))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

INF = float("inf")
IINF = 10 ** 18
MOD = 998244353

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

    lengths = list(accumulate([0] + A))

    # length = sum(A)
    # half = len(A) // 2
    half = lengths[-1] // 2

    # print(lengths, half)

    a1 = 0
    b1 = 0
    a2 = 0
    b2 = 0
    for i in range(1, N):
        if lengths[i] >= half:
            a1 = lengths[i]
            b1 = lengths[N] - lengths[i]
            a2 = lengths[i-1]
            b2 = lengths[N] - lengths[i-1]
            break

    # print(b2, a2)
    # print(b1, a1)
    print(min(abs(b2 - a2), abs(b1 - a1)))




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect

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

sammary = 0
A_sammary = []
for i in range(N):
    sammary += A[i]
    A_sammary.append(sammary)

center = (sum(A) + 1)  // 2
A_center_index = bisect.bisect_left(A_sammary, center)

if center == A_sammary[A_center_index]:
    print(0)
    exit()
else:
    left = sum(A[0:A_center_index + 1])
    right = sum(A[A_center_index + 1: N])
    out = abs(left - right)

print(left)
print(right)
print(out)

"""
3
2 4 3

"""

Submission status: Wrong Answer

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

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

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):
    count += num_list[i]
    diffs = abs(count*2 - sum_list)
    if min_diff < diffs:
        print(min_diff)
        exit()
    else:
        min_diff = diffs

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
    dif = abs(first_half_sum - second_half_sum)
    return dif


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:
from itertools import accumulate

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

acc = [0] + list(accumulate(a))
ans = 10**9

for b in acc:
    if abs(acc[-1] - b - b) < ans:
        ans = abs(acc[-1] - 2*b)
print(ans)

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
diff = sum(data[0:i])-sum(data[i:len(data)])
diff = abs(diff)
print(diff)

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)
    if B_index == N-1:
        B_index -= 1

    ans = sum(A[:B_index+1]) - sum(A[B_index+1:])
    print(abs(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
S=0
ans=100000000
for i in range(n):
  S+=a[i]
  if abs((x-S)-S) < ans:
    ans=abs((x-S)-S)
print(ans)

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-1):
    if S<=T:
        break
    T+=A[i]
    S-=A[i]

print(abs(S-T))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
chk=sum(a)
k=chk//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)
elif chk%2==0:
    print(abs(x-k)+abs((chk-x)-k))
else:
    print(min(abs(x-k)+abs((chk-x)-k),abs(x-k-1)+abs((chk-x)-k-1)))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    *a, = map(int, input().split())
    o = 0
    if n%2==0:
        l, r = sum(a[:n//2]), sum(a[n//2:])
        o = max(l, r) - min(l, r)
    else:
        l1, r1 = sum(a[:n//2]), sum(a[n//2:])
        l2, r2 = sum(a[:n//2+1]), sum(a[n//2+1:])
        o = min(max(l1, r1)-min(l1, r1), max(l2, r2)-min(l2, r2))
    print(o)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

import numpy as np

A=np.array(A)

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

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

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):
        print (min(2 * S - sumh, sumh- 2 * preS))
        # print (2)
        break
    preS = S
    S += A[ii]

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
b=sum(a)
c=0
d=200000
for i in a:
    c+=i
    d=min(d,abs(c-b/2))
print(int(d*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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np
N = int(input())
A = np.array(readline().split(),np.int64)
S = sum(A)
AA = np.cumsum(A)
#print(AA)
if S % 2 == 0 and S // 2 in AA:
  print(0)
  exit()
if N % 2 == 1:
  a = AA[N // 2]
  b = AA[-1] - a
else:
  a = AA[N // 2 - 1]
  b = AA[-1] - a
print(abs(a - b))

Submission status: Wrong Answer

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

a = sum(A)
b = a//2

s = 0
for i in range(N):
  s+=A[i]
  if b<=s:
    break
    
ans = 2*s-a
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
all_sum=sum(A)
tmp=0
for i in A:
  if all_sum//2>tmp:
    tmp+=i
  else:
    break
print(abs(tmp-abs(all_sum-tmp)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from pprint import pprint
import math

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:
            diff_a = s - half
            if diff_a == 0:
                print(0)
                return
            diff_b = half - (s - a_i)
            # print(diff_a, diff_b)
            ans = math.floor(diff_a) + math.floor(diff_b)
            print(ans)
            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:
N = int(input())
import numpy as np
A = np.array(list(map(int,input().split())))
cumsum = np.cumsum(A)
ans = 10**9+7
for i in range(N-1):
    if abs(cumsum[i] - (cumsum[-1] - cumsum[i])) < ans:
        ans = abs(cumsum[i] - (cumsum[-1] - cumsum[i]))
print(ans)
# 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:
# -*- 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 += val
		if tmp >= to:
			manaka = i

	#print(to)
	#print(manaka)

	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:
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):
        print (min(2 * S - sumh, sumh- 2 * preS))
        # print (2)
        break
    preS = S
    S += A[ii]

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
cnt = 0
tgt = sum(A) // 2
for i in range(N):
    cnt += A[i]
    if cnt >= tgt:
        break
        
print(abs(cnt - sum(A[i + 1:])))

Submission status: Wrong Answer

Solution code:
import math

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

zen =list_aaa[0:math.ceil(N/2)]
kou =list_aaa[math.ceil(N/2):]

print(abs(sum(zen)-sum(kou)))

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = tuple(map(int, input().split()))

    n = N // 2
    if N % 2:
        case1 = abs(sum(A[:n]) - sum(A[n:]))
        case2 = abs(sum(A[:n+1]) - sum(A[n+1:]))
        ans = min(case1, case2)
    else:
        ans = abs(sum(A[:n]) - sum(A[n:]))
    print(ans)


main()

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

    second_half_sum = s - first_half_sum
    dif = abs(first_half_sum - second_half_sum)
    return dif


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())
import numpy as np
a=np.array(input().split(),dtype=int)
asum=np.cumsum(a)
half=asum.max()//2
import bisect
p=bisect.bisect_left(asum,half)
choice1=asum[p]-half
choise2=abs(half-(asum.max()-asum[p]))
print(choice1+choise2)

Submission status: Wrong Answer

Solution code:
import sys

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


A_sum=sum(A)
half=A_sum/2
flag=A_sum%2==0
t_half=A
#print(half)
L=0
for i in range(len(A)):
  if flag and L+A[i]==half:
    print(0)
    sys.exit()
  if L+A[i]>half:
    C=A[i]
    R=A_sum-(L+C)
    break
  else:
    L+=A[i]

print(L+C-max(L,R))

Submission status: Wrong Answer

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

ans = 0
for pivot in range(len(A)):
  if sum(A[:pivot]) > sum(A[pivot:]):
    break
ans1 = sum(A[:pivot]) - sum(A[pivot:])
ans2 = sum(A[pivot-1:]) -  sum(A[:pivot-1])

print(min(ans1, ans2))

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 = 999999
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:
n=int(input())

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

#print(r)

temp=sum(r)

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