#include <bits/stdc++.h>
int cmpnum(const void *n1, const void *n2) {
  if (*(long long *)n1 < *(long long *)n2) {
    return 1;
  } else if (*(long *)n1 > *(long *)n2) {
    return -1;
  } else {
    return 0;
  }
}
int max(long long int n1, int long long n2) {
  if (n1 < n2) {
    return n2;
  } else {
    return n1;
  }
}
int main() {
  long long int N;
  typedef struct {
    long long int A;
    long long int number;
  } person;
  scanf("%lld", &N);
  person person1[N];
  for (long long int i = 0; i < N; ++i) {
    scanf("%lld", &person1[i].A);
    person1[i].number = i;
  }
  qsort(person1, sizeof(person1) / sizeof(person1[0]), sizeof(person), cmpnum);
  long long int DP[N + 10][N + 10];
  long long int max_s = 0;
  DP[0][0] = 0;
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= N; ++j) {
      DP[i][j] = 0;
    }
  }
  for (long long int i = 0; i < N; ++i) {
    for (long long int j = 0; i + j + 1 <= N; ++j) {
      DP[i + 1][j] =
          max(DP[i + 1][j],
              DP[i][j] + person1[i + j].A * (person1[i + j].number - i));
      DP[i][j + 1] =
          max(DP[i][j + 1], DP[i][j] + person1[i + j].A *
                                           (N - 1 - j - person1[i + j].number));
    }
  }
  for (int i = 0; i <= N; ++i) {
    if (DP[i][N - i] > max_s) {
      max_s = DP[i][N - i];
    }
  }
  printf("%lld\n", max_s);
  return 0;
}
int D_4_19() {
  long long int N;
  long long int K;
  long long int S1 = 0;
  long long int S2 = 0;
  long long int SUM = 0;
  long long int MOD = 1000000007;
  scanf("%lld %lld", &N, &K);
  for (long long int k = K; k <= N + 1; ++k) {
    S1 = ((k) * (k - 1) / 2);
    S2 = (k * (N + N - k + 1) / 2);
    SUM += S2 - S1 + 1;
    SUM = SUM % MOD;
  }
  printf("%lld\n", SUM);
  return 0;
}
