#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n, x;
unordered_map<int, int> mp;
int stkl[N], stkr[N], ttl, ttr;
int indexl[N], indexr[N];
int main() {
while (cin >> n, n) {
mp.clear();
ttl = ttr = 0;
for (int i = 1; i <= n; i++) {
cin >> x;
mp[i] = x;
}
for (int i = 1; i <= n; i++) {
while (ttl && mp[stkl[ttl]] >= mp[i]) ttl--;
if (ttl) indexl[i] = stkl[ttl];
//如果ttl=0,说明左边所有高度都大于它,那么左边界就记为0
else indexl[i] = 0;
stkl[++ttl] = i;
}
for (int i = n; i > 0; i--) {
while (ttr && mp[stkr[ttr]] >= mp[i]) ttr--;
if (ttr) indexr[i] = stkr[ttr];
//如果ttr=0,说明右边所有高度都大于它,那么有边界就记为n+1
else indexr[i] = n + 1;
stkr[++ttr] = i;
}
ll res = 0;
for (int i = 1; i <= n; i++) {
//注意数据范围,要转化成ll
ll s = (ll) mp[i] * (indexr[i] - indexl[i] - 1);
res = max(s, res);
}
cout << res << endl;
}
return 0;
}