#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 20;
int n, a[N];
long long dp[N][N];
bool found[N][N];
void setval(int i, int j, long long val) {
  found[i][j] = true;
  dp[i][j] = val;
}
long long f(int i, int j) {
  if (found[i][j]) return dp[i][j];
  found[i][j] = true;
  dp[i][j] = INT_MIN;
  for (int pv = 0; pv < N; pv++) {
    long long money = f(i - 1, pv);
    if (pv < j) {
      if (money >= (j - pv) * a[i])
        dp[i][j] = max(dp[i][j], money - (j - pv) * a[i]);
    } else {
      dp[i][j] = max(dp[i][j], money + (pv - j) * a[i]);
    }
  }
  return dp[i][j];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < N; j++) found[i][j] = false;
  setval(0, 0, 1000);
  for (int j = 1; j < N; j++) setval(0, j, INT_MIN);
  long long ans = 0;
  for (int j = 0; j < N; j++) ans = max(ans, f(n, j));
  cout << ans;
}
