#include <bits/stdc++.h>
using namespace std;
template <class T>
void print_vect(std::vector<T> v) {
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
bool func_to_create() {
  uint32_t N;
  std::vector<std::pair<uint64_t, int32_t> > A;
  cin >> N;
  for (int32_t i = 0; i < N; i++) {
    uint64_t a;
    cin >> a;
    A.push_back(std::pair<uint64_t, int32_t>(a, i));
  }
  uint64_t ans = 0;
  int32_t left = 0, right = N - 1;
  while (left <= right) {
    uint64_t max_increase = 0;
    int left_or_right = 0;
    int32_t swap_index = 0;
    for (int32_t i = left; i <= right; i++) {
      if (A[i].first * (A[i].second - left) > max_increase) {
        max_increase = A[i].first * abs(A[i].second - left);
        left_or_right = 1;
        swap_index = i;
      }
      if (A[i].first * (right - A[i].second) > max_increase) {
        max_increase = A[i].first * abs(right - A[i].second);
        left_or_right = 0;
        swap_index = i;
      }
    }
    ans += max_increase;
    if (left_or_right) {
      A[swap_index] = A[left];
      left++;
    } else {
      A[swap_index] = A[right];
      right--;
    }
  }
  cout << ans << endl;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  func_to_create();
  return 0;
}
