/* Author: Rennan Rocha
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
**/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//*find_by_order(); order_of_key();

ll arr[2000];
ll table[2000][2000];

int n;

ll solve(int i) {
    for(int j = 0; j < n; j++) {

    }
}

bool cmp(pi a, pi b) {
    return table[a.F][a.S] < table[b.F][b.S];
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n;

    for(int i = 0; i < n; i++) cin >> arr[i];
    ll ans = 0;

    int pos[n];
    for(int i = 0; i < n; i++) {
        pos[i] = i;
    }

    for(int k = 0; k < 20; k++) {
        for(int i = 0; i < n; i++) {
            int melhor = i;
            ll add_melhor = 0;
            for(int j = 0; j < n; j++) {
                ll agr = abs(pos[i]-i)*arr[pos[i]]+abs(j-pos[j])*arr[pos[j]];
                ll dps = abs(pos[i]-j)*arr[pos[i]]+abs(i-pos[j])*arr[pos[j]];
                if(dps-agr > add_melhor) {
                    melhor = j;
                    add_melhor = dps-agr;
                }
            }
            swap(pos[i], pos[melhor]);
        }
    }
    for(int i = 0; i < n; i++) {
        ans += abs(pos[i]-i)*arr[pos[i]];
    }
    cout << ans << "\n";
}
