#include <bits/stdc++.h>
using lint = long long;
void solve() {
  int n;
  std::cin >> n;
  std::vector<lint> xs(n);
  for (auto& x : xs) std::cin >> x;
  auto calc = [&](int k) {
    lint cost = 0;
    std::vector<lint> ls(k);
    std::copy(xs.begin(), xs.begin() + k, ls.begin());
    for (int i = 0; i < k; ++i) cost -= ls[i] * i;
    std::sort(ls.begin(), ls.end());
    for (int i = 0; i < k; ++i) cost += ls[i] * (n - k + i);
    return cost;
  };
  lint ans = 0;
  for (int k = 0; k <= n; ++k) {
    lint cost = calc(k);
    std::reverse(xs.begin(), xs.end());
    cost += calc(n - k);
    std::reverse(xs.begin(), xs.end());
    ans = std::max(ans, cost);
  }
  std::cout << ans << std::endl;
}
int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  solve();
  return 0;
}
