#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  deque<long long> dq;
  map<long long, long long> m;
  for (int i = 0; i < n; i++) {
    dq.push_back(i + 1);
  }
  priority_queue<pair<long long, long long>> pq;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    pq.push({x, i + 1});
    m[x]++;
  }
  long long ans = 0;
  while (!pq.empty()) {
    pair<int, int> p = pq.top();
    pq.pop();
    long long lcost = abs(dq.front() - p.second) * p.first;
    long long rcost = abs(dq.back() - p.second) * p.first;
    if (lcost > rcost) {
      dq.pop_front();
      ans += lcost;
    } else {
      dq.pop_back();
      ans += rcost;
    }
  }
  if (m.size() < n) ans++;
  cout << ans;
  return 0;
}
