#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1L << 60;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  vector<vector<ll>> dp(2, vector<ll>(n + 1, -INF));
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[0][1] = 0;
  dp[1][1] = 0;
  for (int i = 0; i < (n); ++i) {
    dp[0][i + 1] = max(dp[0][i], dp[1][i]);
    ll tmpMax = 0;
    for (int j = 0; j < (i); ++j) {
      int diff = a[i] - a[j];
      for (int k = 0; k < (2); ++k) {
        ll tmp = dp[k][j] + a[j] * abs(diff) + a[i] * abs(diff);
        tmpMax = max(tmpMax, tmp);
      }
    }
    dp[1][i + 1] = tmpMax;
  }
  cout << max(dp[0][n], dp[1][n]) << endl;
  return 0;
}
