#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  map<int, vector<int>> m;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    m[temp].push_back(i);
  }
  int front = 0, back = n - 1;
  long long ans = 0;
  for (auto it = m.rbegin(); it != m.rend(); it++) {
    for (auto j : it->second) {
      if (abs(j - back) >= abs(j - front)) {
        ans += it->first * abs(j - back);
        back--;
      } else {
        ans += it->first * abs(j - front);
        front++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
