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

struct baby {
    int index = 0;
    int activity = 0;
    int max_activity = 0;

    bool operator>(const baby& right) const {
        return max_activity == right.max_activity ? (activity == right.activity ? index < right.index : activity > right.activity) : max_activity > right.max_activity;
    }
};

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

    vector<baby> a(n);
    for (auto i = 0; i < n; i++) {
        cin >> a[i].activity;
        a[i].index = i;
        a[i].max_activity = max(i, n - i - 1) * a[i].activity;
    }
    sort(a.begin(), a.end(), greater<>());

    int first = 0, last = n - 1;
    int64_t ans = 0;
    for (auto i = 0; i < n; i++) {
        //cout << a[i].activity << " " << a[i].index << " " << a[i].max_activity << endl;
        int forward = abs(a[i].index - first);
        int back = abs(last - a[i].index);
        if(forward >= back) {
            first++;
            ans += a[i].activity * forward;
        } else {
            last--;
            ans += a[i].activity * back;
        }
        //cout << first << " " << last << " " << forward << " " << back << " " << ans << endl;
    }

//    int ans = 0;
//    do {
//        int activity = 0;
//        for (auto i = 0; i < n; i++) {
//            activity += abs(vec[i] - i) * a[i];
//        }
//        ans = max(ans, activity);
//    } while(next_permutation(vec.begin(), vec.end()));

    cout << ans << endl;
    return 0;
}