#include<bits/stdc++.h>

using namespace std;

#define int long long

pair < int, int > a[2005];

inline int good(int i, int j) {
    int val = 0;
    if (i < a[i].second) val -= a[i].first;
    else val += a[i].first;
    if (j <= a[j].second) val += a[j].first;
    else val -= a[j].first;
    return val;
}

main() {
#ifdef HOME
    //freopen("input.txt", "r", stdin);
#endif // HOME
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, pos = 1;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].first;
        a[i].second = i;
    }
    while (pos < n) {
        if (good(pos, pos + 1) > 0) {
            swap(a[pos], a[pos + 1]);
            pos = max((long long)1, pos - 1);
        }
        else pos++;
    }
    int sum = 0;
    for (int i = 1; i <= n; ++i) sum += a[i].first * abs(a[i].second - i);
    cout << sum << '\n';
    return 0;
}

