#include <bits/stdc++.h>
using namespace std;
void run_case() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > a;
  for (int i = 0; i < n; ++i) {
    long long t;
    cin >> t;
    a.emplace_back(t, 1LL * i);
  }
  long long l = 0, r = n - 1;
  sort(a.begin(), a.end(),
       [&](pair<long long, long long> a, pair<long long, long long> b) {
         return a.first > b.first;
       });
  long long ans = 0;
  for (auto i : a) {
    if (abs(i.second - r) < abs(i.second - l)) {
      ans += abs(i.second - l) * i.first;
      l++;
    } else {
      ans += abs(i.second - r) * i.first;
      r--;
    }
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cout.flags(ios::fixed);
  cout.precision(9);
  run_case();
  cout.flush();
  return 0;
}
