#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int main(){
while(1)
{
int h[N]={0},w[N]={0},n,a[N]={0},w1[N]={0},ans=0,tot=0;
scanf("%d",&n);
if(n==0)break;
for(int i=1;i<=n;i++)
{
scanf("%d",h+i);
w[i]=1;
if(h[i]>h[a[tot]])a[++tot]=i;
else
{
while(h[i]<=h[a[tot]])
{
w[i]+=w[a[tot]];
tot--;
}
a[++tot]=i;
}
}
tot=0;
a[0]=0;
for(int i=n;i>0;i--)
{
w1[i]=1;
if(h[i]>h[a[tot]])a[++tot]=i;
else
{
while(h[i]<=h[a[tot]])
{
w1[i]+=w1[a[tot]];
tot--;
}
a[++tot]=i;
}
ans=max(ans,h[i]*(w[i]+w1[i]-1));
}
cout<<ans<<endl;
}
return 0;
}