#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i).first;
    A.at(i).second = i;
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  long long ans = 0;
  long long tmpmin = 0;
  long long tmpmax = N - 1;
  for (long long i = 0; i < N; i++) {
    if (A.at(i).second - tmpmin < tmpmax - A.at(i).second) {
      ans += (tmpmax - A.at(i).second) * A.at(i).first;
      tmpmax -= 1;
    } else if (A.at(i).second - tmpmin > tmpmax - A.at(i).second) {
      ans += (A.at(i).second - tmpmin) * A.at(i).first;
      tmpmin++;
    } else {
      ans += (A.at(i).second - tmpmin) * A.at(i).first;
      tmpmin++;
    }
  }
  cout << ans << endl;
}
