#include <bits/stdc++.h>
using namespace std;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *min_element(all(a))
#define MAX(a) *max_element(all(a))
#define SUM(a) accumulate(all(a), 0LL)
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define RREP(i, n) for(int (i)=(n)-1; (i)>=0; (i)--)
#define FOR(i, m, n) for(int (i)=(m); (i)<(n); i++)
#define FORR(i, m, n) for(int (i)=(n)-1; (i)>=(m); i--)
#define debug(x) cerr << #x << " = " << x << endl
//#define int long long

typedef long long ll;
//ll const MOD = 998244353;
ll const MOD = 1e9+7;
int const inf = 1e9;
ll const INF = 1e18;

inline void ios_(){cin.tie(0); ios::sync_with_stdio(false);}
template<typename T> int size(const T& a){return (int)a.size();}
template<typename T> T Div(T a, T b){return (a + b - 1) / b;}
template<typename T> bool chmin(T& a, const T& b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, const T& b){if(a < b){a = b; return true;} return false;}



signed main(){
    int n;
    cin >> n;
    vector<ll> v(n);
    for(auto&& i : v) cin >> i;

    vector<int> order(n);
    REP(i, n) order[i] = i;
    sort(all(order), [&](auto a, auto b){return v[a] > v[b];});

    vector<bool> used(n, false);
    ll ans = 0;

    int left = 0, right = n-1;

    REP(i, n){
        ll score = -1;
        int p = 0;
        REP(j, n){
            if(used[j]) continue;
            ll l = abs(j - left) * v[j], r = abs(right - j) * v[j];
            if(chmax(score, max(l, r))){
                p = j;
            }
        }
        ll l = abs(p - left) * v[p], r = abs(right - p) * v[p];
        if(l > r){
            ans += l;
            left++;
        }else{
            ans += r;
            right--;
        }
        used[p] = true;
    }

    cout << ans << endl;

    return 0;
}
