#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define N 1000005
#define LL long long
using namespace std;
int a[N],w[N],n,c[N],dt[20][N];
LL f[N],g[N],ans,d[N];
bool cmp(int x,int y)
{
return (a[x]<a[y])||(a[x]==a[y]&&x<y);
}
void doit(int l,int r,int t)
{
if(l==r) f[w[l]]=max(f[w[l]],a[w[l]]-(LL)(w[l]-1)*(w[l])/2),g[w[l]]=(LL)2*f[w[l]]-(LL)w[l]*w[l]-w[l],ans=max(ans,f[w[l]]-(LL)(n-w[l])*(n-w[l]+1)/2);
else
{
int mid=(l+r)/2;
doit(l,mid,t+1);
int j=l;
int x=1,y=0;
d[0]=d[1]=0;
fo(i,mid+1,r)
{
int p=dt[t][i];
while(dt[t][j]<p&&j<=mid)
{
LL q=dt[t][j];
while(x<y&&-(g[q]-g[d[y]])*(d[y]-d[y-1])<=-(g[d[y]]-g[d[y-1]])*(q-d[y])) y--;
d[++y]=q;
j++;
}
while(x<y&&-(g[d[x+1]]-g[d[x]])<=(LL)2*p*(d[x+1]-d[x])) x++;
if(x<=y&&x) f[p]=max(f[p],f[d[x]]+a[p]-(LL)(p-d[x]-1)*(p-d[x])/2);
g[p]=(LL)2*f[p]-p*p-p;
ans=max(ans,f[p]-(LL)(n-p)*(n-p+1)/2);
}
doit(mid+1,r,t+1);
}
}
void gsort(int l,int r,int t)
{
if(l==r) dt[t][l]=w[l];
else
{
int mid=(l+r)/2;
gsort(l,mid,t+1),gsort(mid+1,r,t+1);
int x=l,y=mid+1,le=l-1;
while(x<=mid&&y<=r)
{
if(dt[t+1][x]<dt[t+1][y]) dt[t][++le]=dt[t+1][x++];
else dt[t][++le]=dt[t+1][y++];
}
while(x<=mid) dt[t][++le]=dt[t+1][x++];
while(y<=r) dt[t][++le]=dt[t+1][y++];
}
}
int main()
{
cin>>n;
fo(i,1,n) scanf("%d",&a[i]),w[i]=i,f[i]=-1e15;
sort(w+1,w+n+1,cmp);
f[0]=0;
gsort(1,n,0);
ans=-(LL)n*(LL)(n+1)/2;
doit(1,n,1);
printf("%lld\n",ans);
}