#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par, num;
  UnionFind(int N) : par(N), num(N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
      num[i] = 1;
    }
  }
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
    num[ry] = num[ry] + num[rx];
  }
  int get_num(int x) {
    int rx = root(x);
    return num[rx];
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
long long int modpow(long long int a, long long int n, long long int p) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 0) {
    long long int root = modpow(a, n / 2, p);
    return (root * root + p) % p;
  } else {
    return (a * modpow(a, n - 1, p) + p) % p;
  }
}
int main(void) {
  int n;
  cin >> n;
  long long int a[n], work[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    work[i] = a[i];
  }
  sort(work, work + n);
  int l = 0, r = n - 1;
  long long int ans = 0;
  for (int i = n - 1; i > -1; i--) {
    int j;
    for (j = 0; j < n; j++) {
      if (a[j] == work[i]) break;
    }
    if (r - j > j - l) {
      ans += a[j] * (r - j);
      r--;
    } else {
      ans += a[j] * (j - l);
      l++;
    }
    a[j] = -1;
  }
  cout << ans << endl;
  return 0;
}
