Problem:


Score: 200 points

Problem Statement

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

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

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

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

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

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

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

Sample Input 1

3
2 4 3

Sample Output 1

3

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

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

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

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

Sample Input 2

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

Sample Output 2

0



Sample input:
3
2 4 3


Sample output:
3

Submission status: Wrong Answer

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

ans1 = 0
for i in range(round(n/2)):#12
    ans1 += a[i]
    
ans2 = 0
for i in range(round(n/2),n):#3
    ans2 += a[i]

ans3 = 0
ans3 = abs(ans1-ans2)

ans4 = 0
ans1 = 0
for i in range(math.floor(n/2)):#12
    ans1 += a[i]
    
ans2 = 0
for i in range(math.floor(n/2),n):#3
    ans2 += a[i]
    
ans4 = abs(ans1-ans2)
print(min(ans3,ans4))    
#2 4 3

Submission status: Wrong Answer

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

L=sum(A)
l=0
for i in range(n):
  l+=A[i]
  if l >= L/2:
    r=l-A[i]
    break
print(l,r)
print(min(abs((L-r)-r),abs((L-l)-l)))

Submission status: Wrong Answer

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

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

cnt=0
sum_a=sum(a)
half_sum_a=sum_a//2
tmp_sum=0
for i in range(n):
    tmp_sum+=a[i]
    ind=i
    if tmp_sum>=half_sum_a:
        break
nokori=sum_a-tmp_sum
print(tmp_sum-nokori)

Submission status: Wrong Answer

Solution code:
def 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)
for i in range(n):
    if sum(l[:i+1]) >= s/2:
        tmp = i
        break

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

Submission status: Wrong Answer

Solution code:

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

mae=0
ato=0

while A:
    if ato<mae:
        ato+=A.pop(-1)
    else:
        A=A[::-1]
        mae+=A.pop(-1)
        A=A[::-1]

print(mae-ato)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

tot = sum(ls)

p = 0
for i in range(n):
  p += ls[i]
  if p>= tot/2:
    break
print(2*p-tot)

Submission status: Wrong Answer

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

length = sum(a)
if length %2 ==0:
    check = 0
    half = length//2
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    if check - half > half - (check-a[idx]):
        check -=a[idx]
    
    ans = (half-check)*2
else:
    check = 0
    half = length//2
    idx = 0
    for i in range(len(a)):
        check+=a[i]
        if check > half:
            idx = i
            break
    if check - half > half - (check-a[idx]):
        check -=a[idx]
    
    ans = abs((half-check))*2
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def readInts():
  return [int(s) for s in input().split(" ")]

def solve(N, A):
  s = sum(A)

  sp = 0
  for a in A:
    sp += a
    if sp >= s / 2:
      break

  return 2 * sp - s

def main():
  N = readInts()
  A = readInts()

  print(solve(N, A))

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
A = sum(a)
x = abs(A - 2*a[0])
ll = [x]

for i in range(1,n-1):
    x = abs(x-2*a[i])
    ll.append(x)

print(min(ll))

Submission status: Wrong Answer

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

L = 0
R = 0

for i in range(N//2):
    L += A[i]
    R += A[N-i-1]
if (N % 2 == 1):
    if L < R:
        L += A[N//2]
    else:
        R += A[N//2]

print(abs(L-R))

Submission status: Wrong Answer

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

length = 0
L = sum(A)

for i in range(N):
    length += A[i]
    if length >= L / 2:
        break

sum_left = length
sum_right = L - length

if sum_right == 0:
    sum_left -= A[-1]
    sum_right += A[-1]

ans = abs(sum_left - sum_right)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 10**9
r = sum(a)
l = 0
for i in range(n-1):
    r -= a[i]
    l += a[i]
    s = abs(r-l)
    ans = min(ans,s)
print(ans)

Submission status: Wrong Answer

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

s = sum(a)

left = 0
right = 0
for i in range(n):
    left += a[i]
    if left >= s/2:
        right = s - left
        break
    
if left == s/2:
    print(0)
else:
    print(abs(left-right))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
#print(A)
L=[A[0]]
for i in range(1,N):
    L.append(L[-1]+A[i])
S=sum(A)
#print(S)
ans=10**15
for i in range(1,N):
    if abs(S-2*L[i])<ans:
        ans=abs(S-2*L[i])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import  numpy as np

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    L = R = 0
    M = sum(A)/2
    for a in A:
        if L < M:
            L += a
        else:
            R += a
    res = L-R
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
n, *a = map(int, open(0).read().split())
l, r = a[0], sum(a[1::])
ans = 1e10
for i in range(1, n):
    l += a[i]
    r -= a[i]
    ans = min(ans, abs(l-r))
print(ans)

Submission status: Wrong Answer

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

tot_len = sum(A)
cum = 0
for i in range(N-1):
    cum += A[i]
    if cum >= tot_len/2:
        break
first = sum(A[:i+1])
last = sum(A[i+1:])
print(abs(first - last))

Submission status: Wrong Answer

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

s = sum(a)

sum_ = 0
idx = -1
for i in range(n):
    sum_ += a[i]
    if sum_ * 2 >= s:
        idx = i
        break

j = max(0, idx - 1)

ret = abs(sum_ - (s - sum_))
ret = min(ret, sum_ - a[idx] - (s - sum_ - a[idx]))

print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = sum(a)
s = 0
for aa in a:
    s += aa
    if s >= l/2:
        break
print(s-(l-s))

Submission status: Wrong Answer

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

if(N%2 == 1):
  sum1,sum2 = 0,0
  for i in range(N):
    if(i <= N//2): sum1 += A[i]
    else: sum2 += A[i]
  ans = abs(sum1-sum2)
else:
  sum1,sum2 = 0,0
  for i in range(N):
    if(i <= N//2 -1): sum1 += A[i]
    else: sum2 += A[i]
  ans = abs(sum1-sum2)
  
print(ans)

Submission status: Wrong Answer

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

ans = 10**9
r = sum(a)
l = 0
for i in range(n):
    s = abs(r-l)
    ans = min(ans,s)
    r -= a[i]
    l += a[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
wa=[a[0]]
for i in range(1,n):
    wa.append(a[i]+wa[-1])
s=sum(a)
import bisect
x=bisect.bisect_left(wa,s//2)
ans1=abs(s-wa[x]*2)
print(ans1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans1 = 0
for i in range(round(n/2)):
    ans1 += a[i]
    
ans2 = 0
for i in range(round(n/2),n):
    ans2 += a[i]
    
print(abs(ans1-ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
r = 0
l = 0
k = n//2
for i in range(k):
    l += a[i]
    r += a[-i-1]

if n % 2 == 0:
    print(max(l,r)-min(l,r))
else:
    print(min(max(l+a[k],r)-min(l+a[k],r),max(l,r+a[k])-min(l,r+a[k])))

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    if n % 2 == 0:
        print(abs(sum(a[:n//2]) - sum(a[n//2:])))
    else:
        print(min(abs(sum(a[:n//2]) - sum(a[n//2:])), abs(sum(a[:n//2 + 1]) - sum(a[n//2 + 1:]))))


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	to1 = 0
	to2 = 0

	toto = 0
	for i in A:
		if to1 >= to:
			to2 += i
		else:
			to1 += i

	ans = abs(to1-to2)
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    s = [0]
    for i in a:
        s.append(s[-1]+i)
    k = s[-1]
    f = k&1
    k >>= 1
    ans = float("inf")
    for i in s:
        ans = min(ans,abs(i-k))
    print(ans+f)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
m = map(int, raw_input().split())

for i in range(1, n):
  m[i] += m[i - 1]

mini = 1e10
for i in range(1, n - 1):
  diff = abs(m[i] - (m[n - 1] - m[i]))
  if diff < mini:
    mini = diff

print(mini)

Submission status: Wrong Answer

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

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    numbers=[]
    n = int(input())
    numbers=np.array(list(map(int,input().split())))
    left=[]
    right=[]
    left=np.cumsum(numbers[:1])
    right=np.cumsum(numbers[1:])
    for i in range(2,n):
        if abs(right[-1]-left[-1]) > abs(np.cumsum(numbers[i:])[-1] -np.cumsum(numbers[:i])[-1]):
            left=np.cumsum(numbers[:i])
            right=np.cumsum(numbers[i:])
    print(abs(right[-1]-left[-1]))

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    numbers=[]
    n = int(input())
    numbers=np.array(list(map(int,input().split())))

    for i in range(n):
        if i==0:
            left=numbers[:i].sum()
            right=numbers[i:].sum()
            continue
        else:
            if abs(right-left) > abs(numbers[i:].sum() -numbers[:i].sum()):
                left=numbers[:i].sum()
                right=numbers[i:].sum()
    print(abs(right-left))


if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    numbers=[]
    n = int(input())
    numbers=list(map(int,input().split()))

    for i in range(n):
        if i==0:
            left=sum(numbers[:i])
            right=sum(numbers[i:])
            continue
        else:
            if abs(right-left) > abs(sum(numbers[i:]) -sum(numbers[:i])):
                left=sum(numbers[:i])
                right=sum(numbers[i:])
    print(abs(right-left))        


if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = list(map(int,input().split()))
tmp = 0
i = 0
while tmp < (sum(x) / 2):
    tmp += x[i]
    i += 1
# 2 4 3 sum=9 tmp=6 i=2  
a = tmp - (sum(x) - tmp)
b = (sum(x) - tmp + x[i-1]) - (tmp - x[i-1])
print(min(a, b))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(map(int,input().split()))
ans = 10**10
for i in range(n):
    ans = min(ans,(abs(sum(s[:i]) - sum(s[i:]))))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(map(int,input().split()))
ans = []
for i in range(n):
    ans.append(abs(sum(s[:i]) - sum(s[i:])))
print(min(ans))

Submission status: Time Limit Exceeded

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

l_l = [ sum(a_l[:i])  for i in range(n) ]
r_l = [ sum(a_l[i:])  for i in range(n) ]


ans = float('inf')
for i in range(n):
    ans = min(abs(l_l[i]-r_l[i]), ans)

print(ans)

Submission status: Time Limit Exceeded

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

mini = float('inf')

if sum(A[0:(N//2)]) < sum(A[(N//2):N]):
    for i in range(N//2 + 1):
        if abs(sum(A[0:i+(N//2)]) - sum(A[i+(N//2):N])) < mini:
            mini = abs(sum(A[0:i+(N//2)]) - sum(A[i+(N//2):N]))
elif sum(A[0:(N//2)]) > sum(A[(N//2):N]):
    for i in range(N//2 + 1):
        if abs(sum(A[0:(N//2)-i]) - sum(A[(N//2)-i:N])) < mini:
            mini = abs(sum(A[0:(N//2)-i]) - sum(A[(N//2)-i:N]))
else:
    print(0)
    exit()

'''
for i in range(N):
    if abs(sum(A[0:i]) - sum(A[i:N])) < mini:
        mini = abs(sum(A[0:i]) - sum(A[i:N]))
    if abs(sum(A[0:-i]) - sum(A[-i:N])) < mini:
        mini = abs(sum(A[0:-i]) - sum(A[-i:N]))
    if mini == 0:
        break
'''

print(mini)

Submission status: Time Limit Exceeded

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

mini = float('inf')

for i in range(N):
    if abs(sum(A[0:i]) - sum(A[i:N])) < mini:
        mini = abs(sum(A[0:i]) - sum(A[i:N]))
    if abs(sum(A[0:-i]) - sum(A[-i:N])) < mini:
        mini = abs(sum(A[0:-i]) - sum(A[-i:N]))
    if mini == 0:
        break

print(mini)

Submission status: Time Limit Exceeded

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

mini = float('inf')

for i in range(N):
    if abs(sum(A[0:i]) - sum(A[i:N])) < mini:
        mini = abs(sum(A[0:i]) - sum(A[i:N]))

print(mini)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# DISCO presents ディスカバリーチャンネル コードコンテスト2020 予選: B – Iron Bar Cutting

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

heads = [sum(A[0:i]) for i in range(1, N)]
tails = [sum(A[i:N]) for i in range(1, N)]

candidates = [abs(m - n) for m, n in zip(heads, tails)]

print(min(candidates))

Submission status: Time Limit Exceeded

Solution code:
# DISCO presents ディスカバリーチャンネル コードコンテスト2020 予選: B – Iron Bar Cutting

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

heads = [sum(A[0:i]) for i in range(1, N)]
tails = [sum(A[i:N]) for i in range(1, N)]

candidates = [abs(heads[i] - tails[i]) for i in range(N -1)]

print(min(candidates))

Submission status: Time Limit Exceeded

Solution code:
# DISCO presents ディスカバリーチャンネル コードコンテスト2020 予選: B – Iron Bar Cutting

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

diffs = []
for i in range(1, N):
    diffs.append(abs(sum(A[0:i]) - sum(A[i:N])))

print(min(diffs))

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=20202020200
for i in range(N):
    tmp=abs(sum(A[:i])-sum(A[i:N]))
    if tmp<ans:
        ans=tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

MMM = map(int,input().split())
MM =list(MMM)
mini = 2020202020
for i in range(N):
  total1 = 0
  total2 = 0
  for j in range(i+1):
    
    total1 += MM[j]
  for k in range(i+1,N):
    
    total2 += MM[k]
  if abs(total1-total2)< mini:
    mini = abs(total1 -total2)
print(mini)

Submission status: Time Limit Exceeded

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

ans = [0, 0]
ans_abs = 10**200

for i in range(1, N):
    A_1 = sum(A[:i])
    A_2 = sum(A[i:])
    ans_abs = min(ans_abs, abs(A_1 - A_2))

print(ans_abs)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
tmp_x = -1
tmp_x_diff = -1
for i in range(N - 1):
    sum1 = sum(A[:i + 1])
    sum2 = sum(A[i + 1:])
    diff = abs(sum1 - sum2)
    if tmp_x_diff == -1 or diff < tmp_x_diff:
        tmp_x = i
        tmp_x_diff = diff
print(tmp_x_diff)

Submission status: Time Limit Exceeded

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

    mid = 0
    diff = float("inf")
    for i in range(1, n):
        if abs(sum(A[:i]) - sum(A[i:])) <= diff:
            diff = abs(sum(A[:i]) - sum(A[i:]))
            mid = i
    
    front = A[:mid] 
    back =  A[mid:]
    print(abs(sum(front)-sum(back)))
resolve()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


n = int(stdin.readline().strip())
a_lst = [int(x) for x in stdin.readline().strip().split()]

min_diff = 2020202021

for i in range(len(a_lst)):
  left = sum(a_lst[:i])
  right = sum(a_lst[i:])
  if abs(left - right) < min_diff:
    min_diff = abs(left - right)
    
print(min_diff)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
m=float('inf')
s=sum(A)
for i in range(len(A)):
    m=min(m,abs(2*sum(A[0:i])-s))
print(m)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
m=float('inf')
for i in range(len(A)):
    m=min(m,abs(sum(A[0:i])-sum(A[i:n])))
print(m)

Submission status: Time Limit Exceeded

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

ans = 10**11

for i in range(n):
    if abs(sum(A[:i]) - sum(A[i:])) < ans:
        ans = abs(sum(A[:i]) - sum(A[i:]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 10**18
for i in range(N):
    ans = min(ans, abs(sum(A[:i])-sum(A[i:])))
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    tmp = abs(sum(a[:i])-sum(a[i:]))
    ans = tmp if i==0 else min(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

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

b=0
c=10**10
for i in range(1,n):
  b=abs(sum(a[:i])-sum(a[i:]))
  if b<c:
    c=b
print(c)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
s = sum(a)
r = [sum(a[:i + 1]) for i in range(n)]
print(min(abs(2 * i - s) for i in r))

Submission status: Time Limit Exceeded

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

ans = 10**10
for i in range(1, N):
    ans = min(ans, abs(sum(a[:i])-sum(a[i:])))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    diff = 10 ** 10
    for i in range(N):
        partA = sum(A[:i])
        partB = sum(A[i:])
        diff = min(diff, abs(partA-partB))
    
    print(diff)
    return
resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(1,N):
  try:
    dif=min(dif,abs(sum(A[:i])-sum(A[i:])))
  except:
    dif=abs(sum(A[:i])-sum(A[i:]))
print(dif)

Submission status: Time Limit Exceeded

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

s = []
for i in range(n):
  s.append(abs(sum(a[:i])*2-sum(a)))

print(min(s))

Submission status: Time Limit Exceeded

Solution code:
## B - Iron Bar Cutting
N = int(input())
A = list(map(int, input().split()))
ans = []
for n in range(1,N):
    ans.append(abs(sum(A[:n]) - sum(A[n:])))
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
sticks = list(map(int, input().split()))
diffMin = sum(sticks)
for i in range(len(sticks)):
    diff = abs(sum(sticks[:i]) - sum(sticks[i:]))
    if diff < diffMin:
        diffMin = diff
print(diffMin)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
diff = float("INF")
idx = -1
for i in range(N):
  S[i+1] = S[i] + A[i]
  if abs(sum(A)/2 - S[i+1]) < diff:
    diff = abs(sum(A)/2 - S[i+1])
    idx = i+1
lsum = S[idx]
rsum = S[-1]-S[idx]
print(abs(lsum-rsum))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
diff = float("INF")
idx = -1
for i in range(N):
  S[i+1] = S[i] + A[i]
  if abs(sum(A)/2 - S[i+1]) < diff:
    diff = sum(A)/2 - S[i+1]
    idx = i+1
lsum = S[idx]
rsum = S[-1]-S[idx]
print(abs(lsum-rsum))

Submission status: Time Limit Exceeded

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

total = sum(a)

ans = float("INF")
for i in range(n-1):
    former = sum(a[:i+1])
    ans = min(ans, abs(former - (total - former)))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
mina=10**10
L=list(map(int,input().split()))
for i in range(N-1):
  c=abs(sum(L[:i+1])-sum(L[i+1:]))
  mina=min(mina,c)
mina=min(mina,sum(L))
print(mina)

Submission status: Time Limit Exceeded

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

ans = float("inf")

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 100000000000000000

for i in range(1,n):
    x = sum(li[:i])
    y = sum(li[i:])
    z = abs(x-y)
    if z < ans:
        ans = z
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# For taking integer inputs.
import math
from decimal import Decimal
from itertools import permutations


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


n = inp()
a = inlist()
D = 10000000000000000
mid = 0
for i in range(1, n):

    s1 = sum(a[:i])
    s2 = sum(a[i:])

    d = abs(s1 - s2)

    if d < D:
        mid = i
        D = d
s1 = sum(a[:mid])
s2 = sum(a[mid:])
res = abs(s1-s2)
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split())) # 横入力

b = [0]
aaa = 0
for i in range(n):
    aaa += a[i]
    b.append(aaa)

ans = 202020202020
for i in range(n):
    ans = min(ans, abs(sum(a) - b[i]*2))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
MIN = float('inf')
for i in range(1,N):
    MIN = min(MIN,abs(sum(A[:i])-sum(A[i:])))
print(MIN)

Submission status: Time Limit Exceeded

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = LI()
ans = float('INF')
for i in range(1,N):
    ans = min(ans,abs(sum(A[:i])-sum(A[i:])))
print(ans)

Submission status: Time Limit Exceeded

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

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    ans=10**18
    for i in range(1,n):
        x=abs(sum(l[:i])-sum(l[i:]))
        ans=min(x,ans)
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    lans=[]
    for i in range(1,n):
        lans.append(abs(sum(l[:i])-sum(l[i:])))
    print(min(lans))
resolve()

Submission status: Time Limit Exceeded

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

m = 10**10
for i in range(n):
    x = sum(a[:i])
    y = sum(a[i:])
    m = min(abs(x-y),m)
print(m)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
A=int(input())
l=list(map(int,input().split()))
n=set()
for i in range(A-1):
    n.add(abs(sum(l[:i+1])-sum(l[i+1:])))
print(min(n))

Submission status: Time Limit Exceeded

Solution code:
'''
参考
https://qiita.com/c-yan/items/f7c99c8f8ec418a58c42

棒の全長をL、切断箇所をCとすると、かかるコストは|2C-L|となる
（左端から切断箇所までの長さを2倍し、元の棒の全長を引いたものの絶対値）
∵
・CがL/2よりも左にある場合（=棒の左端からCまでの長さのほうが短い）
⇒Cよりも左側に2C-L足すか、右側を2C-L減らす
・CがL/2よりも右にある場合（=棒の左端からCまでの長さのほうが長い）
⇒Cよりも左側を2C-L減らすか、右側に2C-L足す

与えられたリストAのうち、どの地点をCとするかは
リストAの先頭の要素からN-1個の要素をそれぞれ調べ、
先頭要素から現在の要素までの合計（累積和）を格納するリストを別途作成、
そのリストに対して|2C-L|が最小となるケースを調べる
'''
N = int(input())
A = list(map(int, input().split()))
L = sum(A)
tmp = []

for i in range(N):
    tmp.append(sum(A[:i+1]))

cost = min(abs(c*2-L) for c in tmp)
print(cost)

Submission status: Time Limit Exceeded

Solution code:
'''
参考
https://qiita.com/c-yan/items/f7c99c8f8ec418a58c42

棒の全長をL、切断箇所をCとすると、かかるコストは|2C-L|となる
（左端から切断箇所までの長さを2倍し、元の棒の全長を引いたものの絶対値）
∵
・CがL/2よりも左にある場合（=棒の左端からCまでの長さのほうが短い）
⇒Cよりも左側に2C-L足すか、右側を2C-L減らす
・CがL/2よりも右にある場合（=棒の左端からCまでの長さのほうが長い）
⇒Cよりも左側を2C-L減らすか、右側に2C-L足す

与えられたリストAのうち、どの地点をCとするかは
リストAの先頭の要素からN-1個の要素をそれぞれ調べ、
先頭要素から現在の要素までの合計（累積和）を格納するリストを別途作成、
そのリストに対して|2C-L|が最小となるケースを調べる
'''
N = int(input())
A = list(map(int, input().split()))
L = sum(A)
tmp = []

for i in range(N):
    tmp.append(sum(A[:i+1]))

cost = min([abs(c*2-L) for c in tmp])
print(cost)

Submission status: Time Limit Exceeded

Solution code:
'''
参考
https://qiita.com/c-yan/items/f7c99c8f8ec418a58c42

棒の全長をL、切断箇所をCとすると、かかるコストは|2C-L|となる
（左端から切断箇所までの長さを2倍し、元の棒の全長を引いたものの絶対値）
∵
・CがL/2よりも左にある場合（=棒の左端からCまでの長さのほうが短い）
⇒Cよりも左側に2C-L足すか、右側を2C-L減らす
・CがL/2よりも右にある場合（=棒の左端からCまでの長さのほうが長い）
⇒Cよりも左側を2C-L減らすか、右側に2C-L足す

与えられたリストAのうち、どの地点をCとするかは
リストAの先頭の要素からN-1個の要素をそれぞれ調べ、
先頭要素から現在の要素までの合計（累積和）を格納するリストを別途作成、
そのリストに対して|2C-L|が最小となるケースを調べる
'''
N = int(input())
A = list(map(int, input().split()))
L = sum(A)
tmp = []

for i in range(N):
    tmp.append(sum(A[:i+1]))

cost = max(A)
for c in tmp:
    if abs(c*2-L) <= cost:
        cost = abs(c*2-L)

print(cost)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    A=[int(_) for _ in input().split()]
    ans = sum(A)-A[0]
    for i in range(N):
        ans = min(ans, abs(sum(A[:i]) - sum(A[i:])))
    print(ans)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def resolve():
    N,=pin()
    A=lispin()
    sumA=[sum(A[:i+1])for i in range(N)]
    #print(sumA)
    ans=2**32-1
    t=sumA[-1]
    for i in range(N):
        ans=min(ans,abs(t-sumA[i]*2))
        
    print(ans)
#%%submit!
resolve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def resolve():
    N,=pin()
    A=lispin()
    sumA=[sum(A[:i+1])for i in range(N)]
    #print(sumA)
    ans=2020202020
    t=sumA[-1]
    for i in range(N):
        ans=min(ans,abs(t-sumA[i]*2))
    print(ans)
#%%submit!
resolve()