#include <bits/stdc++.h>
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
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;
    MinHeap<lint> heap;
    int l = 0;
    for (int r = n - k; r < n; ++r) {
      while (l < k && l <= r) {
        cost -= xs[l] * l;
        heap.push(xs[l++]);
      }
      cost += heap.top() * r;
      heap.pop();
    }
    return cost;
  };
  lint ans = 0;
  for (int k = 0; k <= n; ++k) {
    lint cost = 0;
    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;
}
