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: 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
    for i in range(N):
        ans=min(ans,abs((sumA[-1]-sumA[i])-sumA[i]))
    print(ans)
#%%submit!
resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_inputs = list(map(int,input().split()))
len_bar = sum(a_inputs)

gap = float('inf')
for i in range(n-1):
    g = abs(len_bar-2*sum(a_inputs[:i+1]))
    if g<gap:
        gap=g
print(gap)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
arr = list(map(int, input().split()))
min_delta = sum(arr)
for i in range(1, N):
    min_delta = min(abs(sum(arr[:i]) - sum(arr[i:])), min_delta)

print(min_delta)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import*
n, *a = map(int, open(0).read().split())
print(min(abs(2*c - sum(a)) for c in list(accumulate(a))))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    a =list(map(int, input().split()))
    b = sum(a)
    res = 10000000000000000
    for i in range(n-1):
        s = sum(a[:i+1])
        t = b-s
        res = min(res, abs(s-t))
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
all_li = []
for i,l in enumerate(li):
    if i == 0:
        continue
    left = sum(li[:i])
    right = sum(li[i:])
    #print(i,left,right)
    all_li.append(abs(left-right))
print(min(all_li))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
LS = [0]*N
RS = [0]*N
ans = float("INF")
for i in range(N):
  LS[i] = sum(A[:i+1])
  RS[i] = sum(A[i+1:])
  ans = min(ans, abs(RS[i] - LS[i]))
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    l1 += a[i] 
    if l1> sum(a)//2:break
r1 = sum(a) - l1
l2 = l1 -a[i]
r2 = sum(a) - l2
ans = min(abs(r1-l1),abs(r2-l2))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
g=sum(l)/2
l=[sum(l[0:i]) for i in range(1,n)]
print(int(min([abs(g-i)*2 for i in l])))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans = float('inf')
for i in range(1, n):
    x = A[:i]
    y = A[i:]
    sum_x = sum(x)
    sum_y = sum(y)
    ans = min(ans, abs(sum_x - sum_y))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
wa = []
wa2 = []
for i in range(n):
    wa.append(sum(a[0:i+1]))
    wa2.append(sum(a[-1:-(i+2):-1]))
 
ans = 10**10
for i in range(n-1):
    if ans > abs(wa[i] - wa2[-(i+2)]):
        ans = abs(wa[i] - wa2[-(i+2)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
wa = []
wa2 = []
for i in range(n):
    wa.append(sum(a[0:i+1]))
    wa2.append(sum(a[-1:-(i+2):-1]))

ans = 10**10
for i in range(n-1):
    if ans > abs(wa[i] - wa2[-(i+2)]):
        ans = abs(wa[i] - wa2[-(i+2)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
ans = 10000000000
cum_sum1 = []
cum_sum2 = []
for i in range(N-1):
    tmp = abs(sum(A_list[0:i+1])-sum(A_list[i+1:N]))
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
ans = 10000000000
cum_sum1 = []
cum_sum2 = []
for i in range(N-1):
    cum_sum1.append(sum(A_list[0:i+1]))
    cum_sum2.append(sum(A_list[i+1:N]))
for i in range(len(cum_sum2)):
    tmp = abs(cum_sum2[i]-cum_sum1[i])
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

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

left, right = [], []
total = 0

while a != []:
    if total > 0:
        tmp = a.pop()
        right.append(tmp)
        total -= tmp
    else:
        tmp = a.pop(0)
        left.append(tmp)
        total += tmp

print(abs(sum(left) - sum(right)))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int,input().split()))
sumL=sum(L)
ans=2020202020
tmp1=0
tmp2=0
for i in range(n):
    for j in range(i):
        tmp1+=L[j]
    tmp2=sumL-tmp1
    ans=min(ans,abs(tmp1-tmp2))
    tmp1=0
    tmp2=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=0
A1=[]
A2=[]
s1=0
s2=0
c=float("inf")
for i in range(N-1):
    A1=A[:i+1]
    A2=A[i+1:]
    s1=sum(A1)
    s2=sum(A2)
    if abs(s1-s2)<c:
        c=abs(s1-s2)
print(c)

Submission status: Time Limit Exceeded

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

min = 2020202021

for i in range(1,len(a)):
    
    if min > abs(sum(a[:i]) - sum(a[i:])):
        min = abs(sum(a[:i]) - sum(a[i:]))

print(min)

Submission status: Time Limit Exceeded

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

min = sum(a)

for i in range(1,len(a)):
    
    if min > abs(sum(a[:i]) - sum(a[i:])):
        min = abs(sum(a[:i]) - sum(a[i:]))

print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#TLE
import sys
n = int(input())
a = list(map(int, input().split()))
postCenter=dif=0
tmp = 2020202020
for i in range(n):
    if tmp > abs(sum(a[:i])-sum(a[i:])):
        tmp = abs(sum(a[:i])-sum(a[i:]))
        if tmp == 0:
            print(tmp)
            exit()
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
postCenter=dif=0
tmp = sys.maxsize
for i in range(n):
    if tmp > abs(sum(a[:i])-sum(a[i:])):
        tmp = abs(sum(a[:i])-sum(a[i:]))
        if tmp == 0:
            print(tmp)
            exit()
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
postCenter=dif=0
tmp = sys.maxsize
for i in range(n):
    if tmp > abs(sum(a[:i])-sum(a[i:])):
        tmp = abs(sum(a[:i])-sum(a[i:]))
print(int(tmp))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
s = sum(A)
p = 10**20
t = 0
for i in range(n):
    t += A[i]
    p = min(p, abs(s-t*2))
    if 2*t > sum(A):
        break
print(p)

Submission status: Time Limit Exceeded

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

d = sum(A)
for i in range(n):
    d = min(abs(sum(A[:i]) - sum(A[i:])), d)
print(d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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:
		break
 
print(min(s-(sum(a)-s), t-(sum(a)-t)))

Submission status: Time Limit Exceeded

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

Asum = sum(A)
ans = float('inf')
for i in range(len(A)-1):
    s = sum(A[:i+1])
    t = Asum - s
    ans = min(ans, abs(s-t))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=0
d=float("INF")
for i in range(n):
    c+=a[i]
    d=min(d,abs(sum(a)-2*c))
print(d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=0
d=float("INF")
for i in range(n):
    c+=a[i]
    d=min(d,abs(sum(a)-2*c))
print(d)

Submission status: Time Limit Exceeded

Solution code:
N = input().split()
A = input().split()
a = []
if int(N[0]) >= 2 and int(N[0]) <= 200000:
  n = int(N[0])
  for i in range(n):
    if int(A[i]) >= 1 and int(A[i]) <= 2020202020:
      a.append(int(A[i]))
countA = len(a)
sabun = []
right = 0
left = 0
for i in range(n - 1):
  left += a[i]
  for k in range(n - 1, i, -1):
    right += a[k]
  sabun.append(abs(right - left))
  right = 0
print(min(sabun))

Submission status: Time Limit Exceeded

Solution code:
N = input().split()
A = input().split()
intA = []
if int(N[0]) >= 2 and int(N[0]) <= 200000:
	intN = int(N[0])

for i in range(intN):
  if int(A[i]) >= 1 and int(A[i]) <= 2020202020:
  	intA.append(int(A[i]))

countA = len(intA)
sabun = []
right = 0
left = 0

for i in range(len(intA) - 1):
  left += intA[i]
  for k in range(len(intA) - 1, i, -1):
    right += intA[k]
  sabun.append(abs(right - left))
  right = 0
print(min(sabun))

Submission status: Time Limit Exceeded

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

ans = 10**10
s = sum(a_li)

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

Submission status: Time Limit Exceeded

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

ans = 10**10

for i in range(1,n):
    s = abs(sum(a_li[:i]) - sum(a_li[i:]))
    ans = min(ans,s)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:

N = input().split()
A = input().split()
intA = []
if int(N[0]) >= 2 and int(N[0]) <= 200000:
	intN = int(N[0])

for i in range(intN):
  if int(A[i]) >= 1 and int(A[i]) <= 2020202020:
  	intA.append(int(A[i]))

countA = len(intA)
sabun = []
right = 0
left = 0

for i in range(len(intA) - 1):
  left += intA[i]
  for k in range(len(intA) - 1, i, -1):
    right += intA[k]
  sabun.append(abs(right - left))
  right = 0
print(min(sabun))

Submission status: Time Limit Exceeded

Solution code:

N = input().split()
A = input().split()
intA = []
intN = int(N[0])

for i in range(intN):
  intA.append(int(A[i]))

countA = len(intA)
sabun = []
right = 0
left = 0

for i in range(len(intA) - 1):
  left += intA[i]
  for k in range(len(intA) - 1, i, -1):
    right += intA[k]
  sabun.append(abs(right - left))
  right = 0
print(min(sabun))

Submission status: Time Limit Exceeded

Solution code:
#切れ目N　区間長さ
N = input().split()
A = input().split()
intA = []
intN = int(N[0])

for i in range(intN):
  intA.append(int(A[i]))
#print(intA)

countA = len(intA)
sabun = []
right = 0
left = 0

for i in range(countA - 1):
  left += intA[i]
  for k in range(countA -1, i, -1):
    right += intA[k]
  sabun.append(abs(right - left))
  #print(left)
  #print(right)
  right = 0
print(min(sabun))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(int, input().split(" ")))
d = {i: (sum(A[:i]), sum(A[i:])) for i in range(N)}

print(min(abs(value[0] - value[1]) for key, value in d.items()))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
ans = float("inf")
for i in range(N):
    S = sum(L[:i])
    T = sum(L[i:])
    ans = min(ans,(abs(S-T)))
print(ans)

Submission status: Time Limit Exceeded

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))
print(min(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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

print(minx)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
midlen = sum(A)/2
B = [sum(A[:i]) for i in range(N+1)]
ans = 2*min(abs(B[i]-midlen) for i in range(N+1))
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

lines = sys.stdin.readlines()
N = int(lines[0])

stdin = list(map(int,lines[1].split(' ')))
lst = np.zeros((3,N), dtype=np.int32)

lst[1] += sum(stdin)
for i, a in enumerate(stdin):
    lst[0,i:] += a
    lst[1,i:] -= a
    lst[2,i] = abs(lst[0,i]-lst[1,i])
    i += 1

print(min(lst[2]))

Submission status: Time Limit Exceeded

Solution code:
import sys

def expand(bou_len, cost):
    bou_len += 1
    cost += 1
    return bou_len, cost

def shrink(bou_len, cost):
    bou_len -= 1
    cost += 1
    return bou_len, cost

lines = sys.stdin.readlines()
N = int(lines[0])

lst = []
for n in map(int,lines[1].split(' ')):
    lst.append(n)

lst_sum = sum(lst)
half_len = lst_sum / 2

center_idx = 0
bar_len = 0
center_dist = 1000000
for index, element in enumerate(lst):
    bar_len += element
    if abs(half_len - bar_len) < center_dist:
        center_idx = index
        center_dist = abs(half_len - bar_len)
        if center_dist == 0:
            break

left_half = lst[:center_idx+1]
right_half = lst[center_idx+1:]

left_sum = sum(left_half)
right_sum = sum(right_half)

cost = 0
while (left_sum != right_sum):
    if left_sum < right_sum:
        left_sum, cost = expand(left_sum, cost)
    if left_sum > right_sum:
        left_sum, cost = shrink(left_sum, cost)

print(cost)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

INPUT=input()
INPUT_S=INPUT.split()

A=[int(i) for i in INPUT_S]

count=0

MAX_L=sum(A)

def check(i):
    length=0
    
    for i in range(i+1):
        length += A[i]        
    
    if length > MAX_L-length:
        d = length - (MAX_L-length)
    else:
        d = (MAX_L-length) - length

    return d


def main():
    d=0
    min_d=0

    min_d = check(0)

    for i in range(1,N):
        d = check(i)
        if d < min_d:
            min_d = d
    
    if min_d == 0:
        return 0
    else:
        return min_d

count=main()

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

d = []
for i in range(1, len(A_list)):
		left = sum(A_list[:i])
		right = sum(A_list[i:])
		temp = abs(left - right)
		d.append(temp)

print(min(d))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
array = input().split(' ')

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

prev = 0
for i in range(0,n-1):
    diff = abs(getSum(0, i+1) - getSum(i+1, n))
    if(i == 0):
        prev = diff
    else:
        if diff < prev:
            prev = diff
print(prev)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

L = sum(a)

nagasa = 0

ans = 0

i=0
while(i < len(a)):
  nagasa += a[i]
  if nagasa == L/2:
    break
  elif nagasa > L/2:
    sa1 = L/2 - sum(a[0:i])
    sa2 = nagasa - L/2
  
    if sa1 < sa2:
      # 後ろを減らす
      a[i] -= 1
    else:
      # 前を減らす
      a[i-1] -= 1
    
    L = sum(a)
    ans += 1
    i = 0
    nagasa = 0
  else:
    i+=1

print(ans)

Submission status: Time Limit Exceeded

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

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

prev = 0
for i in range(0,n-1):
    diff = abs(getSum(0, i+1) - getSum(i+1, n))
    if(i == 0):
        prev = diff
    else:
        if diff < prev:
            prev = diff
print(prev)

Submission status: Time Limit Exceeded

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

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

prev = 0
for i in range(0,n-1):
    diff = abs(getSum(0, i+1) - getSum(i+1, n))
    if(i == 0):
        prev = diff
    else:
        if diff < prev:
            prev = diff
print(prev)

Submission status: Time Limit Exceeded

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

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

prev = 0
for i in range(0,n-1):
    diff = abs(getSum(0, i+1) - getSum(i+1, n))
    if(i == 0):
        prev = diff
    else:
        if diff < prev:
            prev = diff
print(prev)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
h = list(map(int, input().split()))
b = [0]
c = []
for i in range(1,len(h)):
    b.append(abs(sum(h[0:i]) - sum(h[i:len(h)])))
    if i >= 2 and b[i] >= b[i-1] and c == []:
        c = b[i-1]
        break
else: c = b[a-1]
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
dif=9999999999999999999
x=-1
ans=0
for i in range(N):
  long=abs(sum(A[i+1:])-sum(A[:i+1]))
  if long<dif:
    dif=long
    x=i+1
pl=max(sum(A[:x]),sum(A[x:]))
mi=min(sum(A[:x]),sum(A[x:]))
while mi<pl:
  mi+=1
  ans+=1
  if mi==pl:
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = tuple(map(int, input().split()))
b = np.full(n, np.inf)
for i in range(n-1):
    x = abs(sum(a[:i+1])-sum(a[i+1:]))
    b[i] = x
print(int(min(b)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(200000)

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

def sum(a, b):
    if b == 0:
        return a[b]
    else:
        return a[b] + sum(a, b-1)
sumA = []
for i in range(N):
    sumA.append(sum(A, i))

ans = 0

for i in range(N):
    if sumA[-1] // 2 > sumA[i]:
        continue
    else:
        if i == 0:
            ans = abs(sumA[i] * 2 - sumA[-1])
            break
        else:
            ans = min(abs(sumA[i] * 2 - sumA[-1]), abs(sumA[i-1] * 2 - sumA[-1]))
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def fast_argmin(array):
    array = list( array )
    return array.index(min(array))
def fast_gNV(list, num):
    idx = fast_argmin(np.abs(np.asarray(list) - num))
    return list[idx]
#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 set(Asum):
    ans=0
else:
    ANV=fast_gNV(Asum, Ahalf)
    #ANV=Asum[np.abs(np.asarray(Asum)-Ahalf).argmin()]
    ans=abs(2*ANV-Amax)
print(ans)

Submission status: Time Limit Exceeded

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

s2=s/2
cnt=0
for i,k in zip(a,range(len(a))):
    cnt+=i
    if cnt>s/2:
        break
my2=True
if cnt-a[k]<s-cnt:#小さいほう
    res=0
    my=True
    b=cnt
    while my:
        b-=1
        s-=1
        res+=1
        if b==s/2:
            my=False
        if b<s/2:
            my=False
            my2=False
    if my2:
        print(res)
    else:
        print(0)
else:
    res=0
    my=True
    b=s-cnt+a[k]
    while my:
        b-=1
        s-=1
        res+=1
        if b==s/2:
            my=False
        if b<s/2:
            my=False
            my2=False
    if my2:
        print(res)
    else:
        print(0)

Submission status: Time Limit Exceeded

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

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

min = sum(A)
for i in range(N-1):
    F = A[:i+1]
    L = A[i+1:]
    Fs = sum(F)
    Ls = sum(L)
    temp = abs(Fs-Ls)
    if min > temp:
      index = i
      min = temp
      chk = Fs-Ls
      Flist = F
      Llist = L

c1, c2 = 0, 0

Fmemo = Flist
Lmemo = Llist

if chk == 0:
    print(0)

elif chk > 0:

    counter = 0
    while sum(Flist) != sum(Llist):
        for i in range(len(Flist)):
            if Flist[i] >= 2:
                Flist[i] = Flist[i] - 1
                c1 += 1
                break
    print(c1)

elif chk < 0:
    Flist = Fmemo

    counter = 0
    while sum(Flist) != sum(Llist):
        for i in range(len(Llist)):
            if Llist[i] >= 2:
                Llist[i] = Llist[i] - 1
                c2 += 1
                break
    print(c2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
X=sum(A)
ans=0
long=[abs(X-sum(A[:i+1])*2) for i in range(N)]
x=long.index(min(long))+1
pl=max(sum(A[:x]),sum(A[x:]))
mi=min(sum(A[:x]),sum(A[x:]))
while mi<pl:
  mi+=1
  ans+=1
  if mi==pl:
    break
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def sum(a, b):
    if b == 0:
        return a[b]
    else:
        return a[b] + sum(a, b-1)
sumA = []
for i in range(N):
    sumA.append(sum(A, i))

ans = 0

for i in range(N):
    if sumA[-1] // 2 > sumA[i]:
        continue
    else:
        if i == 0:
            ans = abs(sumA[i] * 2 - sumA[-1])
            break
        else:
            ans = min(abs(sumA[i] * 2 - sumA[-1]), abs(sumA[i-1] * 2 - sumA[-1]))
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

n = int(input().rstrip())
a_list = list(map(int,input().split()))
money = 2020202020

for i in range(len(a_list)-1):
    x = a_list[:i+1]
    y = a_list[i+1:]
    z = abs(sum(x)-sum(y))
    
    if money > z:
        money = z

print(money)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

ans = sum(a)

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = list(map(int, input().split()))
for i in range(a+1):
        if sum(b[0:i-1]) == sum(b)/2:
            print(0)
        if sum(b[0:i-1]) < sum(b)/2 < sum(b[0:i]):
                v = sum(b[0:i])- (sum(b)-sum(b[0:i]))
                u = (sum(b)-sum(b[0:i - 1]))-sum(b[0:i -1])
                print(min(v,u))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=0
long=[abs(sum(A[i+1:])-sum(A[:i+1])) for i in range(N)]
x=long.index(min(long))+1
pl=max(sum(A[:x]),sum(A[x:]))
mi=min(sum(A[:x]),sum(A[x:]))
while mi<pl:
  mi+=1
  ans+=1
  if mi==pl:
    break
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def getSum(list):
    sum = 0
    for i in list:
      sum = sum + i
    return sum  

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