#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
        return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

class state {
public:
        ll score, a;
        int i, j;

        state() = default;

        state(ll score, ll a, int i, int j) 
                : score(score), a(a), i(i), j(j) {}

        state(const state&) = default;
        
        bool operator< (const state& other) {
                if (score != other.score) {
                        return score > other.score;
                } else {
                        return a > other.a;
                }
        }
};

enum class P {
        Emp, Wait, Fill
};

int main() {
        int n;
        cin >> n;

        vector<ll> a(n);
        vector<state> p(n);
        rep (i, n) {
                cin >> a[i];
                if (a[i] * i < a[i] * (n - i)) {
                        p[i] = {a[i] * (n - i), a[i], i, n};
                } else {
                        p[i] = {a[i] * i, a[i], i, 0};
                }
        }

        ll ans = 0;
        vector<pair<P, int>> s(n, pair<P, int>(P::Emp, -1));
        vector<bool> used(n, false);
        rep (_, n) {
                bool first = true;
                int l = -1, r = -1;
                rep (i, n) {
                        if (s[i].first != P::Fill) {
                                if (first) {
                                        first = false;
                                        l = i;
                                }
                                r = i;
                        }
                }
                
                p.clear();
                rep (i, n) {
                        if (used[i]) continue;
                        p.emplace_back(a[i] * abs(i - r), a[i], i, r);
                        p.emplace_back(a[i] * abs(i - l), a[i], i, l);
                }

                sort(p.begin(), p.end());

                if (s[p[0].j].first == P::Wait) {
                        s[s[p[0].j].second].first = P::Fill;
                }

                if (p.size() > 2 && p[0].i == p[1].i && p[0].score == p[1].score) {
                        s[p[0].j] = pair<P, int>(P::Wait, p[1].j);
                        s[p[1].j] = pair<P, int>(P::Wait, p[0].j);
                } else {
                        s[p[0].j] = pair<P, int>(P::Fill, -1);
                }

                //cout << p[0].score << " " << p[0].a << " " << p[0].i << " " << p[0].j << endl;
                
                ans += p[0].score;
                used[p[0].i] = true;
        }

        cout << ans << endl;

        return 0;
}
