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:
N = int(input())
A_list = [int(item) for item in input().split()]
mini = 2020202020

all_sum = sum(A_list)

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

while is_search:
    if j == 0:
        is_search = False

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

    if delta > 0:
        j += int(j / 2)

    elif delta < 0:
        j = int(j / 2)
    else:
        mini = 0
        is_search = False

print(mini)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

N=int(input())
A=[int(x) for x in input().split()]
Asum=[]
for i in range(N):
    Asum.append(sum(A[:i+1]))
Amax=Asum[-1]
Ahalf=Amax/2.0
if Ahalf.is_integer() and int(Ahalf) in set(Asum):
    ans=0
else:
    #ANV=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 = [int(item) for item in input().split()]
A_max = max(A_list)
mini = 2020202020 * 200000

all_sum = sum(A_list)

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

while is_search:
    if j == 0:
        is_search = False

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

    if delta > 0:
        j += int(j / 2)

    elif delta < 0:
        j = int(j / 2)
    else:
        mini = 0
        is_search = False

print(mini)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    start_len = sum(A)
    temp_len = sum(A)
    sousa = 0
    if(start_len % 2 == 1):
        sousa += 1
    while(1):
        seikou1 = kazoe(N, A, start_len + sousa, sousa, "+", start_len)
        seikou2 = kazoe(N, A, start_len - sousa, sousa, "-", start_len)
        if(seikou1 == True or seikou2 == True):
            break
        sousa += 2
    print(sousa)
            
            
def kazoe(N,A,temp_len, sousa, mi_pl, start_len):
    if(temp_len == 0):
        return False
    mokuteki = temp_len/2
    temp = 0
    for i in range(N):
        temp += A[i]
        if(temp > mokuteki):
            break
    i1 = i-1
    kouho1 = temp - A[i]
    i2 = i
    kouho2 = temp
    if(mi_pl == "+"):
        if(kouho1 + sousa == mokuteki or start_len - kouho2 + sousa == mokuteki):
            return True
        else:
            return False
    else:
        if(start_len - kouho1 - sousa == mokuteki or kouho2 - sousa == mokuteki):
            return True
        else:
            return False

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
A_list = [int(item) for item in input().split()]
A_max = max(A_list)
mini = 2020202020 * 200000

all_sum = sum(A_list)

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

while is_search:
    if j == 1:
        is_search = False

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

    if delta > 0:
        j += int(j / 2)

    elif delta < 0:
        j = int(j / 2)
    else:
        mini = 0
        is_search = False

print(mini)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = list(map(int, input().split()))
for i in range(a):
        if sum(b[0:i-1]) == sum(b)/2:
            print(0)
            break
        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)) 
if a == 2:
     print(abs(b[0]-b[1]))

Submission status: Time Limit Exceeded

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

diff = []
sam = 0
for i in range(N-1):
    sam += A[i]
    diff.append(abs(sum(A) - 2*sam))
print(min(diff))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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


getTargetIndex()

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    s = sum(A)
    a = []
    b = []
    for i in range(len(A)):
        if s % 2 != 0:
            if sum(a) >= (s // 2) - 1 or i == N-1:
                b.append(A[i])
            else:
                a.append(A[i])
        else:
            if sum(a) >= (s // 2) or i == N-1:
                b.append(A[i])
            else:
                a.append(A[i])
    ans1 = abs(sum(a)-sum(b))
    ans2 = abs(sum(a[:-1])-sum(b+[a[-1]]))
    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def getTargetIndex():
    target = 0
    prev =  0
    for i in range(0,n-1):
        left = 0
        right = 0
        left = list[:i+1]
        right = list[i+1:]
        diff = abs(sum(left)- sum(right))
        if(i == 0):
            target = i
            prev = diff
        else:
            if diff < prev:
                target = i
                prev = diff
    return target + 1

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

Submission status: Time Limit Exceeded

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

base_1=0
base_2=0
count=0

for i in range(N):
    if base_1>=base_2:
        base_2+=A.pop(N-1-i)
    else:
        base_1+=A.pop(0)
            
if base_1==base_2:
         print(count)
else:
    count+=abs(base_1-base_2)
    print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans=3*(10**9)
for i in range(N-1):
  l=(numpy.cumsum(A))[i]
  D=abs(l-(sum(A)-l))
  if ans > D:
    ans=D
  if l >= (sum(A)//2)+1:
    break

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))
    if min(s)<s[i]:
        break
print(min(s))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

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

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:
import math
import numpy as np
def gNV(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]

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

Submission status: Time Limit Exceeded

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

farst = A[0]
last = A[-1]
A = A[1:-1]
for i in range(N-2):
    if farst > last:
        last+=A[-1]
        del A[-1]
    else:
        farst+=A[0]
        del A[0]
print(abs(farst-last))

Submission status: Time Limit Exceeded

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

for a in range(len(length)+1):
    x = sum(length[:a])
    y = sum(length[a:])
    pos.append(max(x,y) - min(x,y))
    # print("pos " + str(a)+":",sum(length[:a]), sum(length[a:]))
# print(pos)

print(min(pos))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans=3*(10**9)
for i in range(N-1):
  l=(numpy.cumsum(A))[i]
  D=abs(l-(sum(A)-l))
  if ans > D:
    ans=D

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
mindif = 1e15
minidx=0
for i in range(1,N):
    dif = abs(sum(a[:i])-sum(a[i:]))
    if dif<mindif:
        mindif = dif
        minidx = i
    else:
        break
print(mindif)

Submission status: Time Limit Exceeded

Solution code:

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


def reduceNum(index):
    list[index] = list[index] - 1

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

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

if n==2:
  print(abs(r[0]-r[1]))
  exit()

border=sum(r)//2+1
result="INF"
for i in range(1,n):
  #print(r[:i],r[i:])
  if result=="INF":
    result=abs(sum(r[:i])-sum(r[i:]))
  else:
    result=min(result,abs(sum(r[:i])-sum(r[i:])))
  
  if sum(r[:i])>=border:
    print(result)
    exit()
print(result)

Submission status: Time Limit Exceeded

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

def h(n,A):
    res = float('inf')
    for i in range(len(A)):
        temp1 = sum(A[:i])
        temp2 = sum(A[i:])
        res = min(res,abs(temp1-temp2))
    print(res)
h(n,A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
mindif = 1e100
minidx=0
for i in range(1,N):
    dif = abs(sum(a[:i])-sum(a[i:]))
    if dif<mindif:
        mindif = dif
        minidx = i
    else:
        break
print(mindif)

Submission status: Time Limit Exceeded

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

total = sum(a)
mid = int((total + 1) / 2)

over_idx = len(a) - 1
for i in range(1,len(a)):
    if sum(a[:i]) >= mid:
        over_idx = i - 1
        break

diff1 = abs(sum(a[:over_idx]) - sum(a[over_idx:]))
diff2 = abs(sum(a[:over_idx+1]) - sum(a[over_idx+1:]))

ans = min(diff1, diff2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_sekisan=0
money=[]
for i in range(N):
    money.append(abs(sum(A[:i+1])-sum(A[i+1:])))
money_min=min(money)
print(money_min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
al = []
for i in range(1,n):
        sum1 = sum(a[0:i])
        sum2 = sum(a[i:])
        al.append(abs(sum1 - sum2))
        if sum1 == sum2:
                print(0)
                exit()

minDiffIdx = al.index(min(al))

# print("---")
# print(minDiffIdx)
# print("---")

tmp1 = sum(a[0:minDiffIdx])
tmp2 = sum(a[minDiffIdx:])
# print(tmp1)
# print(tmp2)
diff1 = abs(tmp1 - tmp2)

tmp3 = sum(a[0:minDiffIdx + 1])
tmp4 = sum(a[minDiffIdx + 1:])
# print(tmp3)
# print(tmp4)
diff2 = abs(tmp3 - tmp4)
print(min(diff1, diff2))

# d1 = diff1
# d2 = diff2

# if d1 < d2:
#         for i in range(n//2):
#                 if a[i] > 1:
#                         d1 -= a[i]
#                 if d1 < 0:
#                         ans += diff1 + d1
#                         break
#                 elif d1 == 0:
#                         ans += diff1
#                         break
#         if 
# else:

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
anslist = []
for i in range(N):
    lefts = sum(A[:i])
    rights = sum(A[i:])
    anslist.append(abs(lefts - rights))
print(min(anslist))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
la = list(map(int, input().split()))
ld = [abs(sum(la[0:j+1])-sum(la[j+1:n])) for j in range(1, n)]
if n == 2:
    print(abs(la[0] - la[1]))
else:
    print(min(ld))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_sekisan=0
a=[]
for i in range(N):
    A_sekisan = A_sekisan + A[i]
    a.append(A_sekisan)
dif_list = []
for i in range(N):
    dif = abs(float(sum(A))/2 - a[i])
    dif_list.append(dif)
cutnum = dif_list.index(min(dif_list))
money=abs(sum(A[:cutnum+1])-sum(A[cutnum+1:]))
print(money)

Submission status: Time Limit Exceeded

Solution code:
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()))
A_sekisan=0
a=[]
for i in range(N):
    A_sekisan = A_sekisan + A[i]
    a.append(A_sekisan)
dif_list = []
for i in range(N):
    dif = abs(float(sum(A))/2 - a[i])
    dif_list.append(dif)
cutnum = dif_list.index(min(dif_list)) #が最後の切れ目の番号
money=abs(sum(A[:cutnum+1])-sum(A[cutnum+1:]))
print(money)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
P = [int(_) for _ in input().split()]

d = sum(P)
min_i = 0
S_left = 0
S_right= 0

for i in range(N):
    S_left = sum(P[:i])
    S_right = sum(P[-(N-i):])
    d_temp = S_left - S_right
    
    if d >= abs(d_temp):
        d = abs(d_temp)
    
    else:
        break

    if d == 0:
        break

print(abs(d))

Submission status: Time Limit Exceeded

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

ans = 0
k = n // 2

if sum(a[k:]) > sum(a[:k]):
    ans = sum(a[k:]) - sum(a[:k])
    for i in range(k, n):
        if ans > abs(sum(a[i:]) - sum(a[:i])):
            ans = abs(sum(a[i:]) - sum(a[:i]))
else:
    ans = sum(a[:k]) - sum(a[k:])
    for i in range(k, 0, -1):
        if ans > abs(sum(a[i:]) - sum(a[:i])):
            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()))


diff = 2020202020 * 200000 + 1
tot = sum(A)

for i in range(N):
    temp1 = A[:i+1]
    # temp2 = A[i+1:]
    temp3 = abs(tot - 2 * sum(temp1))
    if temp3 <= diff:
        # mid = i
        diff = temp3
    else:
        break

print(diff)

Submission status: Time Limit Exceeded

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

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

# 一度差を出してから，どんどんi-1, i+1の和で縮めていく．
def check():
    for i in range(N):
        if sum(A[:i]) == sum(A[i:]):
            return True
    return False

from functools import lru_cache
@lru_cache(maxsize=100000)
def get_dist(i):
    return abs(sum(A[:i]) - sum(A[i:]))

def check_distance():
    ret = []
    for i in range(1, N):
        tmp_a = get_dist(i)
        ret.append(tmp_a)
        # print(f"at {i}, abs:{tmp_a}")
    return min(ret)

def check_distance_2():
    ret = []
    tmp_a = abs(sum(A[:1]) - sum(A[1:]))
    # print(tmp_a)
    for i in range(1, N):
        tmp_a = abs(tmp_a - (A[i]*2))
        ret.append(tmp_a)
        # print(tmp_a)
        # print(f"at {i}, abs:{tmp_a}")
    return min(ret)

print(check_distance())

Submission status: Time Limit Exceeded

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

mini = 2020202020 * 200000

all_sum = sum(A_list)

for j in range(1,N):
    F = A_list[:j]
    F_sum = sum(F)
    B_sum = all_sum - F_sum

    delta = abs(B_sum - F_sum)
    if delta <= mini:
        mini = delta

print(mini)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list1 = list(map(int,input().split()))
sums = sum(list1)
maxim = sums
for i in range(n):
  list2 = list1[0:i]
  a = sums - sum(list2)
  if maxim > abs(sum(list2)-a):
    maxim = abs(sum(list2)-a)
print(maxim)

Submission status: Time Limit Exceeded

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


S = sum(A)
mp = S/2

i = 1

for i in range(N):
    L = A[0:i]
    R = A[i:N]
    SL = sum(L)
    SR = sum(R)
    # print(L, end = "\t\t")
    # print(R)
    if SL >= SR:
        result = min(result, SL - SR)
        break
    result = SR - SL
    # print(f"result : {result}")

print(abs(result))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
a = 0
b = 0
for i in range(len(A)):
    if S/2 >= sum(A[:i]):
        a = sum(A[:i])
    else:
        b = sum(A[:i])
        break
print(min(abs(S-a*2),abs(S-b*2)))

Submission status: Time Limit Exceeded

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


def loop():
    diff = 2020202020 * 200000 + 1
    tot = sum(A)
    for i in range(N):
        temp1 = A[:i+1]
        # temp2 = A[i+1:]
        temp3 = abs(tot - 2 * sum(temp1))
        if temp3 <= diff:
            # mid = i
            diff = temp3

    print(diff)

loop()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = list(map(int, input().split()))
s = []
c = 100000000000

for i in range(N):
    a = l[:i]
    b = l[i:]
    d = abs(sum(a)-sum(b))
    if d < c:
        c = d
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
l = A.pop(0)
r = A.pop(-1)

for i in range(n-2):
  if r<l:
    r+=A.pop(-1)
  else:
    l+=A.pop(0)
  
print(abs(r-l))

Submission status: Time Limit Exceeded

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

s = 0
S = 0

for i in range(n):
    s = S
    S += ls_a[i]
    if S > sum(ls_a)/2:
        break

print(int(min(abs(sum(ls_a)/2 - s)*2, abs(sum(ls_a)/2 - S)*2)))

Submission status: Time Limit Exceeded

Solution code:
def Solve (N, A):
    l = []
    for i in range(N//3, N//2+1):
        l.append(abs(sum(A[:i]) - sum(A[i:])))
        l.append(abs(sum(A[N-i:]) - sum(A[:N-i])))
    return min(l)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a=list(map(int,input().split()))
ans=2020202020
for i in range(1,N):
    mae=sum(a[:i])
    ato=sum(a)-mae
    ans=min(ans,abs(mae-ato))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
costs = [0 for x in range(N-1)]

mem = [0 for x in range(N-1)]
def calcsum(i):
  if mem[i] == 0:
    mem[i] == calcsum(i - 1) + A[i-1]
  return mem[i]

for i in range(1, N):
  costs[i-1] = abs(total - 2 * sum(A[:i]))
  
print(min(costs))

Submission status: Time Limit Exceeded

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

ans=3*(10**9)
for i in range(N-1):
  l=B[i]
  D=abs(l-(sum(A)-l))
  if ans > D:
    ans=D
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a=list(map(int,input().split()))
gos=[0]*(N-1)
ans=max(a)
for i in range(1,N):
    mae=sum(a[:i])
    ato=sum(a)-mae
    ans=min(ans,abs(mae-ato))
print(ans)

Submission status: Time Limit Exceeded

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


S = sum(A)
mp = S/2

i = 1

while True:
    L = A[0:i]
    R = A[i:N]
    SL = sum(L)
    SR = sum(R)
    # print(L, end = "\t\t")
    # print(R)
    if SL >= SR:
        result = min(result, SL - SR)
        break
    result = SR - SL
    # print(f"result : {result}")
    i += 1

print(abs(result))

Submission status: Time Limit Exceeded

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

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

# mid = N // 2

# print(A[:mid])
# print(A[mid:])


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

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



# print(left)
# print(right)
# if N % 2 == 1:
#     if right > left:
#         left += A[i]
#         i += 1
#     else:
#         right += A[-1 - i]

# print(left)
# print(right)

print(abs(left-right))

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input().split()))[0]
A = list(map(int, input().split()))
def get_result(ary):
    d_old = -1
    for i in range(0, len(ary)):
        left = sum(ary[:i])
        right = sum(ary[i:])
        d = abs(left - right)
        if d > d_old and d_old > -1:
            return d_old
        else:
            d_old = d
    return d_old
print(get_result(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a=list(map(int,input().split()))
gos=[0]*(N-1)
for i in range(1,N):
    mae=sum(a[:i])
    ato=sum(a)-mae
    gos[i-1]=(abs(mae-ato))
print(min(gos))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

diff = 2020202020 * 200000 + 1

tot = sum(A)

for i in range(N):
    temp1 = A[:i+1]
    # temp2 = A[i+1:]
    temp3 = abs(tot - 2 * sum(temp1))
    if temp3 <= diff:
        # mid = i
        diff = temp3

print(diff)

Submission status: Time Limit Exceeded

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

for i in range(N):
    a = l[:i]
    b = l[i:]
    d = abs(sum(a)-sum(b))
    s.append(d)
print(min(s))

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

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

# pref = list(accumilate(a))

tab = [0] * n
for i in range(n):
	tab[i] = abs(sum(a[:i+1]) - sum(a[i+1:])) 
print(min(tab))

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
mm=[int(s) for s in input().split()]
total=sum(mm)


count=0
for j,i in enumerate(mm):
  count+=i
  if count>=total//2:
    flag=j
    break
p=0
q=sum([mm[i+1] for i in range(flag,len(mm)-1)])
while True:
  if count>q:
    q+=1
    p+=1
  else:
    break


mm.reverse()

count=0
for j,i in enumerate(mm):
  count+=i
  if count>=total//2:
    flag=j
    break
p2=0
q=sum([mm[i+1] for i in range(flag,len(mm)-1)])
while True:
  if count>q:
    q+=1
    p2+=1
  else:
    break

if p>=p2:
  print(p2)
else:
  print(p)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a=list(map(int,input().split()))
gos=[]
for i in range(1,N):
    mae=sum(a[:i])
    ato=sum(a)-mae
    gos.append((abs(mae-ato)))
print(min(gos))

Submission status: Time Limit Exceeded

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

leftSum = 0
rightSum = 0
K = N

for i in range(0,N):
    if(leftSum < rightSum):
        leftSum = leftSum + Ai.pop(0)
        K = K-1
    else:
        rightSum = rightSum + Ai.pop(K-1)
        K = K-1
print(abs(leftSum-rightSum))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    *a, = map(int, input().split())
    o = float("inf")
    for i in range(1, n):
        l, r = sum(a[:i]), sum(a[i:])
        tmp = max(l, r) - min(l, r)
        o = tmp if tmp<o else o
    print(o)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
cost = 0
a_diff = A.sum()

for i in range(1, N):
    a_left = A[:i].sum()
    a_right = A[i:].sum()
    if abs(a_left - a_right) < a_diff:
        a_diff = abs(a_left - a_right)

print(a_diff)

Submission status: Time Limit Exceeded

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

costs = [0 for x in range(N-1)]
for i in range(1, N):
  costs[i-1] = abs(sum(A[:i]) - sum(A[i:]))
  
print(min(costs))

Submission status: Time Limit Exceeded

Solution code:
def Solve (N, A):
    l = []
    for i in range(1, N//2+1):
        l.append(abs(sum(A[:i]) - sum(A[i:])))
        l.append(abs(sum(A[N-i:]) - sum(A[:N-i])))
    return min(l)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

A_sum = 0

for i in range(N):
    A_sum += A[i]

min = A_sum

for i in range(N):
    AA_sum = 0
    for j in range(i):
        AA_sum += A[j]
    AA_sa = 2*AA_sum - A_sum
    if AA_sa < 0:
        AA_sa = -1 * AA_sa
    if AA_sa < min:
        min = AA_sa
print(min)

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):
    if ans > abs(sum(a[i:]) - sum(a[:i])):
        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()))
l = A.pop(0)
r = A.pop(-1)
while len(A)>0:
  if r<l:
    r+=A.pop(-1)
  else:
    l+=A.pop(0)
print(abs(r-l))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

diff = 2020202020 * 200000 + 1

for i in range(N):
    temp1 = A[:i+1]
    temp2 = A[i+1:]
    temp3 = abs(sum(temp2) - sum(temp1))
    if temp3 <= diff:
        mid = i
        diff = temp3

print(diff)

Submission status: Time Limit Exceeded

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

import itertools

ans=3*(10**9)
for i in range(N-1):
  l=list(itertools.accumulate(A))[i]
  D=abs(l-(sum(A)-l))
  if ans > D:
    ans=D

print(ans)