#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  vector<bool> moved(N);
  vector<bool> occ(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    vector<int> max_indexes;
    long long max_val = -1;
    for (int j = 0; j < N; j++) {
      if (!moved[j] && max_val < A[j]) {
        max_val = A[j];
      }
    }
    for (int j = 0; j < N; j++) {
      if (!moved[j] && A[j] == max_val) {
        max_indexes.push_back(j);
      }
    }
    long long max_hap = -1;
    int occ_index = -1;
    int max_index = -1;
    for (int k = 0; k < max_indexes.size(); k++) {
      for (int j = 0; j < N; j++) {
        if (!occ[j] && max_hap < (long long)abs(max_indexes[k] - j)) {
          max_index = max_indexes[k];
          max_hap = (long long)abs(max_index - j);
          occ_index = j;
        }
      }
    }
    occ[occ_index] = true;
    moved[max_index] = true;
    ans += max_hap * A[max_index];
  }
  cout << ans << endl;
  return 0;
}
