#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second > b.second);
}
int large(int x, map<int, int> g, int n) {
  int y = x;
  for (int i = 0; i < n; i++) {
    if (i == x) continue;
    if (g[i] == 1)
      continue;
    else if (g[i] == 0) {
      if (abs(x - i) > abs(x - y)) y = i;
    }
  }
  return y;
}
int main() {
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++) v.push_back(make_pair(i, a[i]));
    sort(v.begin(), v.end(), sortbysec);
    int mh = 0;
    map<int, int> g;
    for (int i = 0; i < n; i++) {
      int y = large(v[i].first, g, n);
      g[y]++;
      mh += v[i].second * abs(v[i].first - y);
    }
    map<int, int> m1;
    for (int i = 0; i < n; i++) m1[a[i]]++;
    for (int i = 0; i < n; i++) {
      if ((m1[v[i].second]) > 1) {
        cout << mh + 1;
        return 0;
      }
    }
    cout << mh;
    cout << endl;
  }
  return 0;
}
