#include<bits/stdc++.h>
using namespace std;
int a[100010],a2[100010],b2[100010],c[100010];
stack<int> q1,q2,w1,w2;
int main(){
int n;
cin>>n;
for(int i = 1;i<=n;i++){
int x;
cin>>x;
a[i] = x;
a2[i] = a2[i - 1] + x;
}
for(int i = n;i>=1;i--){
b2[i] = b2[i + 1] + a[i];
}
for(int i=1;i<=n + 1;i++){
while(!q1.empty() && q1.top() > a[i]){
c[q2.top()] += (a2[i - 1] - a2[q2.top()]) * q1.top();
q1.pop();
q2.pop();
}
q1.push(a[i]);
q2.push(i);
}
for(int i=n;i>=0;i--){
while(!w1.empty() && w1.top() > a[i]){
c[w2.top()] += (b2[i + 1] - b2[w2.top() - 1]) * w1.top();
w1.pop();
w2.pop();
}
w1.push(a[i]);
w2.push(i);
}
int mx = 0;
for(int i=1;i<=n;i++){
mx = max(mx,c[i]);
}
cout<<mx;
return 0;
}
实在改不出来了(qwq)