// Author: Walter Guttmann
#include <fstream>
#include <iostream>

using namespace std;

//ifstream in("elias.in");
#define in cin

const int infty = 1<<30;

int c[256], ps[256];
int dp[256][256];
// dp[i][j] = minimum code length for problem c[0]..c[i-1] using j groups

int main() {
  int n;
  while (in >> n) {
    if (n == 0) break;
    ps[0] = 0;
    int first = -1;
    for (int i=0 ; i<n ; i++) {
      in >> c[i];
      ps[i+1] = ps[i] + c[i];
      if (first < 0 && c[i] > 0)
        first = i;
    }
    if (first < 0) {
      cout << 0 << endl;
      continue;
    }
    for (int i=0 ; i<=n ; i++)
      for (int j=0 ; j<=n ; j++)
        if (i==0)
          dp[i][j] = 0;
        else if (j==0)
          dp[i][j] = first<i ? infty : 0;
        else {
          dp[i][j] = infty;
          for (int k=0 ; k<i ; k++)
            dp[i][j] = min(dp[i][j], min(dp[k][j-1] + (j+i)*(ps[i]-ps[k]), infty));
        }
    int result = infty;
    for (int j=0 ; j<=n ; j++)
      result = min(result, dp[n][j]);
    cout << result << endl;
  }
  return 0;
}

