#include<bits/stdc++.h>
using namespace std;
struct s{
int sum,xs;
int lsum,rsum,x1,x2;
int maxarr;
} tree[300000]={0};
void merge(int ind)
{
s l,r;
l= tree[2*ind+1];
r= tree[2*ind+2];
int a= max(l.lsum*l.x1,(l.sum+r.lsum)*(l.xs+r.x1));
if(a==l.sum*l.x1)
{
tree[ind].lsum= l.lsum;
tree[ind].x1= l.x1;
}
else {tree[ind].lsum= l.sum+r.lsum;
tree[ind].x1=(l.xs+r.x1);
}
int b= max(r.rsum*r.x2,(l.rsum+r.sum)*(r.xs+l.x2));
if(b==r.rsum*r.x2)
{
tree[ind].rsum= r.rsum;
tree[ind].x2= r.x2;
}
else{
tree[ind].rsum= l.rsum+ r.sum;
tree[ind].x2= r.xs+l.x2;
}
tree[ind].sum= l.sum+r.sum;
tree[ind].xs= l.xs+r.xs;
tree[ind].maxarr= max(tree[2*ind+1].maxarr,max(tree[2*ind+2].maxarr,(l.rsum+r.lsum)*(l.x2+r.x1)));
}
void build(int st,int en,int ind,int *arr)
{
int mid= (st+en)/2;
if(st==en)
{
tree[ind].maxarr=tree[ind].rsum=tree[ind].lsum=tree[ind].sum= arr[st];
tree[ind].x1=tree[ind].x2=tree[ind].xs=1;
return;
}
build(st,mid,2*ind+1,arr);
build(mid+1,en,2*ind+2,arr);
merge(ind);
}
int main()
{
int n;
cin>>n;
int arr[100000];
for(int i=0;i<n;i++)cin>>arr[i];
build(0,n-1,0,arr);
//for(int i=0;i<2*n-1;i++)cout<<tree[i].maxarr<<" ";cout<<endl;
cout<<tree[0].maxarr<<endl;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3Qgc3sKICAgIGludCBzdW0seHM7CiAgICBpbnQgbHN1bSxyc3VtLHgxLHgyOwogICAgaW50IG1heGFycjsKfSB0cmVlWzMwMDAwMF09ezB9Owp2b2lkIG1lcmdlKGludCBpbmQpCnsKICAgIHMgbCxyOwogICAgbD0gdHJlZVsyKmluZCsxXTsKICAgIHI9IHRyZWVbMippbmQrMl07CiAgICAKICAgaW50IGE9IG1heChsLmxzdW0qbC54MSwobC5zdW0rci5sc3VtKSoobC54cytyLngxKSk7CiAgICBpZihhPT1sLnN1bSpsLngxKQogICAgewogICAgICAgIHRyZWVbaW5kXS5sc3VtPSBsLmxzdW07CiAgICAgICAgdHJlZVtpbmRdLngxPSBsLngxOwogICAgfQogICAgZWxzZSB7dHJlZVtpbmRdLmxzdW09IGwuc3VtK3IubHN1bTsKICAgIHRyZWVbaW5kXS54MT0obC54cytyLngxKTsKICAgIH0KICAgIAogICAgaW50IGI9IG1heChyLnJzdW0qci54MiwobC5yc3VtK3Iuc3VtKSooci54cytsLngyKSk7CiAgICBpZihiPT1yLnJzdW0qci54MikKICAgIHsKICAgICAgICB0cmVlW2luZF0ucnN1bT0gci5yc3VtOwogICAgICAgIHRyZWVbaW5kXS54Mj0gci54MjsKICAgIH0KICAgIGVsc2V7CiAgICAgICAgdHJlZVtpbmRdLnJzdW09IGwucnN1bSsgci5zdW07CiAgICAgICAgdHJlZVtpbmRdLngyPSByLnhzK2wueDI7CiAgICB9CiAgICAKICAgIHRyZWVbaW5kXS5zdW09IGwuc3VtK3Iuc3VtOwogICAgdHJlZVtpbmRdLnhzPSBsLnhzK3IueHM7CiAgICAKICAgIHRyZWVbaW5kXS5tYXhhcnI9IG1heCh0cmVlWzIqaW5kKzFdLm1heGFycixtYXgodHJlZVsyKmluZCsyXS5tYXhhcnIsKGwucnN1bStyLmxzdW0pKihsLngyK3IueDEpKSk7CiAgICAKfQp2b2lkIGJ1aWxkKGludCBzdCxpbnQgZW4saW50IGluZCxpbnQgKmFycikKewogICAgaW50IG1pZD0gKHN0K2VuKS8yOwogICAgaWYoc3Q9PWVuKQogICAgewogICAgICAgIHRyZWVbaW5kXS5tYXhhcnI9dHJlZVtpbmRdLnJzdW09dHJlZVtpbmRdLmxzdW09dHJlZVtpbmRdLnN1bT0gYXJyW3N0XTsKICAgICAgICB0cmVlW2luZF0ueDE9dHJlZVtpbmRdLngyPXRyZWVbaW5kXS54cz0xOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGJ1aWxkKHN0LG1pZCwyKmluZCsxLGFycik7CiAgICBidWlsZChtaWQrMSxlbiwyKmluZCsyLGFycik7CiAgICBtZXJnZShpbmQpOwp9CmludCBtYWluKCkKewogICAgaW50IG47CiAgICBjaW4+Pm47CiAgICBpbnQgYXJyWzEwMDAwMF07CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKWNpbj4+YXJyW2ldOwogICAgYnVpbGQoMCxuLTEsMCxhcnIpOwogICAgLy9mb3IoaW50IGk9MDtpPDIqbi0xO2krKyljb3V0PDx0cmVlW2ldLm1heGFycjw8IiAiO2NvdXQ8PGVuZGw7CiAgICBjb3V0PDx0cmVlWzBdLm1heGFycjw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cg==