显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int n;
ll a[N],ans;
struct made{
ll wid,hig;
};
stack<made>st;
void first_(){
ans = 0;
while(!st.empty())st.pop();
memset(a,0,sizeof(a));
}
int main(){
freopen("histogram.in","r",stdin);
freopen("histogram.out","w",stdout);
while(scanf("%d",&n) && n){
first_();
for(int i = 1;i <= n;i++)scanf("%lld",&a[i]);
a[n+1] = 0;
for(int i = 1;i <= n+1;i++){
ll width = 0;
while(!st.empty() && a[i] <= st.top().hig){
width += st.top().wid;
ans = max(ans,width*st.top().hig);
st.pop();
}
width++;
st.push((made){width,a[i]});
}
printf("%lld\n",ans);
}
return 0;
}