#include <cstdio>
using namespace std;
int f[101][101]={0};
int absint(int a)
{
if (a<0)
return(-a);
else
return(a);
}
int main(void)
{
freopen("remove.in","r",stdin);
freopen("remove.out","w",stdout);
int i=0,j=0,k=0,n=0,temp=0,a[101]={0};
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (j=1;j<=n;j++)
for (i=1;i<=n-j+1;i++)
{
if (a[i]+f[i+1][j-1]>a[i+j-1]+f[i][j-1])
f[i][j]=a[i]+f[i+1][j-1];
else
f[i][j]=a[i+j-1]+f[i][j-1];
for (k=i+1;k<=i+j-3;k++)
{
temp=f[i][k-i+1]/*+f[k+1][i+j-1-k]*/+absint(a[k+1]-a[i+j-1])*(i+j-1-k);
if (temp>f[i][j])
f[i][j]=temp;
temp=absint(a[i]-a[k])*(k-i+1)+/*f[i][k-i+1]+*/f[k+1][i+j-1-k];
if (temp>f[i][j])
f[i][j]=temp;
}
temp=absint(a[i]-a[i+j-1])*j;
if (temp>f[i][j])
f[i][j]=temp;
}
printf("%d\n",f[1][n]);
fclose(stdin);
fclose(stdout);
return(0);
}