提交时间:2024-10-16 12:56:59

运行 ID: 33612

#include<bits/stdc++.h> // #define int long long #define uLL unsigned long long using namespace std; const int maxn=2e6+7; int n; int h[maxn]; int LG[maxn]; int STmax[maxn][22]; inline void GetLG(){ LG[0]=-1; for(int i=1;i<maxn;i++)LG[i]=LG[i>>1]+1; } inline void GetST(){ for(int i=1;i<=n;i++)STmax[i][0]=h[i]; for(int j=1;j<=LG[n];j++){ for(int i=1;i<=n-(1<<j)+1;i++){ STmax[i][j]=max(STmax[i][j-1],STmax[i+(1<<j-1)][j-1]); } } } inline int Qmax(int l,int r){ int t=LG[r-l+1]; return max(STmax[l][t],STmax[r-(1<<t)+1][t]); } int L[maxn],R[maxn]; stack<int> stk; void STD(){ uLL ans=0; for(int i=1;i<=n;i++){ while(!stk.empty()&&h[stk.top()]>h[i])R[stk.top()]=i-1,stk.pop(); stk.push(i); } while(!stk.empty()){ R[stk.top()]=n; stk.pop(); } for(int i=n;i>=1;i--){ while(!stk.empty()&&h[stk.top()]>h[i])L[stk.top()]=i+1,stk.pop(); stk.push(i); } while(!stk.empty()){ L[stk.top()]=1; stk.pop(); } for(int i=1;i<=n;i++){ ans=max(ans,1ull*h[i]*Qmax(L[i],R[i])*(R[i]-L[i]+1)); } cout<<ans<<endl; } signed main(){ // freopen("a.in","r",stdin); // freopen("a.out","w",stdout); cin>>n; for(int i=1;i<=n;i++)cin>>h[i]; GetLG();GetST(); STD(); // cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl; }