質問の単調なスタックは、テンプレートを知りません。。。
簡略アナログ変換と複雑な計算の成功、単調性を一旦スタック要素の取り出しと単調になるまで計算されて破壊され、スタック要素が増分されることを確実にするために重要であると思いました。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
const int mx = 1e6 + 50;
const int inf = 1e9;
inline void fre(){ freopen("sign1.in", "r", stdout); freopen("sign1.out", "w", stdout); }
ll st[mx], top, w[mx], a[mx];
int n;
int main(){
//fre();
while(scanf("%d", &n) == 1 && n) {
top = 0;
ll ans = 0;
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
a[n+1] = 0;
for(int i = 1; i <= n+1; i++) {
if(a[i] > st[top]) st[++top] = a[i], w[top] = 1;
else{
int width = 0;
while(a[i] <= st[top] && top){
width += w[top];
ans = max(ans, (ll)width * st[top]);
top--;
}
st[++top] = a[i]; w[top] = width+1;
}
}
cout << ans << endl;
}
return 0;
}