#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int mv = n / 2;
  map<int, deque<int>> pos;
  priority_queue<int> q;
  int a[2005];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    q.push(a[i]);
    pos[a[i]].push_back(i);
  }
  int ans = 0, mn = 0, mx = n - 1;
  while (mv > 0) {
    int now = q.top();
    q.pop();
    int nwpos = pos[now].front();
    pos[now].pop_front();
    if (nwpos >= n / 2) {
      int dif = nwpos - mn;
      ans += now * dif;
      ans += a[mn] * dif;
      pos[a[mn]].pop_front();
      mn++;
    } else {
      int dif = mx - nwpos;
      ans += now * dif;
      ans += a[mx] * dif;
      pos[a[mx]].pop_front();
      mx--;
    }
    mv--;
  }
  cout << ans;
}
