#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  vector<pair<long long, long long>> num(n);
  long long left = 0;
  long long right = n - 1;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    num[i] = make_pair(a, i);
  }
  sort((num).begin(), (num).end());
  reverse((num).begin(), (num).end());
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long x = num[i].second;
    long long k = num[i].first;
    if (abs(left - x) >= abs(right - x)) {
      ans += k * (abs(left - x));
      left++;
    } else {
      ans += k * (abs(right - x));
      right--;
    }
  }
  cout << ans << endl;
  return 0;
}
