import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n; readV(n);
  long[] a; readA(n, a);

  auto as = a.cumulativeSum;
  auto dp = new long[][](n, n+1);

  long f(int i, int j)
  {
    if (i+1 == j) return 0;
    if (dp[i][j]) return dp[i][j];

    auto r = 10L^^18;
    foreach (k; i+1..j)
      r = min(r, f(i, k) + f(k, j) + as[i..j]);

    return dp[i][j] = r;
  }

  writeln(f(0, n));
}

class CumulativeSum(T)
{
  size_t n;
  T[] s;

  this(T[] a)
  {
    n = a.length;
    s = new T[](n+1);
    s[0] = T(0);
    foreach (i; 0..n) s[i+1] = s[i] + a[i];
  }

  T opSlice(size_t l, size_t r) { return s[r]-s[l]; }
  size_t opDollar() { return n; }
}
auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }
