#include <bits/stdc++.h>
using namespace std;
long long arr[666666];
long long n;
struct str {
  long long num;
  long long id;
  friend bool operator<(str a, str b) {
    return a.num * (max(abs(a.id - 1), abs(a.id - n))) <
           b.num * (max(abs(b.id - 1), abs(b.id - n)));
  }
};
priority_queue<str> q;
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
    q.push((str){arr[i], i});
  }
  long long l = 1, r = n, ans = 0;
  while (!q.empty()) {
    long long x = q.top().num;
    long long y = q.top().id;
    q.pop();
    if (abs(l - y) >= abs(r - y)) {
      ans += x * abs(l - y);
      l++;
    } else {
      ans += x * abs(r - y);
      r--;
    }
  }
  cout << ans;
  return 0;
}
