#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
unsigned long long pow(unsigned long long a, unsigned long long p) {
  if (p == 1) {
    return a % M;
  }
  if (p % 2 == 0) {
    unsigned long long temp = pow(a, p / 2);
    return (temp * temp) % M;
  }
  return ((pow(a, p - 1) % M) * a) % M;
}
int main() {
  int n;
  cin >> n;
  int a[n], b[n], c[n];
  int l = 0, r = n - 1;
  int index = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n, greater<int>());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[j] == b[i]) {
        index = j;
      }
    }
    int temp = a[index];
    a[index] = -1;
    if (abs(index - l) > abs(r - index)) {
      ans += (abs(index - l) * temp);
      c[l] = temp;
      l++;
    } else {
      ans += (abs(r - index) * temp);
      c[r] = temp;
      r--;
    }
  }
  cout << ans << "\n";
  return 0;
}
