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: Runtime Error

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

all_sum = sum(A_list)

F_sum_list = []

for j in range(1,N):
    F = A_list[:j]
    F_sum = sum(F)
    F_sum_list.append(F_sum)
    if F_sum >= all_sum // 2:
        break

mini = min(all_sum - 2 * F_sum_list[-2], -1*(all_sum - 2 * F_sum_list[-1]))
print(mini)

Submission status: Runtime Error

Solution code:
n=int(input())
alist=list(map(int,input().split()))
blist=[alist[0]]
for i in range(1,n):
	blist.append(blist[-1]+alist[i]) #累積和
alist.reverse()
clist=[alist[0]]
for i in range(1,n):
	clist.append(clist[-1]+alist[i])
clist.reverse()
dlist=[]
for i in range(n):
	dlist.append(abs(blist[i]-clist[i])
ans=min(dlist)
print(int(ans))

# hanbun=blist[-1]/2
# shirushi=0
# l=0
# u=n-1
# while u>l+1:
	# aida=(l+u)//2
	# if blist[aida]==hanbun:
		# index=aida
		# shirushi=1
		# break
	# elif blist[aida]>hanbun:
		# l=aida
	# elif blist[aida]<hanbun:
		# u=aida

# if shirushi==1 and blist[-1]%2==0:
	# print(0)
# else:
	# if shirushi==0:
		# if blist[u]-hanbun>hanbun-blist[l]:
			# index=l
		# else:
			# index=u

	# ans=int(2*blist[index]-blist[-1])
	# if ans<0:
		# ans=int(ans*(-1))
	# print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    a[i] += a[i-1]
c = a[-1]
for i in range(n):
   p = abs(a[-1]-a[i]*2)
      if c > p:
        c = p   
print(c)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
a = [int(i) for i in input().split()]

ans = []
x = 0

for h in range(N):
    B = [0]
    for i in range(1,N+1):
        B.append(a[i-1] + B[i-1])

    for i in range(N):
        if B[N] - B[i] == B[i]:
            ans.append(x)
        else:
            a[h] += 1
            x += 1


for h in range(N):
    B = [0]
    for i in range(1,N+1):
        B.append(a[i-1] + B[i-1])

    for i in range(N):
        if B[N] - B[i] == B[i]:
            ans.append(x)
        else:
            a[h] -= 1
            x += 1

#print(ans)
print(min(ans))

Submission status: Runtime Error

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-1):
        left += A[i]
        right -= A[i]
        tmp_a = abs(left - right)
        ret.append(tmp_a)
    return min(ret)

print(check_distance())

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
cnt = 0
S = sum(A)
for i in range(n):
    M1 = sum(A[:i])
    if S/2 < M1:
        M2 = sum(A[:i-1])
        break
print(min(abs(2*M1-S),abs(2*M2-S)))

Submission status: Runtime Error

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



# 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: Runtime Error

Solution code:
import numpy as np
from numba import jit
N=int(input())
A=np.array([int(a) for a in input().split()])
diffs=[]
@jit
def calc(A):
    p=0
    f=sum(A)
    for i in A:
        p+=i
        f-=i
        diff=f-p
        diffs.append(abs(diff))
    return diffs
print(min(calc(A)))

Submission status: Runtime Error

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

if A[:1] < A[1:]:
  print(abs(A[1:]-A[:1]))
  sys.exit()
if A[:1] > A[1:]:
  print(abs(A[:1]-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,d,e))

Submission status: Runtime Error

Solution code:
ans = 0

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

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

Submission status: Runtime Error

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

def dfs(A, k, cnt=0):
  s1, s2 = 0, 0
  for i in range(len(A)):
    if i <= N / 2:
      if A[k] > 1:
        A[k] -= 1
        cnt += 1
      s1 += A[i]
    else:
      if A[k] > 1:
        A[k] -= 1 
      s2 += A[i]
  if s1 == s2:
    return cnt
  if A[k] > 1:
    return dfs(A, k, cnt)
  else:
    return dfs(A, k + 1, cnt)

s = 0
for i in range(len(A)):
  s += A[i]
if s % 2 == 0:
  print(0)
  exit()
else:
  ans = dfs(A, 0)
  print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
#def gNV(list, num):
#    idx = np.abs(np.asarray(list) - num).argmin()
#    return list[idx]

N=int(input())
A=[int(x) for x in input().split()]
Asum=[]
for i in range(N):
    Asum.append(sum(A[:i+1]))
Amax=Asum[-1]
Ahalf=Amax/2.0
if Ahalf.is_integer() and int(Ahalf) in Asum:
    ans=0
else:
    #ANV=gNV(Asum, Ahalf)
    ANV=Asum[np.abs(np.asarray(Asum)-num).argmin()]
    ans=abs(2*ANV-Amax)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

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

sum_of_an = sum(An)

if sum_of_an % 2 == 0:
    half_of_an = sum_of_an / 2
    sum_left_len = 0
    for l in An:
        sum_left_len += l
        if sum_left_len == half_of_an:
            print(0)
            exit(0)
        elif sum_left_len > half_of_an:
            break
min_yen = None
left_len = 0
for (i, l) in zip(range(0, N), An):
    if i == N:
        break

    left_len += l
    if min_yen is None:
        min_yen = abs(sum_of_an - left_len)
    else:
        min_yen = min(min_yen, abs(sum_of_an, left_len))

print(min_yen)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
sumFrontHalf=0
halfIndex=0
sumA=aum(a)
for i in range(n):
  sumFrontHalf+=a[i]
  if sumFrontHalf>=sumA/2:
    halfIndex=i
    break
sumBackHalf=sumA-sumFrontHalf+a[halfIndex]
if sumFrontHalf<sumBackHalf:
  print(sumFrontHalf-sumBackHalf+a[halfIndex])
else:
  print(sumBackHalf-sumFrontHalf+a[halfIndex])

Submission status: Runtime Error

Solution code:
N = int(input())

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

sum_of_an = sum(An)

if sum_of_an % 2 == 0:
    half_of_an = sum_of_an / 2
    sum_left_len = 0
    for l in An:
        sum_left_len += l
        if sum_left_len == half_of_an:
            print(0)
            exit(0)
        elif sum_left_len > half_of_an:
            break
min_yen = None
left_len = 0
for (i, l) in zip(range(0, N - 1), An):
    left_len += l
    if min_yen is None:
        min_yen = abs(sum_of_an - left_len)
    else:
        min_yen = min(min_yen, abs(sum_of_an, left_len))

print(min_yen)

Submission status: Runtime Error

Solution code:
import numpy as np
#def gNV(list, num):
 #   idx = np.abs(np.asarray(list) - num).argmin()
 #   return list[idx]
#def gNV(list, num):
#    sortIdx = np.searchsorted(list, num, side='left')
#    return list[sortIdx]
def gNV(A, target):
    #A must be sorted
    idx = A.searchsorted(target)
    idx = np.clip(idx, 1, len(A)-1)
    left = A[idx-1]
    right = A[idx]
    idx -= target - left < right - target
    return A[idx]

N=int(input())
A=[int(x) for x in input().split()]
Asum=[]
for i in range(N):
    Asum.append(sum(A[:i+1]))
Amax=Asum[-1]
Ahalf=Amax/2.0
if Ahalf.is_integer() and int(Ahalf) in Asum:
    ans=0
else:
    ANV=gNV(Asum, Ahalf)
    ans=abs(2*ANV-Amax)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math

N = int(stdin.readline().rstrip())
d = [int(s) for s in stdin.readline().rstrip().split()]
count = 0
sumF = d[0]
sumB = d[len(d) - 1]
count = 0
countF = 0
countB = len(d) - 1
while count != len(d):
    if sumF < sumB:
        sumF += d[countF+1]
        countF += 1
        count += 1
    elif sumB < sumF:
        sumB += d[countB-1]
        countB -= 1
        count += 1
    else:
        sumF += d[countF+1]
        countF += 1
        sumB += d[countB-1]
        countB -= 1
        count += 2
print(abs(sum(d[0:countF])-sum(d[countF:len(d)])))

Submission status: Runtime Error

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

def dfs(A, k, cnt=0):
  s1, s2 = 0, 0
  for i in range(len(A)):
    if i <= N / 2:
      if A[k] > 1:
        A[k] -= 1
        cnt += 1
      s1 += A[i]
    else:
      if A[k] > 1:
        A[k] -= 1 
      s2 += A[i]
  if s1 == s2:
    return cnt
  if A[k] > 1:
    return dfs(A, k, cnt)
  else:
    return dfs(A, k + 1, cnt)

s = 0
for i in range(len(A)):
  s += A[i]
if s / 2 == 0:
  print(0)
  exit(0)
else:
  ans = dfs(A, 0)
  print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
static const int dx[4] = { 0, 1, 0, -1 };
static const int dy[4] = { 1, 0, -1, 0 };
static const char dir[4] = { 'u', 'r', 'd', 'l' };
static const ll INF = 1 << 21;
static const ll MOD = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N)
    {
        cin >> A[i];
    }
    ll ans = 0(ll);
    if (N % 2 == 0) {
        for (int i = 0; i <= N / 2 - 1; i++) {
            ans += A[i] - A[N - 1 - i];
        }
        cout << abs(ans) << endl;
    } else {
        for (int i = 0; i <= N / 2 - 1; i++) {
            ans += A[i] - A[N - 1 - i];
        }
        cout << abs(abs(ans) - A[N / 2]) << endl;
    }
    return 0;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(map(int, input().split()))

diff = []
for i in range(1, N): 
    diff.append(abs(sum(A[:i]) - sum(A[i:])))
print(min(diff))

Submission status: Runtime Error

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

forward = 0
backward = N-1

sum = list[forward]
sum2 = list[backward]
while (True):
    if sum < sum2:
        forward += 1
        sum += list[forward]
    else:
        backward -= 1
        sum2 += list[backward]
    if backward - forward == 1:
        break
print(abs(sum - sum2))

Submission status: Runtime Error

Solution code:
N=int(input())
A=[int(a) for a in input().split()]
diffs=[]
for i in range(1,N-1):
    x=A[0:i]
    y=A[i:N]
    diff=sum(y)-sum(x)
    diffs.append(abs(diff))

ans=min(diffs)
print(ans)

Submission status: Runtime Error

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

def getTargetIndex():
    diffList = []
    for i in range(0,n-1):
        left = 0
        right = 0
        left = list[:i+1]
        right = list[i+1:]
        diffList.append(abs(sum(left)- sum(right)))  
    return list.index(min(diffList))

div = getTargetIndex()
left = list[:div]
right = list[div:]
print(abs(sum(left) -sum(right)) )

Submission status: Runtime Error

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

sum_a=sum(a)
left_a=0
right_a=sum_a
ans=math.inf

for i in range(n):
    left_a+=a[i]
    right_a-=a[i]
    if(ans>abs(left_a-right_a)):
        ans=abs(left_a-right_a)
print(ans)

Submission status: Runtime Error

Solution code:
half_length = all_length / 2
before_sum = 0
after_sum = 0
for ax in length_list:
    if before_sum+ax < half_length:
        before_sum += ax
        continue
    else:
        after_sum = before_sum + ax
        break
before_diff = all_length - before_sum 
after_diff = all_length - after_sum

if before_diff < after_diff:
    print(before_diff - before_sum)
else:
    print(after_sum - after_diff)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
m = sum(A) // 2
n = 0
for i in range(N):
  if n < m:
    n += A[i]
  elif n >= m and i != N-1:
    a = i
    break
  elif n >= m and i == N - 1:
    a = i
if a == 0:
  print(abs(A[a]-sum(A[a:])))
elif abs(sum(A[:a-1]) - sum(A[a-1:])) > abs(sum(A[:a]) - sum(A[a:])):
  print(abs(sum(A[:a]) - sum(A[a:])))
else:
  print(abs(sum(A[:a-1]) - sum(A[a-1:])))

Submission status: Runtime Error

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

len=sum(a)
len_l=0
for i in range(n):
    if len/2>len_l:
        len_l+=a[i]
    else:
        center=i-1
        break
len_r=len-len_l
len_l-=a[center]

#半分との差
d=len/2-max(len_r,len_l)

print(int(d*2))

Submission status: Runtime Error

Solution code:
import bisect


def CumulativeSum(l):
    a = [0]
    for i in l:
        a.append(a[-1] + i)
    return a[1:]


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = CumulativeSum(a)
    s = b[-1]
    h = s // 2
    x = bisect.bisect_left(b, h)
    try:
        m = (b[x], b[x + 1], b[x - 1])
    except:
        m = (b[x])
    if s % 2 == 0 and h in m:
        print(0)
        return

    print(min([abs(x * 2 - s) for x in m]))


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
#print(n, l)
mid = sum(l) / 2
i = 0
for i in range(n):
    if sum(l[:i]) == mid:
        print(0)
        exit()
    elif sum(l[:i]) < mid:
        i += 1
    else:
        f = i
        break

if min(abs(mid - sum(l[:f - 1])),abs(mid - sum(l[:f]))) == sum(l[:i]) - mid:
    q = sum(l[:i])
else:
    q = sum(l[:i - 1])

p = sum(l)
c = 0

for ii in range(10000000):
    if q / p > 1 / 2:
        q -= 1
        p -= 1
        c += 1
    elif q / p < 1 / 2:
        q += 1
        p += 1
        c += 1
    else:
        break
print(c)

Submission status: Runtime Error

Solution code:
import bisect


def CumulativeSum(l):
    a = [0]
    for i in l:
        a.append(a[-1] + i)
    return a[1:]


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = CumulativeSum(a)
    s = b[-1]
    h = s // 2
    x = bisect.bisect_left(b, h)
    if x != len(a):
        m = (b[x], b[x + 1], b[x - 1])
    else:
        m = (b[x], b[x - 1])
    if s % 2 == 0 and h in m:
        print(0)
        return

    print(min([abs(x * 2 - s) for x in m]))


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
n = int(input())
aa = list(map(int, input().split()))
cnt = 0
total = 0

if sum(aa) % 2 == 1:
    for i in range(n):
        if aa[i] != 1:
            aa[i] -= 1
            cnt += 1
            break

half = sum(aa)//2
for i in range(n):
    total += aa[i]
    if total == half:
        print(0)
        exit()
    if total > half:
        cnt += min(abs(half-total), abs(half-total+aa[i+1]))*2
        break

print(cnt)

Submission status: Runtime Error

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

div_sum_A = sum(A_list) / 2
ans = 0
calculator = 0
for i in range(N):
    calculator += A_list[i]
    if calculator >= div_sum_A:
        if calculator == div_sum_A:
            ans = 0
            break
        elif A_list[i] > div_sum_A:
            ans = 2 * A_list - calculator
            break
        else:
            cand_1 = calculator - (2 * div_sum_A - calculator)
            cand_2 = abs((calculator - A_list[i]) - (2 * div_sum_A - (calculator - A_list[i])))
            ans = min(cand_1, cand_2)
            break

print(int(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
tmp = 0
std = sum(A)/2
index = 0
while tmp < std:
    tmp += A[index]
    index += 1
print(abs(sum(a)-sum(b)))

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
#print(n, l)
mid = sum(l) / 2
i = 0
for i in range(n):
    if sum(l[:i]) == mid:
        print(0)
        exit()
    elif sum(l[:i]) < mid:
        i += 1
    else:
        f = i
        break

if min(abs(mid - sum(l[:f - 1])),abs(mid - sum(l[:f]))) == sum(l[:i]) - mid:
    q = sum(l[:i])
else:
    q = sum(l[:i - 1])

p = sum(l)
c = 0

print(p, q)
for ii in range(10000000):
    if q / p > 1 / 2:
        q -= 1
        p -= 1
        c += 1
    elif q / p < 1 / 2:
        q += 1
        p += 1
        c += 1
    else:
        break
print(c)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
m = sum(A) // 2
n = 0
for i in range(N):
  if n < m:
    n += A[i]
  elif n >= m:
    a = i
    break

if abs(sum(A[:a-1]) - sum(A[a-1:])) > abs(sum(A[:a]) - sum(A[a:])):
  print(abs(sum(A[:a]) - sum(A[a:])))
else:
  print(abs(sum(A[:a-1]) - sum(A[a-1:])))

Submission status: Runtime Error

Solution code:
import bisect


def CumulativeSum(l):
    a = [0]
    for i in l:
        a.append(a[-1] + i)
    return a[1:]


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = CumulativeSum(a)
    s = b[-1]
    h = s // 2
    x = bisect.bisect_left(b, h)
    m1, m2 = b[x], b[x + 1]
    if s % 2 == 0 and h in (m1, m2):
        print(0)
        return
    a = []
    for (i, j) in ((m1, x), (m2, x + 1)):
        t = 0
        n = s
        while True:
            if i * 2 == n:
                a.append(t)
                break
            t += 1
            if i * 2 < n:
                i += 1
                n += 1
            elif j < i:
                i -= 1
                n -= 1
            else:
                n += 1
    print(min(a))


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
import numpy as np
import bisect
#input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
sumA = sum(A)
ans = 0
if sumA%2 != 0:
    ans += 1
cumsum = np.cumsum(A)
half = sumA//2
id = bisect.bisect_left(cumsum,half)
mini = min(abs(half-cumsum[id]),abs(cumsum[id+1]-half))
ans += mini
print(ans)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
l = []
for i in range(N - 2):
    if sum(A[:i + 1]) == sum(A[i + 1:]):
        print(0)
        exit()
for j in range(N - 2):
    d = abs(sum(A[:j + 1]) - sum(A[j + 1:]))
    l.append(d)
print(min(l))

Submission status: Runtime Error

Solution code:
# 2019/11/23
# DISCO presents ディスカバリーチャンネル コードコンテスト2020 予選 - B

# Binary Search
def binSearch (tlis, tval):
    lf = 0
    rg = len(tlis)
    while(lf <= rg):
        ct = int((lf + rg) / 2)
        if tlis[ct] < tval:
            lf = ct + 1
        elif tlis[ct] > tval:
            rg = ct - 1
        else:
            return ct
    return ct

# Input
n = int(input())
a = list(map(int,input().split()))
acm_a = list()
acm_a.append(0)

# Sum
for i in range(len(a)):
    acm_a.append(a[i] + acm_a[i])

# Calc Center
center = acm_a[len(a)] / 2
ctidx = binSearch(acm_a, center)

if (center - acm_a[ctidx]) < (acm_a[ctidx] - center):
    ans = (acm_a[len(a)] - acm_a[ctidx + 1]) - acm_a[ctidx+1]
elif (center - acm_a[ctidx]) > (acm_a[ctidx] - center):
    ans = acm_a[ctidx+1] - (acm_a[len(a)] - acm_a[ctidx + 1])
else:
    ans = 0

# Output
print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

print(abs(right-left))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
from itertools import accumulate
from bisect import bisect_left, bisect_right
n = int(input())
a = list(map(int, input().split()))

sum_par = 0
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):
        obj = prv
        break
    prv = cur

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
l = []
for i in range(N - 2):
    if A[:i + 1] == A[i + 1:]:
        print(0)
        exit()
    else:
        d = abs(A[:i + 1] - A[i + 1:])
        l.append(d)
print(min(l))

Submission status: Runtime Error

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
L=sum(A)
for i in range(N):#真ん中の棒を探す
  if sum(A[:i])>=L/2:
    p=i
    break
X=abs(sum(A[:p])-sum(A[p+1:]))#両サイドの差
Y=A[p]
result=abs(X-Y)
print(result)

Submission status: Runtime Error

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: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = sum(A)/2
M
a = 0
i = 0
while a < M:
    a += A[i]
    i += 1
    
print(int(min(a-M,M-a+A[i])*2))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

for k in range((n+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: Runtime Error

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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect


def CumulativeSum(l):
    a = [0]
    for i in l:
        a.append(a[-1] + i)
    return a[1:]


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = CumulativeSum(a)
    s = b[-1]
    h = s // 2
    x = bisect.bisect_left(b, h)
    m1, m2 = b[x], b[x + 1]
    if s % 2 == 0 and h in (m1, m2):
        print(0)
        return
    a1 = abs(m1 * 2 - s)
    a2 = abs(m2 * 2 - s)
    print(min(a1, a2))


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = sum(A)/2
M
a = 0
i = 0
while a < M:
    a += A[i]
    i += 1
print(int(min(a-M,a-M+A[i])*2))

Submission status: Runtime Error

Solution code:
#import math

input = raw_input
n = int(input())

as = list(map(int, input().split(" ")))
sa = sum(as)
#half_sa= sa // 2 if sa % 2 == 0 else (sa // 2) + 1
half_sa = sa // 2

cur_val = 0
for i, a in enumerate(as):
    cur_val += a
    if half_sa <= cur_val:
        over_i = i # made
        break

cur_val2 = cur_val
cur_val1 = cur_val - a

#print(cur_val1, half_sa, cur_val2)
diff1 = half_sa - cur_val1
diff2 = cur_val2 - half_sa

val_a = cur_val1
val_b = sa - val_a

val_c = cur_val2
val_d = sa - val_c

out = min(val_b-val_a, val_c - val_d)

#if diff1 <= diff2:
#    #half_sa = int(math.floor(half_sa))
#    out = diff1
#else:
#    out = diff2
print(out)

Submission status: Runtime Error

Solution code:
"""
NTC here
"""
from sys import stdin, setrecursionlimit
setrecursionlimit(10**7)

def iin(): return int(stdin.readline())
 
 
def lin(): return list(map(int, stdin.readline().split()))

# range = xrange
# input = raw_input
from math import gcd
gcd=gcd
def lcm(a,b):
    return (a*b)//(gcd(a,b))
def main():
    n=iin()
    a=lin()
    s2=sum(a)
    s1=0
    mn=abs(s2-s1)
    for i in range(n-1):
        s1+=a[i]
        s2-=a[i]
        mn=min(mn,abs(s1-s2))
    print(mn)

"""
1 2 5 8 15 20 23 11 45
"""
        


    
    
            



    
                











main()

Submission status: Runtime Error

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

    sm = sum(A)
    agg = 0
    idx = 0
    mid = sm / 2
    while agg < mid:
        agg += A[idx]
        idx += 1
    
    ans = min(abs(agg-mid) / 0.5, abs(agg-A[idx]-mid)/0.5)
    
    print(int(ans))

if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
r = 0
counter = 0
while r < s//2:
    r += a[counter]
    counter += 1
x = sum(a[:counter])
y = sum(a[counter+1:])
if x <= y:
    x += a[counter]
else:
    y += a[counter]
print(abs(x-y))

Submission status: Runtime Error

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:]))
ans2=abs(cnt+A[i]-sum(A[i+1:]))
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))
ans=9999999999
hidari=sum(a)
migi=0
for i in range(n):
    hidari-=a[i]
    migi+=a[i]
    sa=math.abs(hidari-migi)
    if sa<ans:
        ans=sa
print(ans)

Submission status: Runtime Error

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

import numpy as np

A=np.array(A)

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

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

Submission status: Runtime Error

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


n = INT()
l = MAP()

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

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

Submission status: Runtime Error

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

s=sum(l)
m=0
i=0
while m<s//2:
    m+=l[i]
    i+=1

print(min(2*m-s,s-2*(m-l[i])))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
x,y=map(int,input().split())
list=[300000,200000,100000,0]
if x>4:
    x=4
if y>4:
    y=4
if x==1 and y==1:
    print(2*300000+400000)
else:
    print(list[x-1]+list[y-1])