#include <bits/stdc++.h>
const unsigned int M = 1000000007;
using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int, greater<int>> mp;
  int a[n + 1], l = 1, r = n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[a[i]] = i;
  }
  int ans = 0;
  for (auto it : mp) {
    if (abs(it.second - l) >= abs(it.second - r)) {
      ans += it.first * (abs(it.second - l));
      l++;
    } else {
      ans += it.first * (abs(it.second - r));
      r--;
    }
  }
  cout << ans << endl;
}
