#include <bits/stdc++.h>
using namespace std;
int num[3030], tmp[3030];
long long play(int first, int a) {
  long long ans = 0;
  for (int i = (1); i <= (a); ++i)
    tmp[i] = num[i],
    ans += (long long)tmp[i] * ((i <= first) ? (a - i) : (i - 1));
  sort(tmp + 1, tmp + 1 + first);
  sort(tmp + 1 + first, tmp + a + 1);
  for (int i = (1); i <= (first); ++i) ans -= (long long)tmp[i] * (first - i);
  for (int i = (first + 1); i <= (a); ++i) ans -= (long long)tmp[i] * (a - i);
  return ans;
}
void permute(int a) {
  vector<int> first(a);
  iota((first).begin(), (first).end(), 1);
  long long r = 0;
  do {
    long long ans = 0;
    for (int i = (1); i <= (a); ++i) ans += num[i] * (abs(first[i - 1] - i));
    if (r < ans) {
      printf("=%d\n", ans);
      for (int i = (0); i <= (a - 1); ++i) printf("%d ", first[i]);
      printf("\n");
      r = ans;
    }
  } while (next_permutation((first).begin(), (first).end()));
}
int main() {
  int i, j, k, l, a, b, c, d;
  scanf("%d", &a);
  for (int i = (1); i <= (a); ++i) {
    scanf("%d", &num[i]);
  }
  long long ans = 0;
  for (int i = (1); i <= (a); ++i) ans = max(ans, play(i, a));
  printf("%lld", ans);
  return 0;
}
