// Author: Adrian Kuegel
// Algorithm: randomized greedy (not optimal)
// Complexity: O(n^2)
// Date: June 10'th, 2009

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <algorithm>
using namespace std;

#define MAXN 128

int c[MAXN], p[MAXN], ps[MAXN + 1];
int dp[MAXN+1][MAXN+1];
int c2[MAXN], ind[MAXN], save[MAXN];

int main() {
	int n;
//	freopen("elias.in", "r", stdin);
	while(scanf("%d", &n) == 1 && n) {
		assert(n > 0 && n <= MAXN);
		int l = 0, total = 0;
		for (int i=0; i<n; ++i) {
			scanf("%d", &c[i]);
			assert(c[i] >= 0 && c[i] <= 100000);
			if (c[i] > 0) {
				p[l++] = i;
				total += c[i] * (l + i + 1);
			}
		}
		int save_total = total;
		for (int i=0; i<n; ++i)
			c2[i] = c[i];
		int best = total;
		for (int iter=0; iter<100; ++iter) {
			l = 0;
			for (int i=0; i<n; ++i) {
				c[i] = c2[i];
				if (c[i] > 0)
					p[l++] = i;
			}
			total = save_total;
			while(1) {
				ps[n] = 0;
				for (int i=n-1; i>=0; --i)
					ps[i] = c[i] + ps[i+1];
				int plen = 0;
				for (int i=0; i+1<l; ++i) {
					int j = p[i];
					int next = p[i+1];
					save[i] = ps[next] - c[j] * (next - j);
					if (save[i] > 0) {
						ind[plen] = i;
						++plen;
						if (iter == 0)
							break;
					}
				}
				if (!plen)
					break;
				int bestind = ind[rand() % plen];
				if (iter == 1) {
					bestind = ind[plen-1];
				}
				else if (iter == 2) {
					for (int i=0; i<plen; ++i)
						if (save[ind[i]] > save[bestind])
							bestind = ind[i];
				}
				total -= save[bestind];
				c[p[bestind+1]] += c[p[bestind]];
				c[p[bestind]] = 0;
				for (int i=bestind+1; i<l; ++i)
					p[i-1] = p[i];
				--l;
			}
			if (total < best)
				best = total;
		}
		printf("%d\n", best);
	}
	return 0;
}
