#include <bits/stdc++.h>
int main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  for (auto& itr : a) {
    int tmp;
    std::cin >> tmp;
    itr = tmp;
  }
  std::vector<int> index(n);
  std::iota(index.begin(), index.end(), 0);
  std::sort(index.begin(), index.end(),
            [&](int x, int y) { return a[x] > a[y]; });
  std::vector<std::vector<int> > dp(n + 1, std::vector<int>(n + 1));
  int ans = 0;
  for (int l = 0; l < n; ++l) {
    for (int r = 0; r < n; ++r) {
      if (l + r == n) {
        ans = std::max(ans, dp[l][r]);
        break;
      }
      dp[l + 1][r] =
          std::max(dp[l + 1][r],
                   dp[l][r] + std::abs(l - index[l + r]) * a[index[l + r]]);
      dp[l][r + 1] =
          std::max(dp[l][r + 1], dp[l][r] + std::abs(n - r - 1 - index[l + r]) *
                                                a[index[l + r]]);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
