#include <cstdio>
#include <algorithm>
#define re register
#define int long long
using namespace std;
const int MAXN=1e4+5,MAXX=6e6,INF=1e18;
int a[MAXN],d[MAXN],n,dp[MAXX+1000],s[MAXN];
inline int read(){
char ch=getchar();int x=0;
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x;
}
signed main(){
n=read();a[1]=read();
for(re int i=2;i<=n;++i){
a[i]=read();
d[i-1]=a[i]-a[i-1];
}
sort(d+1,d+n);
for(re int i=1;i<=MAXX;++i) dp[i]=INF;
dp[0]=0;s[0]=0;int mx=0;
for(re int i=1;i<n;++i){
s[i]=s[i-1]+d[i];
if(d[i]==0) continue;
for(re int x=mx;x>=0;--x){
if(dp[x]==INF) continue;
dp[x+id[i]]=min(dp[x+id[i]],dp[x]+id[i]d[i]+2d[i]x);
dp[x+s[i]]=min(dp[x+s[i]],dp[x]+s[i]s[i]);
dp[x]=INF;
mx=max(max(x+id[i],x+s[i]),mx);
}
}
int ans=INF;
for(re int x=0;x<=mx;++x){
if(dp[x]!=INF) ans=min(ans,ndp[x]-xx);
}
printf("%lld",ans);
return 0;
}