#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
using P = pair<ll, ll>;
int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    vector<P> B(N);
    for (int i = 0; i < N; i++) {
        cin >> A.at(i);
        B.at(i) = P(A[i], i);
    }

    sort(B.begin(), B.end());
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        ans += B[i].first * abs(i - B[i].second);
        B[i].second = i - B[i].second;
    }

    int l = 0;
    for (int i = N - 1; i > l;) {
        ll sum = B[i].first * (abs(B[i].second - (i - l)) - abs(B[i].second));
        for (int j = i - 1; j >= l; j--) {
            ll new_id = B[j].second + 1;
            sum += B[j].first * (abs(new_id) - abs(B[j].second));
        }
        if (sum > 0) {
            for (int j = i - 1; j >= l; j--) {
                B[j].second++;
                swap(B[j], B[j + 1]);
            }
            l++;
            ans += sum;
        } else {
            i--;
        }
    }
    cout << ans << endl;
}
