#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;
template <typename T>
inline void veccin(vector<T>& v) {
  for (auto&& e : v) {
    cin >> e;
  }
}
template <typename T>
inline void dbg(T a) {
  cerr << a << ' ';
}
ll n, m, k;
ll ans = 0, sum = 0, cnt = 0;
string s;
char c;
bool flag = false;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  vi a(n);
  veccin(a);
  vi srtd(n);
  ll bcnt = 0, ecnt = n - 1;
  for (ll i = (ll)(0); i < (ll)(n); ++i) {
    auto j = max_element(a.begin(), a.end());
    if (j - (a.begin() + bcnt) > (a.begin() + bcnt) - j) {
      ans += *j * abs(j - (a.begin() + bcnt));
      srtd[bcnt++] = *j;
    } else {
      ans += *j * abs((a.begin() + ecnt) - j);
      srtd[ecnt--] = *j;
    }
    *j = 0;
  }
  cout << fixed << setprecision(12) << ans;
}
