#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  vector<long long int> A;
  vector<long long int> V;
  for (int i = 0; i < N; i++) {
    long long int a;
    cin >> a;
    A.push_back(a);
    V.push_back(0);
  }
  long long int result = 0;
  for (int x = 0; x < N; x++) {
    long long int max_e;
    max_e = *std::max_element(A.begin(), A.end());
    bool firstflag = true;
    int begin = 0, end = N - 1;
    for (int y = 0; y < N; y++) {
      if (V[y] == 0 && firstflag) {
        firstflag = false;
        begin = y;
      } else if (V[y] != 0 && !firstflag) {
        end = y - 1;
        break;
      }
    }
    for (int y = 0; y < N; y++) {
      if (A[y] == max_e) {
        int max_distance, insert_pos;
        if (y - begin < end - y) {
          max_distance = end - y;
          insert_pos = end;
        } else {
          max_distance = y - begin;
          insert_pos = begin;
        }
        V[insert_pos] = A[y];
        result += max_distance * A[y];
        A[y] = 0;
        break;
      } else {
        continue;
      }
    }
  }
  cout << result << "\n";
  return 0;
}
