#include <bits/stdc++.h>
using namespace std;
using lli = int_fast64_t;
using ulli = uint_fast64_t;
constexpr lli MOD = (lli)1e9 + 7;
constexpr lli LIMIT = 1000000000000000000 * 5;
int main() {
  int N;
  cin >> N;
  vector<lli> v(N);
  vector<bool> done(N, false);
  set<int> se;
  for (int i = 0; i < N; i++) {
    cin >> v[i];
    se.insert(v[i]);
  }
  lli left = 0;
  lli right = N - 1;
  lli ans = 0;
  if (se.size() != N) ans++;
  for (int i = 0; i < N; i++) {
    lli index = -1;
    lli val = -1;
    for (int j = 0; j < N; j++) {
      if (!done[j]) {
        if (val < v[j]) {
          index = j;
          val = v[j];
        }
      }
    }
    if (abs(index - left) > ((left + right) / 2)) {
      ans += abs(index - left) * val;
      done[index] = true;
      left++;
    } else {
      ans += abs(index - right) * val;
      done[index] = true;
      right--;
    }
  }
  cout << ans << endl;
  return 0;
}
