f[i][j] = max{
f[i][j - 1] + a[j],
f[i + 1][j] + a[i],
f[i + k][j] + abs(a[i] - a[i + k - 1]) * k | 1 < k < j - i + 1,
f[i][j - k] + abs(a[j] - a[j - k + 1]) * k | 1 < k < j - i + 1
}
#include <iostream>
int a[105];
int mem[105][105];
int f(int, int);
int max(int, int);
int abs(int);
int main(){
int n;
std::cin >> n;
for(int i = 0;i < n;i++){
std::cin >> a[i];
}
std::cout << f(0, n - 1);
return 0;
}
int f(int i, int j){
if(i == j){
return a[i];
}
if(mem[i][j]) return mem[i][j];
int ans = max(f(i, j - 1) + a[j], f(i + 1, j) + a[i]);
for(int k = 2;k <= j - i;k++){
ans = max(ans, f(i + k, j) + abs(a[i] - a[i + k - 1]) * k);
ans = max(ans, f(i, j - k) + abs(a[j] - a[j - k + 1]) * k);
}
return mem[i][j] = ans;
}
int max(int a, int b) {return a > b ? a : b;}
int abs(int n) {return n < 0 ? -n : n;}