CodeEff/ECCO huggingface dataset:
# Original solution
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(eval(input()))
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())
atoz = [chr(i) for i in range(97,97+26)]

A,B = inpl()
if A<=8 and B<=8:
	print('Yay!')
else:
	print(':(')


# Optimized solution
A,B = list(map(int, input().split()))
if A<=8 and B<=8:
	print('Yay!')
else:
	print(':(')
# Original solution
A,B = list(map(int,input().split()))
if max(A,B) <= 8:
    print("Yay!")
else:
    print(":(")

# Optimized solution
A, B = list(map(int,input().split()))
if A <= 8 and B <= 8:
    print("Yay!")
else:
    print(":(")
# Original solution
import functools

import os

INF = float('inf')


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


def inpf():
    return float(input())


def inps():
    return input()


def inl():
    return list(map(int, input().split()))


def inlf():
    return list(map(float, input().split()))


def inls():
    return input().split()


def debug(fn):
    if not os.getenv('LOCAL'):
        return fn

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        print('DEBUG: {}({}) -> '.format(
            fn.__name__,
            ', '.join(
                list(map(str, args)) +
                ['{}={}'.format(k, str(v)) for k, v in kwargs.items()]
            )
        ), end='')
        ret = fn(*args, **kwargs)
        print(ret)
        return ret

    return wrapper


a, b = inl()
print('Yay!' if a*2 <= 16 and b*2 <= 16 else ':(')


# Optimized solution
import os
import sys

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

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


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

ok = A <= 8 and B <= 8
if ok:
    print('Yay!')
else:
    print(':(')
# Original solution
#S=input()
#N=int(input())
A,B=list(map(int,input().split()))
if A<=8 and B<=8:
    print("Yay!")
else:
    print(":(")

# Optimized solution
A,B=list(map(int,input().split()))
if A<9 and B<9:
    print("Yay!")
else:
    print(":(")
# Original solution
a,b=list(map(int,input().split()))
print(("Yay!" if a<9 and b<9 else ":("))

# Optimized solution
print(("Yay!" if max(list(map(int,input().split())))<9 else ":("))
# Original solution
A,B = list(map(int,input().split()))
print(([":(","Yay!"][A<9 and B<9]))

# Optimized solution
if max(list(map(int,input().split())))<=8:
  print("Yay!")
else:
  print(":(")
# Original solution
a,b=list(map(int,input().split()))
if a<=8 and b<=8:
  print("Yay!")
else:
  print(":(")

# Optimized solution
print(("Yay!" if max(list(map(int,input().split())))<9 else ":("))
# Original solution
a, b = list(map(int, input().split()))

if a > 8 or b > 8:
    print(':(')
else:
    print('Yay!')


# Optimized solution
parameters = list(map(int, input().split()))

if(parameters[0] <= 8 and parameters[1] <= 8):
    print('Yay!')
else:
    print(':(')
# Original solution
A, B = list(map(int,input().split()))

if(A < 9 and B < 9):
  print("Yay!")
else:
  print(":(")

# Optimized solution
A, B = list(map(int,input().split()))
if(A <= 8 and B <= 8):
  print("Yay!")
else:
  print(":(")
# Original solution
A, B = list(map(int, input().split()))
ans = 'Yay!' if A <= 8 and B <= 8 else ':('
print(ans)


# Optimized solution
A, B = list(map(int, input().split()))
ans = 'Yay!' if max(A, B) <= 8 else ':('
print(ans)
# Original solution
A = [int(i) for i in input().split()]
if max(A) <= 8:
    print('Yay!')
else:
    print(':(')

# Optimized solution
A, B = list(map(int, input().split()))
print(('Yay!' if A <= 8 and B <= 8 else ':('))
# Original solution
A, B, C = list(map(int, input().split()))
if B + C <= A:
    print((0))
else:
    print((B + C - A))


# Optimized solution
A, B, C = list(map(int, input().split()))
# if B + C <= A:
#     print(0)
# else:
#     print(B + C - A)
print((max(0, B + C - A)))
# Original solution
temp=list(map(int, input().split()))
a=temp[2]-(temp[0]-temp[1])
if a>=0:
  print(a)
else:
  print((0))

# Optimized solution
temp=list(map(int, input().split()))

temp=temp[2]-(temp[0]-temp[1])

if temp<0:
  print((0))
else:
  print((int(temp)))
# Original solution
a, b, c = list(map(int, input().split()))
print((max(c-(a-b), 0)))

# Optimized solution
a, b, c = list(map(int, input().split()))
ans = max(c-(a-b), 0)
print(ans)
# Original solution
A, B, C = list(map(int, input().split()))

C -= A-B
if C < 0:
    C = 0

print(C)


# Optimized solution
A, B, C = list(map(int, input().split()))

d = min(A-B, C)
print((C-d))
# Original solution
a, b, c = list(map(int, input().split()))
print((max(0, c - min(c, a - b))))

# Optimized solution
a, b, c = list(map(int, input().split()))
print((c - min(c, a - b)))
# Original solution
def main():
    A, B, C = list(map(int, input().split()))

    ans = C - (A - B)

    print((max(ans, 0)))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    A, B, C = list(map(int, input().split()))

    t = min(A - B, C)
    ans = C - t

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
def slove():
    import sys
    input = sys.stdin.readline
    a, b, c = list(map(int, input().rstrip('\n').split()))
    print((max(c - (a - b), 0)))


if __name__ == '__main__':
    slove()


# Optimized solution
#1行に入力が複数の場合
a, b, c = list(map(int, input().split()))
#容器1に入れることができる水の量
can_ad_a = a - b
#aに追加で注げる水の量は
#「aに入れることができる水の量」、「cに入っている水の量」のうち小さい値
#「aに入れた水の量」を「c」から引けば「cに残る水の量」
#min(value, value)で最小値を取得
print((c - min(can_ad_a, c)))
# Original solution
a, b, c = list(map(int, input().split()))
tmp = a - b

if tmp - c >= 0:
    print((0))
else:
    print((c- tmp))

# Optimized solution
a, b, c = list(map(int, input().split()))
tmp = a - b

print((c - min(tmp, c)))
# Original solution
A,B,C = list(map(int, input().split()))
if A-B<=C:
  print((C-(A-B)))
else:
  print((0))

# Optimized solution
A, B, C = list(map(int, input().split()))

res1 = A - B

if res1 <= C:
    trans = res1
else:
    trans = C

print((C - trans))
# Original solution
a, b, c = list(map(int, input().split()))

x = min(a-b, c)
ans = c - x
print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines  

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

x = A-B
print((0 if C-x < 0 else C-x))
# Original solution
A, B, C = list(map(int, input().split()))
print((0 if A - B >= C else C - A + B))

# Optimized solution
A, B, C = list(map(int, input().split()))
if (A - B > C):
    print((0))
else:
    print((C - A + B))
# Original solution
A, B, C = [int(i) for i in input().split()]
print((C - min(A - B, C)))

# Optimized solution
A, B, C = list(map(int, input().split()))
print((max(C - A + B, 0)))
# Original solution
A,B,C=list(map(int,input().split()))
d=C-(A-B)
print((d if d>0 else 0))

# Optimized solution
A,B,C=list(map(int,input().split()))
print((max(0,C-A+B)))
# Original solution
A,B,C=list(map(int,input().split()))
print((max(0,C-A+B)))

# Optimized solution
A, B, C = list(map(int, input().split()))
water = C-(A-B)
print((water if water > 0 else 0))
# Original solution
A, B, C = list(map(int, input().split(' ')))
print((max(C - (A - B), 0)))


# Optimized solution
#!/usr/bin/env python3
import sys


def solve(A: int, B: int, C: int):
    print((max(C - (A - B), 0)))
    return



# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    solve(A, B, C)

if __name__ == '__main__':
    main()
# Original solution
a, b, c = list(map(int, input().split()))
if c-(a-b) <= 0:
    print((0))
else:
    print((c-(a-b)))

# Optimized solution
a, b, c = list(map(int, input().split()))
print((max(0,c-(a-b))))
# Original solution
A, B, C = list(map(int, input().split()))
D = B + C - A
print((max(0, D)))

# Optimized solution
A, B, C = list(map(int, input().split()))
print((max(0, B + C - A)))
# Original solution
A, B, C = list(map(int, input().split()))
if C > (A - B): print((C - (A - B)))
else: print((0))

# Optimized solution
import sys

def solve():
    input = sys.stdin.readline
    A, B, C = list(map(int, input().split()))
    print((max(0, C - A + B)))

    return 0

if __name__ == "__main__":
    solve()
# Original solution
N = list(map(int, input().split()))
a,b,c = N[0],N[1],N[2]
ans = c-(a-b)
if ans < 0:
    ans = 0
print(ans)

# Optimized solution
#一行数値　複数
a,b,c = list(map(int, input().split()))
ans = c-(a-b)
if ans < 0:
    ans = 0
print(ans)
# Original solution
import sys
input = sys.stdin.readline

a, b, c = list(map(int, input().split()))
ans =  b+c - a
if ans <= 0:
    ans = 0
print(ans)

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

a, b, c = list(map(int, read().split()))
print((max(0, c - (a - b))))
# Original solution
a, b, c = list(map(int, input().split()))

if a < b + c:
    print((b+c-a))
else:
    print((0))

# Optimized solution
a,b,c = list(map(int, input().split()))

print((max(0, c-(a-b))))
# Original solution
a,b,c=list(map(int,input().split()));print((max(0,c-a+b)))

# Optimized solution
a,b,c = list(map(int,input().split()))
d = a - b
c -= min(c, d)
print(c)
# Original solution
N = int(eval(input()))
a = list(map(int, input().split()))

def A(i, j):
    if i == j:
        return 0
    elif j == i + 1:
        return sum(a[i:i+2])
    else:
        S = sum(a[i:j+1])
        return min([A(i, k) + A(k+1, j) for k in range(i, j)]) + S

print((A(0, N-1)))

# Optimized solution
N = int(eval(input()))
a = list(map(int, input().split()))

A = [[0 for __ in range(N)] for _ in range(N)]
S = [[0 for __ in range(N)] for _ in range(N)]

for i in range(N-1):
    for j in range(i, N):
        S[i][j] = sum(a[i:j+1])

for w in range(1, N):
    for i in range(N):
        j = i + w
        if j >= N:
            break
        
        A[i][j] = min([
            A[i][k] + A[k+1][j] for k in range(i, j)
        ]) + S[i][j]

print((A[0][-1]))
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))

    dp = [[float("inf")]*(N-i+1) for i in range(N+1)]
    dp[1] = [0] * N
    for i in range(2, N+1):
        for j in range(N-i+1):
            for k in range(1, i):
                dp[i][j] = min(dp[k][j] + dp[i-k][j+k] +
                               sum(A[j:j+i]), dp[i][j])
    print((dp[N][0]))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))

    cumsum = [[0]*(N-i+1) for i in range(N+1)]

    dp = [[float("inf")]*(N-i+1) for i in range(N+1)]
    dp[1] = [0] * N
    cumsum[1] = A
    for i in range(2, N+1):
        for j in range(N-i+1):
            cumsum[i][j] = cumsum[i-1][j+1] + A[j]
            for k in range(1, i):
                dp[i][j] = min(dp[k][j] + dp[i-k][j+k] +
                               cumsum[i][j], dp[i][j])
    print((dp[N][0]))


if __name__ == "__main__":
    main()
# Original solution
import sys
sys.setrecursionlimit(10**9)

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

memo = [[float('inf')]*410 for _ in range(410)]
visit = [[False]*410 for _ in range(410)]

from itertools import accumulate
B = [0]+A
B = list(accumulate(B))

def dp(l, r):
  if l == r:
    return 0
  if visit[l][r]:
    return memo[l][r]
  visit[l][r] = True
  res = float('inf')
  for c in range(l, r):
    res = min(res, dp(l, c)+dp(c+1, r)+B[r+1]-B[l])
  memo[l][r] = res
  return res

print((dp(0, n-1)))

# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))

from itertools import accumulate
B = [0] + A
B = list(accumulate(B))

INF = 10**18
dp = [[INF]*(n+1) for i in range(n+1)]
# dp[i][j]: 区間[i, j)のスライムを取り除いたコストの最小値
for i in range(n):
    dp[i][i+1] = 0
# 区間の幅(d)が狭い方から更新していく(d: 2, 3, ...n+1)
# d = 0, 1については初期化で更新した
for d in range(2, n+1):
    for i in range(n+1-d):
        j = i + d
        for k in range(i+1, j):
            dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]+B[j]-B[i])
#print(dp)
print((dp[0][n]))
# Original solution
#!/usr/bin/env python3
#EDPC N

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = [0] + list(accumulate(a))
dp = [[inf]*(n) for _ in range(n)]
for i in range(n):
    dp[i][i] = 0
for l in range(n-1)[::-1]:
    for r in range(l+1,n):
        s = b[r+1] - b[l]
        if r - l == 1:
            dp[l][r] = s
            continue
        for c in range(l,r):
            dp[l][r] = min(dp[l][r],s + dp[l][c] + dp[c+1][r])
print((dp[0][-1]))

# Optimized solution
#!/usr/bin/env python3
#EDPC N

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = [0] + list(accumulate(a))
dp = [[inf]*(n) for _ in range(n)]
for i in range(n):
    dp[i][i] = 0
for l in range(n-1)[::-1]:
    for r in range(l+1,n):
        s = b[r+1] - b[l]
        for c in range(l,r):
            dp[l][r] = min(dp[l][r],s + dp[l][c] + dp[c+1][r])
print((dp[0][-1]))
# Original solution
# 大きさがsum(a)のスライムが1匹いる
# dp[l][r]=min(区間[l,r]を分解するための最小コスト)
n=int(eval(input()))
a=list(map(int,input().split()))
INF=10**15
dp=[[0]*n for i in range(n)]
for i in range(n):
    dp[i][i]=a[i]
for i in range(n-1):
    for j in range(i+1,n):
        dp[i][j]=dp[i][j-1]+a[j]

for i in range(n-1):
    for j in range(i+1,n):
        ans=INF
        # print(j-i-1,j)
        for k in range(j-i-1,j):
            ans=min(ans,dp[j-i-1][k]+dp[k+1][j])
        if i<n-2:
            dp[j-i-1][j]+=ans
        else:
            print(ans)

# Optimized solution
# dp[l][r]=min(区間[l,r]を合成するための最小コスト)
n=int(eval(input()))
a=list(map(int,input().split()))
INF=10**15
dp=[[0]*n for i in range(n)]
for i in range(n):
    dp[i][i]=a[i]
# 合成するときの必要経費を構成
for i in range(n-1):
    for j in range(i+1,n):
        dp[i][j]=dp[i][j-1]+a[j]
dp[0][-1]=0
# dp[i][j]=min(dp[i][k]+dp[k+1][j])+必要経費 で斜め(0,1),(1,2),(0,2),(1,3),...の順に見ていく
# ループの書き方に手間取った
for i in range(n-1):
    for j in range(i+1,n):
        ans=INF
        x=j-i-1
        for k in range(x,j):
            ans=min(ans,dp[x][k]+dp[k+1][j])
        dp[x][j]+=ans
print((dp[0][-1]))
# Original solution
from itertools import accumulate

INF = float('inf')

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

accAs = [0] + list(accumulate(As))

dp = [[0] * (N) for _ in range(N)]
for num in range(2, N+1):
    for i in range(N-num+1):
        cost = INF
        for j in range(1, num):
            cost = min(cost, dp[i][i+j-1] + dp[i+j][i+num-1])
        dp[i][i+num-1] = accAs[i+num] - accAs[i] + cost

print((dp[0][N-1]))


# Optimized solution
from itertools import accumulate

def solve():
    INF = 10**15

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

    accAs = list(accumulate([0]+As))

    dp = [[INF]*(N) for _ in range(N)]
    for i in range(N):
        dp[i][i] = 0

    for W in range(2, N+1):
        for L in range(N-W+1):
            R = L+W-1
            dp[L][R] = min([dp[L][k-1] + dp[k][R] for k in range(L+1, R+1)]) + accAs[R+1] - accAs[L]

    ans = dp[0][-1]
    print(ans)


solve()
# Original solution
from itertools import accumulate

def solve():
    INF = 10**15

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

    accAs = list(accumulate([0]+As))

    dp = [[INF]*(N) for _ in range(N)]
    for i in range(N):
        dp[i][i] = 0

    for W in range(2, N+1):
        for L in range(N-W+1):
            R = L+W-1
            dp[L][R] = min([dp[L][k-1] + dp[k][R] for k in range(L+1, R+1)]) + accAs[R+1] - accAs[L]

    ans = dp[0][-1]
    print(ans)


solve()


# Optimized solution
from itertools import accumulate

def solve():
    INF = 10**15

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

    accAs = list(accumulate([0]+As))

    dp = [[INF]*(N) for _ in range(N)]
    for i in range(N):
        dp[i][i] = 0

    for L in reversed(list(range(N))):
        dpL = dp[L]
        for R in range(L+1, N):
            dpL[R] = min([dpL[k-1] + dp[k][R] for k in range(L+1, R+1)]) + accAs[R+1] - accAs[L]

    print((dp[0][-1]))


solve()
# Original solution
import math
from functools import lru_cache, reduce


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def prefix_sum(arr, a):
    arr.append(arr[-1]+a)
    return arr


def solve():
    """
    OPT[i][j] = min(OPT[i][k]+OPT[k+1][j])+sum(A[i:j+1])    i <= k < j
    OPT[i][j] = 0 if i == j
    """
    N = read_int()
    A = read_ints()
    prefix = reduce(prefix_sum, A, [0])

    @lru_cache(None)
    def dp(i, j):
        if i == j:
            return 0
        return min(dp(i, k)+dp(k+1, j)+(prefix[j+1]-prefix[i]) for k in range(i, j))

    return dp(0, N-1)


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


# Optimized solution
import math
from functools import lru_cache, reduce


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def prefix_sum(arr, a):
    arr.append(arr[-1]+a)
    return arr


def solve():
    """
    OPT[i][j] = min(OPT[i][k]+OPT[k+1][j])+sum(A[i:j+1])    i <= k < j
    OPT[i][j] = 0 if i == j
    """
    N = read_int()
    A = read_ints()
    prefix = reduce(prefix_sum, A, [0])

    @lru_cache(None)
    def dp(i, j):
        if i == j:
            return 0
        return min(dp(i, k)+dp(k+1, j)+(prefix[j+1]-prefix[i]) for k in range(i, j))

    OPT = [[0]*N for _ in range(N)]
    for j in range(N):
        for i in range(j-1, -1, -1):
            OPT[i][j] = min(OPT[i][k]+OPT[k+1][j] for k in range(i, j))+(prefix[j+1]-prefix[i])

    return OPT[0][N-1]


if __name__ == '__main__':
    print((solve()))
# Original solution
def main():
    N = int(eval(input()))
    a = list(map(int, input().split()))
    l = [[10**9+1] * (N+1) for _ in range(N+1)]
    s = [[0] * (N+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(N+1):
            s[i][j] = sum(a[k] for k in range(i, j))
    for i in range(N):
        l[i][i+1] = 0
    for j in range(2, N+1):
        for i in range(N):
            m = i + j
            if m > N:
                continue
            l[i][m] = min(l[i][k] + l[k][m] for k in range(i+1, m)) + s[i][m]
    return l[0][-1]

print((main()))


# Optimized solution
def main():
    N = int(eval(input()))
    a = list(map(int, input().split()))
    l = [[10**9+1] * (N+1) for _ in range(N+1)]
    s = [[0] * (N+1) for _ in range(N+1)]
    for i in range(N-1):
        for j in range(i+1, N+1):
            s[i][j] = s[i][j-1] + a[j-1]
    for i in range(N):
        l[i][i+1] = 0
    for j in range(2, N+1):
        for i in range(N):
            m = i + j
            if m > N:
                continue
            l[i][m] = min(l[i][k] + l[k][m] for k in range(i+1, m)) + s[i][m]
    return l[0][-1]

print((main()))
# Original solution
# coding: utf-8
from functools import lru_cache
from itertools import accumulate as accum
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

@lru_cache(maxsize = None)
def f(l, r):
    if l == r:
        return(0)
    cost = float('inf')
    for x in range(l, r):
        cost_ = f(l, x) + f(x + 1, r)
        if cost_ < cost:
            cost = cost_
    return(cost + acsum[r] - acsum[l - 1])

n = int(eval(input())) # 2 <= n <= 400
acsum = list(accum(list(map(int, input().split())))) + [0]
print((f(0, n - 1)))


# Optimized solution
# coding: utf-8

from itertools import accumulate as accum
import sys
input = sys.stdin.readline

def f2(n, a):
    acsum = list(accum(a)) + [0]
    dp = [[float('inf')] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = 0
        if i + 1 != n:
            dp[i][i + 1] = a[i] + a[i + 1]
    for i in range(n - 3, -1, -1):
        for j in range(i + 2, n):
            c = float('inf')
            dpi = dp[i]
            for x in range(i, j):
                c_ = dpi[x] + dp[x + 1][j]
                if c_ < c:
                    c = c_
            dpi[j] = c + acsum[j] - acsum[i - 1]
    return(dp[0][n - 1])

n = int(eval(input())) # 2 <= n <= 400
a = list(map(int, input().split()))
print((f2(n, a)))
# Original solution
from functools import lru_cache
N = int(eval(input()))
A = list(map(int, input().split()))
INF = 1.0e+100

@lru_cache(maxsize=None)
def f(l, r):
  if r - l == 1:
    return (A[l], 0)
  ret = (INF, INF)
  for i in range(l+1, r):
    (lr, lc) = f(l, i)
    (rr, rc) = f(i, r)
    if lr+rr+lc+rc < ret[1]:
      ret = (lr+rr, lr+rr+lc+rc)
  return ret

print((f(0, N)[1]))


# Optimized solution
from functools import lru_cache
N = int(eval(input()))
A = list(map(int, input().split()))

INF = 1.0e+100


dp = [[[None, INF] for _ in range(N+1)] for _ in range(N+1)]
for i in range(N):
    dp[i][i+1] = [A[i], 0]

for g in range(2, N+1):
    for l in range(N-g+1):
        r = l + g
        for i in range(l+1, r):
            lv, lc = dp[l][i]
            rv, rc = dp[i][r]
            nc = lv + rv + lc + rc
            if dp[l][r][1] > nc:
                dp[l][r][0] = lv + rv
                dp[l][r][1] = nc

print((dp[0][N][1]))
# Original solution
def f(n, a):
    dp = [[0] * n for _ in range(n)]
    for i in range(n - 2, -1, -1):
        s = a[i]
        dpi=dp[i]
        for j in range(i + 1, n):
            k = []
            s += a[j]
            for d in range(i, j):
                k += [dpi[d] + dp[d + 1][j]]
            dp[i][j] = min(k) + s
    return dp[0][-1]


n = int(eval(input()))
a = tuple(map(int, input().split()))
print((f(n, a)))


# Optimized solution
def f(n, a):
    inf = float("inf")
    dp = [[0] * n for _ in range(n)]
    for i in range(n - 2, -1, -1):
        s = a[i]
        dpi = dp[i]
        for j in range(i + 1, n):
            mn = inf
            s += a[j]
            for d in range(i, j):
                k = dpi[d] + dp[d + 1][j]
                if k < mn:
                    mn = k
            dpi[j] = mn + s
    return dp[0][-1]


n = int(eval(input()))
a = tuple(map(int, input().split()))
print((f(n, a)))