#include <bits/stdc++.h>
using namespace std;
const int_fast32_t mod = 1e9 + 7;
const int_fast32_t maxn = 200005;
struct pr {
  int_fast32_t x;
  int_fast32_t y;
  int_fast32_t dst;
  pr() {}
  pr(int_fast32_t x, int_fast32_t y, int_fast32_t dst) : x(x), y(y), dst(dst) {}
  inline bool operator<(const pr& b) const {
    if (dst != b.dst) return dst > b.dst;
    if (x != b.x) return x > b.x;
    return y > b.y;
  }
};
struct point {
  int_fast32_t x;
  int_fast32_t y;
  point() {}
  point(int_fast32_t x, int_fast32_t y) : x(x), y(y) {}
  inline bool operator<(const point& b) const {
    if (x != b.x) return x > b.x;
    return y > b.y;
  }
};
inline bool spoint(point a, point b) {
  if (a.x != b.x) return a.x < b.x;
  return a.y < b.y;
}
inline bool spr(pr a, pr b) {
  if (a.dst != b.dst) return a.dst < b.dst;
  if (a.x != b.x) return a.x < b.x;
  return a.y < b.y;
}
int_fast32_t arr[maxn];
vector<int_fast32_t> num;
void solve() {
  int_fast32_t n;
  cin >> n;
  for (int_fast32_t i = 0; i < n; ++i) {
    cin >> arr[i];
    num.push_back(arr[i]);
  }
  sort(num.begin(), num.end(), greater<int_fast32_t>());
  int_fast32_t l = 0, r = n - 1, ans = 0;
  ;
  for (auto i : num) {
    for (int_fast32_t j = 0; j < n; ++j) {
      if (i == arr[j]) {
        if (j - l > r - j) {
          ans += (j - l) * i;
          ++l;
        } else {
          ans += (r - j) * i;
          --r;
        }
      }
    }
  }
  cout << ans << endl;
}
void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  srand(time(0));
}
int main() {
  init();
  solve();
  return 0;
}
