#include <vector>
#include <algorithm>
using namespace std;
typedef long long lld;
class Solution {
public:
lld largestRectangleArea(vector<lld> &height) {
lld len= height.size();
vector<lld> left(len);
vector<lld> right(len);
for(lld i= 0; i< len; ++i){
if(i == 0){
left[i]= i;
}else{
if(height[i]>height[i-1]){
left[i]= i;
}else{
left[i]= left[i-1];
while(left[i]>= 0 && height[left[i]]>=height[i]){
--left[i];
}
++left[i];
}
}
}
for(lld i= len-1; i>= 0; --i){
if(i==len-1){
right[i]=i;
}else{
if(height[i]>height[i+1]){
right[i]=i;
}else{
right[i]=right[i+1];
while(right[i]<= len-1 && height[right[i]]>=height[i]){
++right[i];
}
--right[i];
}
}
}
lld x= 0;
for(lld i= 0; i< len; ++i){
x= max(x,(right[i]-left[i]+1)*height[i]);
}
return x;
}
};
#include <iostream>
int main(){
lld n;
Solution foo;
while(cin>>n && n){
vector<lld> c;
lld x;
for(lld i= 0; i< n; ++i){
cin>>x;
c.push_back(x);
}
cout<<foo.largestRectangleArea(c)<<endl;
}
}
#include <algorithm>
using namespace std;
typedef long long lld;
class Solution {
public:
lld largestRectangleArea(vector<lld> &height) {
lld len= height.size();
vector<lld> left(len);
vector<lld> right(len);
for(lld i= 0; i< len; ++i){
if(i == 0){
left[i]= i;
}else{
if(height[i]>height[i-1]){
left[i]= i;
}else{
left[i]= left[i-1];
while(left[i]>= 0 && height[left[i]]>=height[i]){
--left[i];
}
++left[i];
}
}
}
for(lld i= len-1; i>= 0; --i){
if(i==len-1){
right[i]=i;
}else{
if(height[i]>height[i+1]){
right[i]=i;
}else{
right[i]=right[i+1];
while(right[i]<= len-1 && height[right[i]]>=height[i]){
++right[i];
}
--right[i];
}
}
}
lld x= 0;
for(lld i= 0; i< len; ++i){
x= max(x,(right[i]-left[i]+1)*height[i]);
}
return x;
}
};
#include <iostream>
int main(){
lld n;
Solution foo;
while(cin>>n && n){
vector<lld> c;
lld x;
for(lld i= 0; i< n; ++i){
cin>>x;
c.push_back(x);
}
cout<<foo.largestRectangleArea(c)<<endl;
}
}