#include<cstdio>
#include<vector>
typedef long long i64;
const int R=5000000,N=100007;
char buf[R+3],*ptr=buf-1;
int _(){
int x=0,c=*++ptr;
while(c<48)c=*++ptr;
while(c>47)x=x*10+c-48,c=*++ptr;
return x;
}
int n,a[N],pv[N],pw[10007],c[N];
i64 f[N];
std::vector<int>q[10007];
void maxs(i64&a,i64 b){if(a<b)a=b;}
i64 p2(i64 x){return x*x;}
double F(int x,int y,int ci,int ai){
return (f[x-1]-f[y-1]+ai*(p2(c[x])-p2(c[y])))/double(i64(c[x]-c[y])*ai<<1);
}
int main(){
fread(buf,1,R,stdin);
n=_();
for(int i=1;i<=n;++i){
pv[i]=pw[a[i]=_()];
c[i]=c[pw[a[i]]]+1;
pw[a[i]]=i;
}
for(int i=1;i<=n;++i){
int w=a[i],ci=c[i]+1;
std::vector<int>&Q=q[w];
while(Q.size()>1&&F(i,Q[Q.size()-1],ci,w)>F(Q[Q.size()-1],Q[Q.size()-2],ci,w))Q.pop_back();
Q.push_back(i);
int l=0,r=Q.size()-1;
while(l+4<=r){
int m1=l+r>>1,m2=m1+r>>1;
if(f[Q[m1]-1]+w*p2(ci-c[Q[m1]])<f[Q[m2]-1]+w*p2(ci-c[Q[m2]]))l=m1;
else r=m2;
}
for(int p=l;p<=r;++p)maxs(f[i],f[Q[p]-1]+w*p2(ci-c[Q[p]]));
}
printf("%lld",f[n]);
return 0;
}