#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
bool f(string s) {
  bool ans = true;
  for (int i = 0; i < s.size() / 2; ++i) {
    if (s[i] != s[s.size() / 2 + i]) {
      ans = false;
      break;
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int num;
  cin >> N;
  vector<tuple<int, int>> vec(N);
  for (int idx = 0; idx < N; ++idx) {
    cin >> num;
    vec[idx] = make_tuple(num, idx);
  }
  sort(vec.begin(), vec.end());
  int A[N];
  int B[N];
  for (int i = 0; i < N; ++i) {
    A[i] = get<1>(vec[i]);
    B[i] = get<0>(vec[i]);
  }
  int right = N - 1;
  int left = 0;
  int ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (abs(right - A[i]) >= abs(left - A[i])) {
      ans += abs(right - A[i]) * B[i];
      right -= 1;
    } else {
      ans += abs(left - A[i]) * B[i];
      left += 1;
    }
  }
  cout << ans << endl;
}
