#!/usr/bin/env python2





import os

import sys

from atexit import register

from io import BytesIO



range = xrange



sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))

sys.stdout = BytesIO()

register(lambda: os.write(1, sys.stdout.getvalue()))



input = lambda: sys.stdin.readline().rstrip('\r\n')



n = int(eval(input()))

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



pref_sums = [0] * (n + 1)

for i in range(n):

    pref_sums[i + 1] = pref_sums[i] + a[i]



dp = [[0] * n for _ in range(n)]





def solve(start, end):

    if (dp[start][end] == 0) and (start != end):

        dp[start][end] = min(pref_sums[i + 1] - pref_sums[start] + pref_sums[end + 1] - pref_sums[i + 1] +

                             solve(start, i) + solve(i + 1, end) for i in range(start, end))

    return dp[start][end]





print(solve(0, n - 1))

#!/usr/bin/env python2





import os

import sys

from atexit import register

from io import BytesIO



range = xrange



sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))

sys.stdout = BytesIO()

register(lambda: os.write(1, sys.stdout.getvalue()))



input = lambda: sys.stdin.readline().rstrip('\r\n')



n = int(eval(input()))

a = [float(i) for i in input().split()]



pref_sums = [0.0] * (n + 1)

for i in range(n):

    pref_sums[i + 1] = pref_sums[i] + a[i]



dp = [[0.0] * n for _ in range(n)]





def solve(start, end):

    if (dp[start][end] == 0.0) and (start != end):

        dp[start][end] = min(

            solve(start, i) + solve(i + 1, end) + pref_sums[end + 1] - pref_sums[start] for i in range(start, end))

    return dp[start][end]





print(int(solve(0, n - 1)))

#!/usr/bin/env python2
import os
import sys
from atexit import register
from io import BytesIO

range = xrange
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip("\r\n")
n = int(eval(input()))
a = [int(i) for i in input().split()]
pref_sums = [0] * (n + 1)
for i in range(n):
    pref_sums[i + 1] = pref_sums[i] + a[i]
dp = [[0] * n for _ in range(n)]


def solve(start, end):
    if (dp[start][end] == 0) and (start != end):
        dp[start][end] = min(
            pref_sums[i + 1]
            - pref_sums[start]
            + pref_sums[end + 1]
            - pref_sums[i + 1]
            + solve(start, i)
            + solve(i + 1, end)
            for i in range(start, end)
        )
    return dp[start][end]


print(solve(0, n - 1))

#!/usr/bin/env python2
import os
import sys
from atexit import register
from io import BytesIO

range = xrange
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip("\r\n")
n = int(eval(input()))
a = [float(i) for i in input().split()]
pref_sums = [0.0] * (n + 1)
for i in range(n):
    pref_sums[i + 1] = pref_sums[i] + a[i]
dp = [[0.0] * n for _ in range(n)]


def solve(start, end):
    if (dp[start][end] == 0.0) and (start != end):
        dp[start][end] = min(
            solve(start, i) + solve(i + 1, end) + pref_sums[end + 1] - pref_sums[start]
            for i in range(start, end)
        )
    return dp[start][end]


print(int(solve(0, n - 1)))

-a = [int(i) for i in input().split()] -pref_sums = [0] * (n + 1) +a = [float(i) for i in input().split()] +pref_sums = [0.0] * (n + 1) -dp = [[0] * n for _ in range(n)] +dp = [[0.0] * n for _ in range(n)] -    if (dp[start][end] == 0) and (start != end): +    if (dp[start][end] == 0.0) and (start != end): -            pref_sums[i + 1] -            - pref_sums[start] -            + pref_sums[end + 1] -            - pref_sums[i + 1] -            + solve(start, i) -            + solve(i + 1, end) +            solve(start, i) + solve(i + 1, end) + pref_sums[end + 1] - pref_sums[start] -print(solve(0, n - 1)) +print(int(solve(0, n - 1)))