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

typedef long long int ll;
typedef pair <ll, ll> pll;

const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const ll Inf = 1LL << 62;

ll ar[Max];


int main() {
#ifdef Mr_Emrul
        freopen("inputf.in", "r", stdin);
#endif /// Mr_Emrul


        ios_base::sync_with_stdio(false);
        cin.tie(0);

        ll ans = 0;
        ll n;
        cin >> n;
        set <int> st;
        for(int i = 1; i <= n; i++) {
                cin >> ar[i];
                st.insert(i);
        }
        vector <pll> vec;
        set <int> ache = st;
        while(ache.size()) {
                bool f = 0;
                vec.clear();
                for(int i : ache) {
                        ll x = *st.begin();
                        ll y = *st.rbegin();
                        if(abs(x - i) >= abs(y - i)) {
                                vec.push_back({abs(x - i) * ar[i], i});
                        }
                        else  {
                                vec.push_back({abs(y - i) * ar[i], i});
                        }
                }
                sort(vec.begin(), vec.end());
                reverse(vec.begin(), vec.end());
                for(pll x : vec) {
                        int l = *st.begin();
                        int r = *st.rbegin();
                        if(1LL * abs(l - x.second) * ar[x.second] == x.first) {
                                ans += abs(l - x.second) * ar[x.second];
                                st.erase(l);
                                ache.erase(x.second);
                                f = 1;
                        }
                        else if(1LL * abs(r - x.second) * ar[x.second] == x.first) {
                                ans += abs(r - x.second) * ar[x.second];
                                st.erase(r);
                                ache.erase(x.second);
                                f = 1;
                        }
                        break;
                }
        }
        assert(ache.size() == 0);
        cout << ans << endl;
        return 0;
}
