#include <bits/stdc++.h>
using namespace std;
void p_() { cout << "\n"; }
template <class Head, class... Body>
void p_(Head head, Body... body) {
  cout << head << ",";
  p_(body...);
}
int main(int argc, char const *argv[]) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  vector<long long> sum_l(N, 0), sum_r(N, 0);
  sum_l[0] = A[0];
  sum_r[N - 1] = A[N - 1];
  for (int i = 1; i < N; ++i) {
    sum_l[i] = sum_l[i - 1] + A[i];
    sum_r[N - 1 - i] = sum_r[N - i] + A[N - 1 - i];
  }
  vector<int> idxs(N);
  for (int i = 0; i < N; ++i) {
    idxs[i] = i;
  }
  sort(idxs.begin(), idxs.end(), [&](int x, int y) { return A[x] > A[y]; });
  int l_idx = 0;
  int r_idx = N - 1;
  vector<bool> used(N, false);
  long long point = 0;
  for (int idx : idxs) {
    used[idx] = true;
    long long point_l = A[idx] * (idx - l_idx);
    for (int i = idx + 1; i < N; ++i) {
      if (!used[i]) {
        point_l -= A[i];
      }
    }
    long long point_r = A[idx] * (r_idx - idx);
    for (int i = idx - 1; i >= 0; --i) {
      if (!used[i]) {
        point_r -= A[i];
      }
    }
    if (point_l >= point_r) {
      point += A[idx] * (idx - l_idx);
      l_idx += 1;
    } else {
      point += A[idx] * (r_idx - idx);
      r_idx -= 1;
    }
  }
  cout << point << endl;
  return 0;
}
