#include <bits/stdc++.h>
using namespace std;
long long power(long long x, unsigned long long y, long long p) {
  long long res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long abs(long long a, long long b) {
  if (a > b) return a - b;
  return b - a;
}
void solve(long long a) {
  int n;
  cin >> n;
  vector<pair<long long, int> > p;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    p.push_back(make_pair(temp, i));
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  int l = 0, r = n - 1;
  long long ans = 0;
  bool dnl = false;
  for (int i = 0; i < n; i++) {
    if (abs(p[i].second, l) > abs(p[i].second, r)) {
      ans += abs(p[i].second, l++) * p[i].first;
      if (dnl) r--;
      dnl = false;
    } else if (abs(p[i].second, l) < abs(p[i].second, r)) {
      ans += abs(p[i].second, r--) * p[i].first;
      if (dnl) l++;
      dnl = false;
    } else {
      ans += abs(p[i].second, r) * p[i].first;
      dnl = true;
    }
  }
  cout << ans;
}
int main() {
  int t = 1;
  while (t--) {
    solve(power(10, 100, 1000000007));
  }
}
