#include <bits/stdc++.h>
using namespace std;
long long N, ans;
pair<long long, long long> a[200005];
void doLft() {
  while (1) {
    bool f = 0;
    for (long long i = 1; i < N; i++) {
      long long j = i + 1;
      long long vl =
          a[i].first * abs(a[i].second - i) + a[j].first * abs(a[j].second - j);
      long long vl2 =
          a[i].first * abs(a[i].second - j) + a[j].first * abs(a[j].second - i);
      if (vl2 > vl) {
        f = 1;
        swap(a[i], a[i + 1]);
      }
    }
    if (!f) break;
  }
}
void doRgt() {
  while (1) {
    bool f = 0;
    for (long long i = N; i > 1; i--) {
      long long j = i - 1;
      long long vl =
          a[i].first * abs(a[i].second - i) + a[j].first * abs(a[j].second - j);
      long long vl2 =
          a[i].first * abs(a[i].second - j) + a[j].first * abs(a[j].second - i);
      if (vl2 > vl) {
        f = 1;
        swap(a[i], a[i + 1]);
      }
    }
    if (!f) break;
  }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (long long i = 1; i <= N; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  for (int i = 1; i <= 4; i++) {
    doLft();
    doRgt();
  }
  for (long long i = 1; i <= N; i++) ans += a[i].first * abs(a[i].second - i);
  cout << ans << "\n";
  return 0;
}
