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: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

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

A = list(accumulate(A))
i = bisect(A, A[-1]/2)

print(min(abs(A[i]*2-A[-1]), abs(A[i-1]*2 - A[-1])))

Submission status: Accepted

Solution code:
def inpl(): return list(map(int, input().split()))

N = int(input())
A = inpl()
S = sum(A)
l = 0
ans = 10**20
for a in A:
    l += a
    ans = min(ans, abs(S-2*l))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    cum = [0]* (n+1)
    for i in range(n):
        cum[i+1] = cum[i] + a[i]
    print(min(abs(2 * i - s) for i in cum))

main()

Submission status: Accepted

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

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

right = sum(a)
left = 0
ans = right

for i in range(n):
    right -= a[i]
    left += a[i]
    ans = min(ans,abs(right - left))

print(ans)

Submission status: Accepted

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

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

B = []
b = 0
for a in A:
    b += a
    B.append(b)

ans = 10**14
for i in range(N):
    ans = min(abs(B[-1]-2*B[i]), ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
ans = 10 ** 10
num = sum(lis)
nu = 0
for i in range(n):
  nu += lis[i]
  ans = min(ans,abs(nu-num/2))
print(int(ans//0.5))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sr = sum(A)
sl = 0
ans = 20202020200
for i in range(N-1):
    sl+=A[i]
    sr-=A[i]
    ans = min(ans, abs(sl-sr))
print(ans)

Submission status: Accepted

Solution code:
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# n
# a, b
# s = input()

N = int(input())
A = [0] + list(map(int, input().split()))


left = 0
right = sum(A)
ans = right
for i in range(1, N+1):
    left += A[i]
    right -= A[i]
    ans = min(ans, abs(right-left))
print(ans)

Submission status: Accepted

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

cost = int(1e18)
p = 0
for i in range(N):
    cost = min(cost, abs(S - p - p))
    p += A[i]
p = 0
for i in range(N):
    cost = min(cost, abs(S - p - p))
    p += A[N - i - 1]
print(cost)

Submission status: Accepted

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):
    c = min(c, abs(a[-1]-a[i]*2))
print(c)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))
right = 0
left = sum(arr)
min = sum(arr)
for i in arr:
    right += i
    left -= i
    if abs(right - left) < min:
        min = abs(right - left)
print(min)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    s=sum(A)
    for i in range(n-1):
        A[i+1]+=A[i]
    ans=INF
    for i in range(n-1):
        ans=min(ans,abs(A[i]-(s-A[i])))
    print(ans)
resolve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))
s = 0
sa = sum(A)
ans = float("inf")
for i in range(n-1):
    s += A[i]
    ans = min(ans, abs(s-(sa-s)))
    # print(s,sa-s)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
al=list(map(int,input().split()))
ans=10**10
l=0
r=sum(al)
for i in al:
    l+=i
    r-=i
    ans=min(ans,abs(l-r))
print(ans)

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	A = getlist()
	X = float("inf")
	s = sum(A)
	c = 0
	for i in range(N):
		c += A[i]
		X = min(X, abs(s - 2 * c))
	print(X)
	


if __name__ == '__main__':
	main()

Submission status: Accepted

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

left = 0
right = sum(a)

d = 10**10
for i in a:
    left += i
    right -= i
    d = min(abs(left-right), d)

print(d)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10**20
p=sum(a)
q=0
for i in range(n-1):
	q+=a[i]
	ans=min(ans,abs(p-q-q))
print(ans)

Submission status: Accepted

Solution code:

N = int(input())

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

asum = sum(A)

l = 0
r = asum
ans = asum
for i in range(N):

    l += A[i]
    r -= A[i]

    ans = min(ans , abs(l - r))

print (ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
sa=sum(a)
now=0
cost=10000000000000000000000
for i in range(n-1):
    now+=a[i]
    rest=sa-now
    cost=min(cost,abs(rest-now))
print(cost)

Submission status: Accepted

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

ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

ans = 10**18
for i in range(n-1):
    ans = min(abs(abs(ruiseki[n] - ruiseki[i+1]) - ruiseki[i+1]), ans)
print(ans)

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)
cs1 = A.cumsum()
cs2 = cs1[-1] - cs1
ans = np.abs(cs1 - cs2).min()
print(ans)

Submission status: Accepted

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

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

SUM=sum(A)

S=0
ANS=1<<60

for a in A:
    S+=a
    ANS=min(ANS,abs((SUM-S)-S))

print(ANS)

Submission status: Accepted

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

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

s = sum(a)

t = 0
res = float("inf")

for i in range(n-1):
    t += a[i]
    res = min(res,abs(s-2*t))
print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

s1 = 0
s2 = sum(As)

ans = s2
for a in As:
    s1 += a
    s2 -= a
    # print(s1, s2)
    ans = min(ans, abs(s1 - s2))
print(ans)

Submission status: Accepted

Solution code:

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


n = int(input())
a = [int(i) for i in readline().split()]

l = sum(a)
r = 0


ans = 10**18
for i in range(n-1):
    l -= a[i]
    r += a[i]
    ans = min(ans, abs(l-r))

print(ans)

Submission status: Accepted

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

su = sum(A)
left = 0
right = su

mindiff = 10**12

for a in A :
    left += a
    right -= a
    mindiff = min(mindiff, abs(right-left))


print(mindiff)

Submission status: Accepted

Solution code:

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
AA = inpl()

S = sum(AA)
L = 0

ans = INF
for a in AA:
    L += a
    R = S - L
    ans = min(ans,abs(L-R))

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  sm=sum(l)
  ans=inf

  _sm=0
  for x in l:
    _sm+=x
    other=sm-_sm

    ans=min(ans,abs(_sm-other))

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

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

s = sum(A)
sa = [A[0]]
for i in range(1, N) :
    sa.append(sa[-1] + A[i])

ret = float('inf')
for i in range(N) :
    ret = min(ret, abs(sa[i] - (s - sa[i])))
    
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = sum(a)
t = 0
ans = float('inf')
for i in range(n - 1):
    t += a[i]
    ans = min(ans, abs(l - 2 * t))
print(ans)

Submission status: Accepted

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

L = 0
R = sum(A)
ans = sum(A)
for i in range(N-1):
  L += A[i]
  R -= A[i]
  ans = min(ans, abs(L-R))
print ans

Submission status: Accepted

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

s = 0
for a in A:
    if (s+a) * 2 <= su:
        s += a
    else:
        ans = min((s+a)*2-su, su-s*2)
        print(ans)
        break

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
SA = sum(A)
CA = list(accumulate(A))
ans = float('inf')
for ca in CA:
    b = SA - ca
    ans = min(ans, abs(b - ca))
print(ans)

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    S = sum(A)
    l = A[0]
    I = 0
    for i in range(1, N):
        if l + A[i] < S / 2:
            l += A[i]
            I = i
    if l == S / 2:
        print(0)
        exit()
    c = A[I + 1]
    r = S - l - c
    ans1 = abs(r - (l + c))
    ans2 = abs(l - (r + c))
    print(min(ans1, ans2))

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    S = sum(A)
    l = A[0]
    I = 0
    for i in range(1, N):
        if l + A[i] <= S / 2:
            l += A[i]
            I = i
    if l == S / 2:
        print(0)
        exit()
    c = A[I + 1]
    r = S - l - c
    ans1 = abs(r - (l + c))
    ans2 = abs(l - (r + c))
    print(min(ans1, ans2))

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
from itertools import accumulate

N = int(input())
A = list([int(x) for x in input().split()])

count = 0
while True:
    # 中心に最も近い点を探す
    ironbar = list(accumulate(A))
    center = sum(A) // 2

    i = 0
    j = len(A) - 1

    while i + 1 < j:
        check = ironbar[(i + j) // 2]
        if check < center:
            i = (i + j) // 2
        elif check > center:
            j = (i + j) // 2
        else:
            print(count)
            exit()

    if check > center:
        A[j] -= 1
    else:
        A[j] += 1

    count += 1

Submission status: Runtime Error

Solution code:
import sys
import collections
import bisect


def main():
    n = int(input())
    AList = list(map(int, input().split()))
    ASum = sum(a)
    c = 0
    for i in range(n):
        c += AList[i]
        if c >= ASum / 2:
            k = i
            break
    ans = min(2 * c - ASum, ASum - 2 * c + 2 * AList[k])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
m=float('INF')
sa=[]
for i,a in enumerate(A):
    sa.append(sa[i]+ai)
for i in range(n+1):
    ans=min(ans,abs(sa[i]-(sa[-1]-sa[i])))
print(ans)

Submission status: Runtime Error

Solution code:
#ifndef LOCAL
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#else
#include <iostream>
#include <iomanip> 
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; }
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds; 
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 

template<typename T> void setmax(T& x, T y) {x = max(x, y);}
template<typename T> void setmin(T& x, T y) {x = min(x, y);}
#define fix(a) fixed << setprecision(a)
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
#define len(V) (int)V.size()
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define endl '\n'

void solve(){
  int n;
  cin >> n;
  vector<ll> a(n);
  ll sm = 0;
  forn(i, n) cin >> a[i], sm += a[i];
  ll ans = sm;
  ll cur = 0;
  forn(i, n){
    cur += a[i];
    sm -= a[i];
    setmin(ans, abs(cur - sm));
  }
  cout << ans;

}

int main() {
  ios::sync_with_stdio(false);
  // cin.tie(0); cout.tie(0);
  int t = 1;
  // cin >> t;
  forn(i, t){
    solve();
  }
}

/* 

k (k-1) (k-1)

2 6 3
4 6 5
6 6 7
7 7 7

*/

Submission status: Runtime Error

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

if sum(a)/2 == sum(a[:(n//2)]):
  print(0)
elif sum(a)/2 >= sum(a[:(n//2)]):
  x = n//2
  while sum(a)/2 >= sum(a[:x]):
    x += 1  
  print(min(abs(2*sum(a[:x])-sum(a)), abs(2*sum(a[:x-1])-sum(a))))
else:
  while sum(a)/2 <= sum(a[:x]):
    x -= 1
  print(min(abs(2*sum(a[:x])-sum(a)), abs(2*sum(a[:x-1])-sum(a))))

Submission status: Runtime Error

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

for i in range(n):
  if sum(a)/2 <= sum(a[:i]):
    x = i
    break
    
print(sum(a[:x])*2 - sum(a))

Submission status: Runtime Error

Solution code:
## B - Iron Bar Cutting
N = int(input())
A = list(map(int, input().split()))
val = sum(A)//2
tmp = 0
for n in range(N):
    tmp += A[n]
    if tmp >= val:
        print( min( abs(sum(A[n+1:] - tmp ), abs(sum(A[n:] - tmp - A[n] ) )
        break

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
tmp=0
for i in range(n): 
    tmp+=a[i]
    if tmp*2==sum(a):
        print(0)
        sys.exit()
    if tmp*2>sum(a):
        l=i
        break
if abs(tmp*2-sum(a))>abs((tmp-a[l])*2-sum(a)):
    print(abs((tmp-a[l-1])*2-sum(a)))
else:
    print(abs(tmp*2-sum(a)))

Submission status: Runtime Error

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

count1=0
i=0
while True:
    count1+=A[i]
    i+=1
    ans1=count1-sum(A[i:])
    if ans1>=0 and ans1<=i:
        break


A=A[::-1]
count2=0
j=0
while True:
    count2+=A[j]
    j+=1
    ans2=count2-sum(A[j:])
    if ans2>=0 and ans2<=j:
        break

print(min(ans1,ans2))

Submission status: Runtime Error

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

ans = 10**8 
sum_A = sum(A)
t = 0


for i in range(N-1):
  hoge = hoge + A[i]
  ans = min(ans,abs(sum_A - (hoge*2)))

print(ans)

Submission status: Runtime Error

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

ans = 10**8 
sum_A = sum(A)
hoge = 0


for i in range(N-1):
  CumSum = CumSum + A[i]
  ans = min(ans,(abs((sum_A - hoge)-(hoge))))

print(ans)

Submission status: Runtime Error

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

A_cs = A.cumsum()
A_cs = A_cs - A_cs[-1]/2
div = np.abs(A_cs).argmin()

print(abs(A[:div+1].sum - A[div+1:].sum()))

Submission status: Runtime Error

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

A_cs = A.cumsum()
A_cs = A_cs - A_cs[-1]/2
div = np.abs(A_cs).argmin()

print(abs(A[:div+1].sum()A - [div+1:].sum()))

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    from itertools import accumulate

    N = int(input())  # len(A)
    A = list(map(int, input().split()))
    zenbu=sum(A)
    B = [0] + A
    B = list(accumulate(B))
    C = [zenbu]*N
    import numpy as np
    B=np.array(B[1:])
    C=np.array(C)
    D=C-B
    ans=10**18
    for i in range(1, N+1):
        ans = min(abs(B[i]-D[i]),ans)
    print(ans)
resolve()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
res = 2020202020
for i in range(n):
  res = min(res, abs(a[:i] - a[i:]))
print(res)

Submission status: Runtime Error

Solution code:
rom itertools import accumulate
def myAnswer(N:int,A:list) -> int:
   accum = list(accumulate(A))
   ans = 10**10
   now = accum.pop()
   for a in accum:
      if(abs(now - a - a) < ans):
         ans = abs(now - 2*a)
   return ans

   

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Submission status: Runtime Error

Solution code:
FUCKOFF

Submission status: Runtime Error

Solution code:
# B - Iron Bar Cutting
N = int(input())
A = [map(int, input().split())]

left = 0
right = sum(A)
ans = right

for a in A:
	left += a
	right -= a
	
	diff = abs(right - left)
	ans = min(ans, diff)
	
	if left > right:
		break

print(ans)

Submission status: Runtime Error

Solution code:
from numpy import cumsum

N = int(input())
A = list(map(int, input().split()))
a_cum = cumsum(A)
inv_a_cum = [a_cum[-1] - a_cum[i] for i in range(N)]

for i in range(N):
    ans = min(ans, abs(a_cum[i] - inv_a_cum[i]))

Submission status: Runtime Error

Solution code:
def main():
    import sys
    import bisect


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

    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect(b, l // 2)
    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    import bisect
    from itertools import accumulate

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

    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect_left(b, l // 2)
    ans = min(abs(l - 2 * b[i]), abs(l - 2 * b[i + 1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

n = int(input())
a = list(map(int,input().split()))
d = [0]

for i in range(n):
    d.append(d[i] ;a[i])

ans = d[-1]

for i in range(n):
    ans =min(ans,abs(d[-1] -2*d[i]))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
S = sum(A)
L = A[0]
m = S-L
k = 1
for i in range(1, N-1):
  L += A[i]
  if S-L < m:
    k = i+1
print(abs(S-2*sum(A[:k]))

Submission status: Runtime Error

Solution code:
# import math
import bisect
import numpy as np
mod = 10**9 + 7

# http://cav-inet.hateblo.jp/entry/2017/02/18/165141
def binary_search(list,target): 

	result = -1
	left = 0
	right = len(list) - 1

	while left <= right:
		center = (left + right)//2
		if list[center] == target:
			result = center
			break
		elif list[center] < target:
			left = center + 1
		elif list[center] > target:
			right = center - 1

	if result == -1:
		return False
	else:
		return True

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

if binary_search(c, c[n-1]/2):
    print(0)
    exit()

i = bisect.bisect_left(c, c[n-1]/2)
ans = c[i+1] - c[i]
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
cn=0
buf=s
for i in range(n):
  cn+=a[i]
  buf=min(bug,abs(s-2+cn))
print(buf)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
tmp=sys.maxsize
s = sum(a)
for i in range(n):
    t += a[i]
    tmp = min(tmp,abs(s-t*2))
print(tmp)

Submission status: Runtime Error

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

i, s = 0, 0
while True:
	s += a[i]
	i += 1
	if s >= sum(a) // 2:
		break

b = a[::-1]
j, t = 0, 0
while True:
	t += b[j]
	j += 1
	if t >= sum(a) // 2:
 
print(min(s-(sum(a)-s), t-(sum(a)-t)))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Asum = sum(A)
Asum2 = 0
for mid in range(len(A)):
    if 2*Asum2 > Asum:
        Asum3 = Asum2
        mid -= 1
        Asum2 -= A[mid]
        break
    Asum2 += A[mid]

if abs(Asum - 2*Asum2) <= abs(Asum - 2*Asum3):
    Asuma = Asum2
else:
    Asuma = Asum3
c = abs(Asum - 2*Asuma)
print(c)

Submission status: Runtime Error

Solution code:
n = input()
a = [int(ai) for ai in input().split()]
 
ans = sum(a)
 
for i in range(len(a)):
  al, ar = a[:i], a[i:]
  ans = min(ans, abs(sum(al) - sum(ar)))

print(ans)
            
input()

Submission status: Runtime Error

Solution code:
n = input()
a = [int(ai) for ai in input().split()]
 
ans = sum(a)
 
for i in range(a):
  al, ar = a[:i], a[i:]
  ans = min(ans, abs(sum(al) - sum(ar))
 
print(ans)

Submission status: Runtime Error

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

ans = sum(a)

for i in range(a):
  al, ar = a[:i], a[i:]
  ans = min(ans, abs(sum(al) - sum(ar))

print(ans

Submission status: Runtime Error

Solution code:
n=int(input())
arr=list(map(int,input().split()))
total=sum(arr)
acum=[arr[0]]
for i in range(1,n):
  acum.append(arr[i]+acum[-1])
ans=10**18
for i in range(n):
  tmp=abs(acum[i]-(total-acu[i]))
  ans=min(ans,tmp)
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

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

sum_l = [0]+list(accumulate(a))
sum_r = [0]+list(accumulate(a[:-1]))

cost = sum_r[-1]
for i in range(n+1):
    cost = min(cost, abs(sum_l[i]-sum_r[n-i]))
print(cost)

Submission status: Runtime Error

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

import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    al = list(map(int, input().split()))

    cum_len = [0] * len(al)
    cum_len[0] = al[0]
    for i in range(1, len(al)):
        cum_len[i + 1] = cum_len[i] + al[i]

    ans = 1000000
    for i in range(len(al)-1):
        left = cum_len[i]
        right = cum_len[-1] - cum_len[i]
        ans = min(ans, abs(left - right))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
arr = map(int,input().split())
k=0
if n%2==1:
	k1=0
	k2=0
	for i in range(n//2):
		k1=k1+arr[i]-arr[n-i-1]
	print(abs(abs(k1)-arr[n//2]))
else:
	for i in range(n//2):
		k=k+arr[i]-arr[n-1-i]
	print(abs(k))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, A: "List[int]"):
    r = [0] * (N+1)
    rr = [0] * (N+1)

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

    from bisect import bisect_left
    c = sum(A)/2
    p = bisect_left(r, c)
    t = min(abs(r[p] - c),abs(r[p+1] - c))/0.5
    print(int(t))

    return



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

Solution code:
from itertools import accumulate
import math

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

ans = 10**10

sum_list_aaa =list(accumulate(list_aaa))

for i in range(N):
    zen = sum_list_aaa[i]
    go = sum_list_aaa[-1]-sum_list_aaa[i]
    ans = min(ans,abs(zen-go)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(n) for n in input().split()]
T = sum(A)
M = T/2
L = 0
ans = 0
for i in range(len(A)):
    if L < M:
        L += A[i]
    elif L == M:
        R = sum(A[i:])
        break
    else:
        L -= A[i-1]
        R = sum(A[i:])
        if L < R:
            L += A[i-1]
        else:
            R += A[i-1]
        break
print(abs(L-R))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(n) for n in input().split()]
T = sum(A)
M = T//2
L = 0
idx = 0
for i in range(len(A)):
    if L < M:
        L += A[i]
    else:
        R = sum(A[i:])
        break
print(abs(L-R))

Submission status: Runtime Error

Solution code:
n=int(input())

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

s=0

for i in range(len(x)):

	s+=x[i]i=0

l=0

while (l<(s//2)):

	l+=x[i]

	i+=1

r=abs((s-l)-l) 

i=len(x)-1

l=0

while(l<(s//2)):

	l+=x[i]

	i-=1

if abs((s-l)-l)<r:

	r=abs((s-l)-l) 

print (r)

Submission status: Runtime Error

Solution code:
import bisect
N = int(input())
A = [int(i) for i in input().split()]
g = sum(A)/2

S = []
s = 0
for a in A:
    s += a
    S.append(s)

i = bisect.bisect_left(S, g)

if int(g) in S:
    print(0)
else:
    print(min(S[i]-S[i-1], S[i+1]-S[i]))

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
A=list(map(int, input().split()))
A=np.array(A)
left=0 
right=N-1
while left<right:
  ans=0
  mid=(left+right)//2
  if A[:mid+1].sum()<A[mid+1:].sum():
    ans=min(ans,abs(A[:mid+1].sum()<A[mid+1:]))
    left=mid+1
  else:
    right=mid
print(abs(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
A=list(map(int, input().split()))
A=np.array(A)
left=0 
right=N-1
while left<right:
  ans=0
  mid=(left+right)//2
  if A[:mid+1].sum()<A[mid+1:].sum():
    ans=min(ans,A[:mid+1].sum()<A[mid+1:])
    left=mid+1
  else:
    right=mid
print(ans)

Submission status: Runtime Error

Solution code:
_ = input()
input_cra = input().split(" ")
input_num = [int(input_) for input_ in input_cra]

S = sum(input_num)
minx = S
for i in range(len(input_num)):
    sum_ = sum(input_num[i:])
    minx = min(minx,abs(sum_-(S-sum_)))
    if minx = 0:
      break

print(minx)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
L, R, df = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if df < 0:
        L += 1,
        df += A[L]
    else:
        R -= 1
        df -= A[R]
print(abs(df))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
L, R, df = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if df < 0:
        L += 1,
        df += A[L]
    else:
        R -= 1
        df -= A[R]
print(abs(sumL_R))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input.split()))
r = sum(a)
ra = 0
result = 1e30
for i in range(0,N):
    ra += a[i]
    r -= a[i]
    result = min(result, abs(ra-r))
print(int(result))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
r = sum(a)
ra = 0
result = 1e30
for i in tqdm(range(0,N)):
    ra += a[i]
    r -= a[i]
    result = min(result, abs(ra-r))
print(result)

Submission status: Runtime Error

Solution code:
n = int(input())

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

L = sum(a)

nagasa = 0

ans = 0

i=0

for i in range(n):
  nagasa += a[i]
  if nagasa == L/2:
    break
  elif nagasa > L/2:
    if i == 0:
      sa = abs(nagasa - L/2)
    elif i == n-1:
      sa = abs(L/2-sum(a[0:i-1])
    else:
      sa = min(abs(nagasa - L/2),abs(L/2-sum(a[0:i-1])))
    
    ans = int(sa * 2)
    break
    

print(ans)

Submission status: Runtime Error

Solution code:
import sys

def main():
    n, *a = map(int, sys.stdin.readline().split())

    l = 0
    r = sum(a)
    m = sum(a)
    for i in range(n):
        l += a[i]
        r -= a[i]
        m = min(m, abs(l - r))
    
    print(m)


if __name__ == '__main__':
    main()