#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  long long int ans = 0;
  cin >> N;
  int max = N - 1, min = 0;
  vector<pair<long, int>> A(N);
  for (int i = 0; i < N; i++) {
    long tmp;
    cin >> tmp;
    A[i] = make_pair(tmp, i);
  }
  sort(A.begin(), A.end());
  for (int i = N - 1; i >= 0; i--) {
    if (max - A[i].second == A[i].second - min && max != min) {
      ans += A[i].first * (max - A[i].second);
      if (A[i - 1].second > A[i].second)
        max--;
      else
        min++;
    } else if (max - A[i].second >= A[i].second - min) {
      ans += A[i].first * (max - A[i].second);
      max--;
    } else {
      ans += A[i].first * (A[i].second - min);
      min++;
    }
  }
  cout << ans << endl;
  return 0;
}
