#include <bits/stdc++.h>
using namespace std;
constexpr auto INF = 2000000000;
constexpr auto MOD = 1000000007;
long long sum(int i) {
  long long x = i * ((long long)i + 1) / 2;
  return x % MOD;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  priority_queue<int, vector<int>, less<int>> q;
  for (int i = 0; i < (n); i++) {
    int k;
    cin >> k;
    a[i] = k;
    q.push(k);
  }
  long long ans = 0;
  int l = 0, r = n - 1;
  for (int i = 0; i < (n); i++) {
    int t = find((a).begin(), (a).end(), q.top()) - a.begin();
    q.pop();
    int next = find((a).begin(), (a).end(), q.top()) - a.begin();
    if (abs(r - t) > abs(t - l)) {
      ans += (long long)abs(r - t) * a[t];
      r--;
    } else if (abs(r - t) < abs(t - l)) {
      ans += (long long)abs(t - l) * a[t];
      l++;
    } else if (abs(r - next) < abs(next - l)) {
      ans += (long long)abs(r - t) * a[t];
      r--;
    } else if (abs(r - next) > abs(next - l)) {
      ans += (long long)abs(t - l) * a[t];
      l++;
    }
  }
  cout << ans << endl;
}
