#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1000000007;
constexpr long long INF = numeric_limits<long long>::max() / 2;
using Graph = vector<vector<long long>>;
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  for (long long i = 0; i < (long long)(N); i++) {
    cin >> A[i].first;
    A[i].second = i;
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  long long left = 0, right = N - 1;
  long long ans = 0;
  for (long long i = 0; i < (long long)(N); i++) {
    if (abs(left - A[i].second) > abs(right - A[i].second)) {
      ans += abs(left - A[i].second) * A[i].first;
      left++;
    } else {
      ans += abs(right - A[i].second) * A[i].first;
      right--;
    }
  }
  cout << ans << endl;
}
