#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<long long int> a(n);
  vector<pair<long long int, int>> v;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(make_pair(a[i] * (max(i, n - i - 1)), i));
  }
  long long int left = 0, right = n - 1;
  sort(v.rbegin(), v.rend());
  long long int res = 0;
  for (int i = 0; i < n; i++) {
    if (abs(v[i].second - left) >= abs(v[i].second - right)) {
      res += abs(v[i].second - left) * a[v[i].second];
      left++;
    } else {
      res += abs(v[i].second - right) * a[v[i].second];
      right--;
    }
  }
  printf("%lld\n", res);
}
