//题意,给一堆相邻的长方形,求切割出的最大的矩形面积。
//经典的dp
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100005;
int l[maxn], r[maxn], h[maxn]; //分别记录i位置左右高度小于h[i]的最远位置
int main(){
//freopen("1.txt", "r", stdin);
int i, n;
while(scanf("%d", &n)&&n){
long long ans=0;
for(i=0; i<n; i++)
scanf("%d", &h[i]);
for(i=0; i<n; i++){
l[i]=i;
while(l[i]>=1&&h[l[i]-1]>=h[i]){
l[i]=l[l[i]-1];
}
}
for(i=n-1; i>=0; i--){
r[i]=i;
while(r[i]<n-1&&h[r[i]+1]>=h[i]){
r[i]=r[r[i]+1];
}
}
for(i=0; i<n; i++){
if((long long)(h[i])*(r[i]-l[i]+1)>ans)
ans=(long long)(h[i])*(r[i]-l[i]+1);
}
printf("%lld\n", ans);
}
return 0;
}
poj 2082 Terrible Sets
//几乎一样
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=50005;
int l[maxn], r[maxn], h[maxn], w[maxn], s[maxn];
int main(){
//freopen("1.txt", "r", stdin);
int i, n;
while(scanf("%d", &n)&&n!=-1){
int ans=0;
for(i=0; i<n; i++)
scanf("%d%d", &w[i], &h[i]);
s[0]=w[0];
for(i=1; i<n; i++)s[i]=s[i-1]+w[i];
for(i=0; i<n; i++){
l[i]=i;
while(l[i]>=1&&h[l[i]-1]>=h[i]){
l[i]=l[l[i]-1];
}
}
for(i=n-1; i>=0; i--){
r[i]=i;
while(r[i]<n-1&&h[r[i]+1]>=h[i]){
r[i]=r[r[i]+1];
}
}
for(i=0; i<n; i++){
if((h[i])*(s[r[i]]-s[l[i]]+w[l[i]])>ans)
ans=(h[i])*(s[r[i]]-s[l[i]]+w[l[i]]);
}
printf("%d\n", ans);
}
return 0;
}