#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n, x;
  cin >> n;
  map<long long int, vector<long long int> > m;
  for (long long int i = 1; i <= n; i++) {
    cin >> x;
    m[x].push_back(i);
  }
  long long int ans = 0;
  long long int left = 1, right = n;
  long long int flag = 0;
  for (auto it = m.rbegin(); it != m.rend(); it++) {
    vector<long long int> v = m[it->first];
    if (v.size() > 1) flag = 1;
    for (auto x : v) {
      long long int diff1 = abs(left - x);
      long long int diff2 = abs(right - x);
      if (diff1 < diff2) {
        ans += it->first * diff2;
        right--;
      } else if (diff1 > diff2) {
        ans += it->first * diff1;
        left++;
      } else {
        ans += it->first * diff1;
        left++;
      }
    }
  }
  if (flag) ans++;
  cout << ans << "\n";
  return 0;
}
