Problem:


Score: 200 points

Problem Statement

Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters.

Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:

Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).

Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.

Find the minimum amount of money needed before cutting the bar into two parts with the same length.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.

Sample Input 1

3
2 4 3

Sample Output 1

3

The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:

Shrink the second section from the left. The lengths of the sections are now [2, 3, 3].

Shrink the first section from the left. The lengths of the sections are now [1, 3, 3].

Shrink the second section from the left. The lengths of the sections are now [1, 2, 3], and we can cut the bar at the second notch from the left into two parts of length 3 each.

Sample Input 2

12
100 104 102 105 103 103 101 105 104 102 104 101

Sample Output 2

0



Sample input:
3
2 4 3


Sample output:
3

Submission status: Wrong Answer

Solution code:
N = input()
N = int(N)

A = input().split(' ')
A = [int(a) for a in A]

cut_left = []
cut_right = []

# 長さ
long = sum(A)
cut_long = 0
i=0
cut_items = 0

kakou = 0
diff = 0

for i in range(len(A)):

    if sum(cut_left) < sum(A) and i < len(A) - 1:
        # print(A[i])
        cut_left.append(A[i])
        A[i] = 0

        if sum(cut_left) == sum(A):
            diff = 0
        elif sum(cut_left) > sum(A):
            diff = sum(cut_left) - sum(A)
        else:
            diff = abs(sum(cut_left) - sum(A))

print(diff)

            # print(sum(cut_left) ,diff)


            # diff = sum(cut_left) - long / 2
            #
            # if sum([ max(cut - 1,0) for cut in cut_left if cut >= 2 ]) >= diff * 2:
            #     kakou = diff * 2
            #     break
            #
            # else:
            #     kakou = diff * 2

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))
 
calculator = 0
pluc_index = 0
minus_index = -1
for i in range(N):
    if calculator < A_list[pluc_index]:
        calculator += A_list[pluc_index]
        pluc_index += 1
    else:
        calculator -= A_list[minus_index]
        minus_index -= 1
print(calculator)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
l = sum(a)
l2 = l / 2
tmp = 0
err = [l,l]
errindex = [0,0]
for i in range(n):
    tmp += a[i]
    if err[0] > l2 - tmp and l2 - tmp >= 0:
        err[0] = l2 - tmp
    if err[1] < tmp - l2 and l2 - tmp <= 0:
        err[1] = tmp - l2
ans = min(err)
if ans != int(ans):
    ans += 1
print(int(ans))

Submission status: Wrong Answer

Solution code:
num_of_sections = int(input())
section_lengths = input().split()
section_list = []
cost_list = []
for each in section_lengths:
    section_list.append(int(each))

def split_list_1(a_list):
    half = len(a_list)//2
    return a_list[:half], a_list[half:]

def split_list_2(a_list):
    half = len(a_list)//2
    return a_list[:half+1], a_list[half-1:]

first_half, second_half = split_list_1(section_list)
cost_list.append(abs(sum(first_half) - sum(second_half)))

first_half, second_half = split_list_2(section_list)
cost_list.append(abs(sum(first_half) - sum(second_half)))
print(min(cost_list))

Submission status: Wrong Answer

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

mini = 2020202020 * 2000000

all_sum = sum(A_list)

j = int(N/2+1)
distance = j
is_search = True

while is_search:


    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum
    delta = B_sum - F_sum
    abs_delta = abs(B_sum - F_sum)
    if abs_delta <= mini:
        mini = abs_delta

    distance /= 2 

    if delta > 0:
        j += int(distance+1)

    elif delta < 0:
        j -= int(distance)
    else:
        mini = 0
        is_search = False

    if int(distance) == 0 :
        is_search = False
        if abs_delta <= mini:
            mini = abs_delta

Submission status: Wrong Answer

Solution code:
N = input()
N = int(N)

A = input().split(' ')
A = [int(a) for a in A]

cut_left = []
cut_right = []

# 長さ
long = sum(A)
cut_long = 0
i=0
cut_items = 0

kakou = 0
diff = 0

for i in range(len(A)):

    if sum(cut_left) < sum(A):
        # print(A[i])
        cut_left.append(A[i])
        A[i] = 0

        if sum(cut_left) == sum(A):
            print('0')
            break
        if sum(cut_left) > sum(A):
            diff = sum(cut_left) - sum(A)
            print(diff)

            # print(sum(cut_left) ,diff)


            # diff = sum(cut_left) - long / 2
            #
            # if sum([ max(cut - 1,0) for cut in cut_left if cut >= 2 ]) >= diff * 2:
            #     kakou = diff * 2
            #     break
            #
            # else:
            #     kakou = diff * 2

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ディスカバリーチャンネルコードコンテスト2020 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
s = sum(a)
l = list(accumulate(a))
tmp = s//2
m = inf
for i in range(n):
    if m > abs(tmp - l[i]):
        m = abs(tmp - l[i])
if s % 2:
    m += 1
print(m)

Submission status: Wrong Answer

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

s = sum(A)
i = 0
half = s / 2
neibor = 0
for i in range(n):
	if neibor >= half:
		break
	else:
		neibor += A[i]
aneibor = neibor - A[i-1]
if s-aneibor*2 < abs(neibor-(s-neibor)):
	print(s-aneibor*2)
else:
	print(abs(neibor-(s-neibor)))

Submission status: Wrong Answer

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

s = sum(A)
i = 0
half = s / 2
neibor = 0
for i in range(n-1):
	if neibor >= half:
		break
	else:
		neibor += A[i]
if i == 1:
	aneibor = 0
else:
	aneibor = neibor - A[i]
if s-aneibor*2 < abs(neibor-(s-neibor)):
	print(s-aneibor*2)
else:
	print(abs(neibor-(s-neibor)))

Submission status: Wrong Answer

Solution code:
import numpy
N=int(input())
A=list(map(int,input().split()))
Ap=numpy.cumsum(A)
A2=[abs(sum(A)-n*2) for n in Ap]
ans=0
x=A2.index(min(A2))+1
if sum(A[:x])<sum(A)//2:
  mi=sum(A[:x])
  pl=sum(A)-2*mi
else:
  pl=sum(A[:x])
  mi=2*pl-sum(A)
  while mi<pl:
    mi+=1
    ans+=1
    if mi==pl:
      break
print(ans)

Submission status: Wrong Answer

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

ans = sum(a)
left = a[0]
right = sum(a[1:])

for i in range(1, n-1):
    left += a[i]
    right -= a[i]
    tmp = abs(left - right)
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
  l = int(input())
  arr = map(int, input().split())
  invalid_input = False
  if(l < 2 or l > 200000):
    invalid_input = True
  for i in arr:
    if(i < 1 or i > 2020202020):
      invalid_input = True

  
  if(invalid_input):
    first = 0
    second = 0
    if(l % 2 == 0):
      for i in range(0, l/2):
        first = first + i
      for i in range(l/2, l):
        second = second + i
      print(abs(first - second))
    if(l % 2 == 1):
      for i in range(0, (l - 1)/2):
        first = first + i
      for i in range((l + 1)/2, l):
        second = second + i
      print(abs(abs(first -second) - arr((l - 1)/2)))

Submission status: Wrong Answer

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

mini = 2020202020 * 2000000

all_sum = sum(A_list)

j = int(N/2+1)
distance = j
is_search = True

while is_search:


    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum
    delta = B_sum - F_sum
    abs_delta = abs(B_sum - F_sum)
    if abs_delta <= mini:
        mini = abs_delta

    distance /= 2 

    if delta > 0:
        j += int(distance)

    elif delta < 0:
        j -= int(distance)
    else:
        mini = 0
        is_search = False

    if int(distance) == 0 :
        is_search = False
        if abs_delta <= mini:
            mini = abs_delta

print(mini)

Submission status: Wrong Answer

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

min_aaa =10000000
cnt =0
cnt_b=0

sum_list_aaa =list(accumulate(list_aaa))
judge_aaa = sum_list_aaa[-1]/2

# +する場合
for i in range(min_aaa):
    if judge_aaa %2 ==0:
        if judge_aaa in sum_list_aaa:
            break
        else:
            cnt+=1
            judge_aaa +=1
    else:
        sum_list_aaa[-1] +=1
        judge_aaa = sum_list_aaa[-1]/2
        cnt+=1


sum_list_aaa =list(accumulate(list_aaa))
judge_aaa = math.ceil(sum_list_aaa[-1]/2)
# -する場合
for i in range(min_aaa):
    if judge_aaa %2 ==0:
        if judge_aaa in sum_list_aaa:
            break
        else:
            cnt_b+=1
            judge_aaa -=1
    else:
        sum_list_aaa[-1] +=1
        judge_aaa = sum_list_aaa[-1]/2
        cnt_b+=1


print(min(cnt,cnt_b))

Submission status: Wrong Answer

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

a_sum = sum(a)
c = 0
for i in a:
    if c >= a_sum/2:
        break
    else:
        c += i

print(int(abs(a_sum/2-c)*2))

Submission status: Wrong Answer

Solution code:

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

mini = 2020202020 * 2000000

all_sum = sum(A_list)

j = int(N/2+1)
distance = j
is_search = True

while is_search:


    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum
    delta = B_sum - F_sum
    abs_delta = abs(B_sum - F_sum)
    if abs_delta <= mini:
        mini = abs_delta

    distance /= 2 

    if delta > 0:
        j += int(distance)

    elif delta < 0:
        j -= int(distance)
    else:
        mini = 0
        is_search = False
    if int(distance) == 0 :
        is_search = False
        mini = abs_delta

print(mini)

Submission status: Wrong Answer

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

cur, prv = 0, 0
sum_a = sum(a)
mid = sum_a//2
cost = 0
if sum_a%2 != 0:
    cost += 1
    mid += 1
for i in range(n):
    cur += a[i]
    if abs(cur-mid) >= abs(prv-mid):
        break
    prv = cur

obj = prv
cost += abs(obj-mid)*2
print(cost)

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
  
# 出力
half1 = length - left
half2 = half1 + len_list[index-1]
diff = half1 if half2 > half1 else half2
print(diff)

Submission status: Wrong Answer

Solution code:
n =int(input())
a=list(map(int, input().split()))
m=0
s=[2020202020]
for i in range(len(a)):
    m+=a[i]
    mins=min(s)
    s.append(abs(sum(a)-m*2))
    if mins<s[i]:
        break
print(mins)

Submission status: Wrong Answer

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

Asum = sum(A)

s=0
t=0
for i,Ai in enumerate(A):
    s+=Ai
    if s == Asum/2:
        print(0)
        exit()
    if s >= Asum/2:
        s = i
        break
for i,Ai in enumerate(A[::-1]):
    t+=Ai
    if t >= Asum/2:
        t = i
        break

ans1 = abs(sum(A[:s])-(Asum-sum(A[:s])))
ans2 = abs(sum(A[:t])-(Asum-sum(A[:t])))

print(min(ans1,ans2))

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:
        ans = abs(cnt - (l-cnt))
        break
        
        
print(int(ans))

Submission status: Wrong Answer

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

ans = sum(a)
left = a[0]
right = sum(a[1:])

for i in range(1, n):
    left += a[i]
    right -= a[i]
    tmp = abs(left - right)
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:

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

mini = 2020202020 * 2000000

all_sum = sum(A_list)

j = int(N/2+1)
distance = j
is_search = True

while is_search:
    if int(distance) == 0 :
        is_search = False

    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum
    delta = B_sum - F_sum
    abs_delta = abs(B_sum - F_sum)
    if abs_delta <= mini:
        mini = abs_delta

    distance /= 2 

    if delta > 0:
        j += int(distance)

    elif delta < 0:
        j -= int(distance)
    else:
        mini = 0
        is_search = False

print(mini)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
Alist = np.array(list(map(int, input().split())))
cs = np.cumsum(Alist)
sm = np.sum(Alist)
if (sm // 2) in cs:
    print(0)
else:
    print(np.min(np.abs((cs[:-1] - sm))))

Submission status: Wrong Answer

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

Asum = sum(A)

s=0
t=0
for i,Ai in enumerate(A):
    s+=Ai
    if s == Asum/2:
        print(0)
        exit()
    if s >= Asum/2:
        s = i
        break
for i,Ai in enumerate(A[::-1]):
    t+=Ai
    if t >= Asum/2:
        t = i
        break

ans1 = sum(A[:s])-(Asum-sum(A[:s]))
ans2 = sum(A[:t])-(Asum-sum(A[:t]))

print(min(ans1,ans2))

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
    if i > 1:
        i += 1
    if j >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:
from itertools import accumulate
from bisect import bisect_left, bisect_right
n = int(input())
a = list(map(int, input().split()))

a = list(accumulate(a))
cost = 0

if a[-1]%2 != 0:
    mid = (a[-1]+1)//2
    cost += 1
else:
    mid = a[-1]//2
l = bisect_left(a, mid)
r = bisect_right(a, mid)

sma = a[l]
lar = a[-1]-a[l]
print(abs(lar-sma))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = [int(item) for item in input().split()]
A_max = max(A_list)
mini = 2020202020 * 2000000

all_sum = sum(A_list)

j = int(N/2)
distance = j
is_search = True

while is_search:
    if int(distance) == 0 :
        is_search = False

    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum
    delta = B_sum - F_sum
    abs_delta = abs(B_sum - F_sum)
    if abs_delta <= mini:
        mini = abs_delta

    distance /= 2 

    if delta > 0:
        j += int(distance)

    elif delta < 0:
        j -= int(distance)
    else:
        mini = 0
        is_search = False

print(mini)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
Alist = np.array(list(map(int, input().split())))
cs = np.cumsum(Alist)
sm = np.sum(Alist)
if (sm // 2) in cs:
    print(0)
else:
    print(np.min(np.abs((cs - sm)[:-1])))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = [int(item) for item in input().split()]
A_max = max(A_list)
mini = 2020202020 * 2000000

all_sum = sum(A_list)

j = int(N/2+1)
distance = j
is_search = True

while is_search:
    if int(distance) == 0 :
        is_search = False

    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum
    delta = B_sum - F_sum
    abs_delta = abs(B_sum - F_sum)
    if abs_delta <= mini:
        mini = abs_delta

    distance /= 2 

    if delta > 0:
        j += int(distance+1)

    elif delta < 0:
        j -= int(distance)
    else:
        mini = 0
        is_search = False

print(mini)

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:
        ans = abs(cnt - (l-cnt))
        break
        
        
print(int(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy
from bisect import bisect_left

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

res = numpy.cumsum(A)
m = res[N-1]
ans = 0

if m % 2 == 0:
    l = m / 2
    b = bisect_left(res, l)
    if b == l:
        print(ans)
    else:
        x = l - res[b-1]
        y = res[b] - l
        if x <= y:
            ans = x * 2
            print(ans)
        else:
            ans = y * 2
            print(ans)

else:
    l1 = (m-1) / 2
    l2 = (m+1) / 2
    b1 = bisect_left(res, l1)
    c1 = b1 + 1
    b2 = bisect_left(res, l2)
    c2 = b2 + 1
    B1 = l1 - res[b1-1]
    C1 = res[c1-1] - l1
    B2 = l2 - res[b2-1]
    C2 = res[c2-1] - l2
    d = [B1, C1, B2, C2]
    d.sort()
    ans = d[0]*2 + 1
    print(ans)

Submission status: Wrong Answer

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

lp, rp = 0, len(A)-1
lb, rb = -1, -1

l, r = 0, 0
while lp < rp:
    if lp != lb:
        l += A[lp]
    if rp != rb:
        r += A[rp]
    #print(lp, l, rp, r)

    lb, rb = lp, rp
    if l == r:
        lp += 1
        rp -= 1
    elif l < r:
        lp += 1
    else:
        rp -= 1

print(abs(l-r))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(x) for x in input().split()]
sum_s = sum(s)
middle_s = sum_s / 2
w = 0
for i in range(len(s)):
    w += s[i]
    if w == middle_s:
        print(0)
        break
    elif w > middle_s:
        if i == 0:
            ans = s[0] - sum(s[1:])
            print(ans)
        elif s[i] == s[-1]:
            ans = s[-1] - sum(s[:-1])
            print(ans)
        else:
            left = sum(s[0:(i+1)]) - sum(s[(i+1):])
            right = sum(s[i:]) - sum(s[0:i])
            ans = min(abs(left),abs(right))
            print(ans)
        break

Submission status: Wrong Answer

Solution code:
N = input()
N = int(N)

A = input().split(' ')
A = [int(a) for a in A]

# 長さ
long = sum(A)
cut_long = 0
i=0
cut_items = 0

for i in range(len(A)):

    if cut_long < long / 2:
        cut_long += A[i]

        if cut_long >= long / 2:
            diff = cut_long - long / 2
            break
print(int(diff / 0.5))

Submission status: Wrong Answer

Solution code:
n = int(input())
a= [int(x) for x in input().split(" ",n-1)]
print(a)
s = 0
for x in range(n):
    s += a[x-1]
r = 0
l = 0
x = 0
while r < s/2:
    r += a[x]
    x += 1
y = 1
while l < s/2:
    l += a[n-y]
    y += 1
if r == s//2:
    print(0)
else:
    k = min(r,l)  
    v = k - s/2
    m = v // 0.5
    print(int(m))

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)
    b = mx - b
    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()))

lp, rp = 0, len(A)-1
lb, rb = -1, -1

l, r = 0, 0
while lp < rp:
    if lp != lb:
        l += A[lp]
    if rp != rb:
        r += A[rp]
    print(lp, l, rp, r)

    lb, rb = lp, rp
    if l == r:
        lp += 1
        rp -= 1
    elif l < r:
        lp += 1
    else:
        rp -= 1

print(abs(l-r))

Submission status: Wrong Answer

Solution code:
n =int(input())
a=list(map(int, input().split()))
m=0
s=[2020202020]
for i in range(len(a)):
    m+=a[i]
    mins=min(s)
    s.append(abs(sum(a)-m*2))
    if mins<=s[i]:
        break
print(min(s))

Submission status: Wrong Answer

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

s = sum(A)
i = 0
half = s / 2
neibor = 0
for i in range(n-1):
	if neibor >= half:
		break
	else:
		neibor += A[i]
aneibor = neibor - A[i]
if s-aneibor*2 < abs(neibor-(s-neibor)):
	print(s-aneibor*2)
else:
	print(abs(neibor-(s-neibor)))

Submission status: Wrong Answer

Solution code:
N=int(input())  #数値入力 「N」だけの入力のとき
A=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

# from functools import lru_cache
# @lru_cache(maxsize=100000)

# 一度差を出してから，どんどんi-1, i+1の和で縮めていく．


def check_distance():
    min = 100000000000000
    left = A[0]
    right = sum(A[1:])

    for i in range(1, N-1):
        left += A[i]
        right -= A[i]
        tmp_a = abs(left - right)
        if min > tmp_a:
            min = tmp_a
        else:
            return min
    return min
print(check_distance())

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = sum(a)
s = 0
cnt = 0
while s < l/2:
  s += a[cnt]
  cnt += 1
if s == l/2:
  print(0)
elif(s - a[cnt-1] == l/2):
  print(0)
else:
  print(2*s - l)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):
    cnt += a[i]
    if cnt >= L / 2:
        break

for i in range(n):
    if cnt_2 >= L / 2:
        cnt_2 -= a[i-1]
        break
    else:
        cnt_2 += a[i]

if cnt == L / 2:
    ans = 0
else:
    ans = abs(L - cnt * 2)

if cnt_2 == L / 2:
    ans_2 = 0
else:
    ans_2 = abs(L - cnt_2 * 2)

print(min(ans, ans_2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

base=0
sum=0
count=0

for i in range(N):
    sum+=A[i]
while base<sum//2:
    base+=A.pop(0)

if base==sum//2:
         print(count)
else:
    count+=abs(base-(sum-base))
    print(count)

Submission status: Wrong Answer

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

cur, prv = 0, 0
sum_a = sum(a)
mid = sum_a//2
cost = 0
if sum_a%2 != 0:
    cost += 1
for i in range(n):
    cur += a[i]
    if abs(cur-mid) > abs(prv-mid):
        break
    prv = cur

obj = prv
cost += abs(obj-mid)*2
print(cost)

Submission status: Wrong Answer

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

cur, prv = 0, 0
sum_a = sum(a)
mid = sum_a//2
cost = 0
if sum_a%2 != 0:
    cost += 1
    mid -= 1
for i in range(n):
    cur += a[i]
    if abs(cur-mid) > abs(prv-mid):
        break
    prv = cur

obj = prv
cost += abs(obj-mid)*2
print(cost)

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
    elif A_tmp == A_1:
        print(0)
        exit()
    else:
        A_tmp = A_tmp-A[i]
        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()))

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

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(max(0, s//2 - i), max(0, j - s//2))
    res += 2 * tmp

print(res)

Submission status: Wrong Answer

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

def getSum(s, e):
    sum = 0
    for i in (s, e):
      sum = sum + list[i]
    return sum

prev = 0
for i in range(0,n-1):
    left = list[:i+1]
    right = list[i+1:]
    diff = abs(getSum(0, i) - getSum(i+1, len(list)-1))
    if(i == 0):
        prev = diff
    else:
        if diff < prev:
            prev = diff
    if diff == 0:
        print(prev)
        exit()
print(prev)

Submission status: Wrong Answer

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

cur, prv = 0, 0
sum_a = sum(a)
mid = sum_a//2
cost = 0
if sum_a%2 != 0:
    cost += 1
    mid += 1
for i in range(n):
    cur += a[i]
    if abs(cur-mid) > abs(prv-mid):
        break
    prv = cur

obj = prv
cost += abs(obj-mid)*2
print(cost)

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
d = s-c+a[i-1]

if c<d:
  print(c*2-s)
else:
  print(d*2-s)

Submission status: Wrong Answer

Solution code:
N=int(input())  #数値入力 「N」だけの入力のとき
A=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

# from functools import lru_cache
# @lru_cache(maxsize=100000)

# 一度差を出してから，どんどんi-1, i+1の和で縮めていく．

def check_distance():
    ret = []
    left = A[0]
    right = sum(A[1:])

    for i in range(1, N):
        left += A[i]
        right -= A[i]
        tmp_a = abs(left - right)
        ret.append(tmp_a)
    return min(ret)

print(check_distance())

Submission status: Wrong Answer

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

#print(A)

#print(int(sum(A)/2))

l = 0
for i in range(N):
  l = l + A[i]
  if l >= int(sum(A)/2):
    #print(i)
    break
print(abs(int(sum(A[i+1:N])) - int(l)))

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

A = 0
count = 0
for i in Nlist:
    A += i
    if A >= H_S:
        break
    count += 1
    
D = S - A    

if S % 2 == 0:
    if A == H_S:
        print(0)
    else:
        if abs(H_S - A) > abs(H_S - D):
            print(abs(H_S - D))
        else:
            print(abs(H_S - A))
    
if S % 2 != 0: 
    if A == H_S:
        print(0)
    else:
        if abs(H_S - A) > abs(H_S - (A - Nlist[count])):
            print(abs(H_S - (A - Nlist[count])))
        else:
            print(abs(H_S - A) + 1)


#    print(abs(H_S - D + 1))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
r = 0
n = N//2
s = sum(A)
 
a = s/2
t = a
cnt = 0
 
for i in range(len(A)):
    if t >= A[i]:
        t = t - A[i]
        cnt = cnt + 1
    else:
        break
 
if (N-cnt)%2 == 1:
    if (s - sum(A[0:cnt+1])) < (s - sum(A[cnt:N])):
        r = (sum(A[cnt:N])) - (sum(A[0:cnt]))
 
    elif (s - sum(A[0:cnt+1])) > (s - sum(A[cnt:N])):
        r = (sum(A[0:cnt+1])) - (sum(A[cnt + 1:N]))
    else:
        pass
 
else:
    if (s - sum(A[0:cnt])) < (s - sum(A[cnt:N])):
        r = (s - sum(A[cnt:N])) - (s - sum(A[0:cnt]))
 
    elif (s - sum(A[0:cnt])) > (s - sum(A[cnt:N])):
        r = (s - sum(A[0:cnt])) - (s - sum(A[cnt:N]))
 
    else:
        pass
 
print(r)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
# A = input().split()
A = list(map(int, input().split()))
# f = open("tmp.text")
# A = list(map(int, f.read().split()))

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

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

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

print(tmp)



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

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

# print(abs(left-right))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(x) for x in input().split(" "))
center = sum(a) / 2
print(center)

sum = 0
diff = float('inf')
for x in a:
    if abs(sum + x - center) > diff:
        break
    sum += x
    diff = abs(sum - center)

print(int(diff * 2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
cost_1 = 0
cost_2 = 0
cost_3 = 0
cost_4 = 0
S = sum(A)
S_1 = sum(A)
S_2 = sum(A)
S_3 = sum(A)
S_4 = sum(A)
half = S / 2
l = 0
i = 0
while l < half:
    l += A[i]
    i += 1
l_big = l
l_small = l - A[i-1]
if l_big == S - l:
    print(0)
elif l_big - half < half - l_small:
    l_1 = l_big
    l_2 = l_big
    while l_1 > S_1 - l:
        l_1 -= 1
        cost_1 += 1
    while l > S_2 - l_2:
        l_2 -= 1
        cost_2 += 1
    print(min(cost_1, cost_2))
else:
    l_3 = l_small
    l_4 = l_small
    while l_3 < S_3 - l_3:
        l_3 += 1
        cost_3 += 1
    while l_4 < S_4 - l_4:
        l_4 += 1
        cost_4 += 1
    print(min(cost_3, cost_4))

Submission status: Wrong Answer

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

if sum(A[:1]) > sum(A)//2:
  print(abs(sum(A[1:])-sum(A[:1])))
  sys.exit()
if sum(A[-1:]) > sum(A)//2:
  print(abs(sum(A[:1])-sum(A[1:])))
  sys.exit()

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


a = abs(sum(A[:sep])-sum(A[sep:]))
b = abs(sum(A[:sep+1])-sum(A[sep+1:]))
c = abs(sum(A[:sep-1])-sum(A[sep-1:]))
print(min(a,b,c))

Submission status: Wrong Answer

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

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

print(abs(neibor-(s-neibor)))