#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <climits>
#include <cfloat>
#include <algorithm>
#include <memory>
using namespace std; 

typedef long long ll; 
typedef unsigned long long ull;
typedef unsigned uint; 
typedef long double l_double;

#define vtr vector
#define PB push_back
#define EB emplace_back
#define gcd __gcd
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define st_ct static_cast // <>
#define TEST uint _ {}; cin >> _; while (_--)
#define PI (3.1415926535897932384626433832795)
#define MOD st_ct<int>(1e9 + 7)
#define EPS 1e-9
#define deb(x) cout<< #x << " --> " << x << endl;

template<typename T>
void vtrS(const vector<T> &ARR) 
{ for (T const &val: ARR) cout<<val<<" "; cout<<endl; }

template<typename T>
void vtrV(const vector<T> &ARR) 
{ for (T const &val: ARR) cout<<val<<endl; }

template<typename T>
void read_vvtr(vector<vector<T>> &ARR) 
{ for (vector<T> &i: ARR) for (T &j: i) cin >> j; }

template<typename T>
void vvtrS(const vector<vector<T>> &ARR) 
{ for (vector<T> const &i: ARR) { for (T const &j: i) cout<<j<<" "; cout<<endl; } }

template<typename T>
void vvtrNS(const vector<vector<T>> &ARR) 
{ for (vector<T> const &i: ARR) { for (T const &j: i) cout<<j; cout<<endl; } }

template<typename T>
T cntDigits(const T &N) { return (floor(log10(N)) + 1); }

template<typename T>
bool isPowOfTwo(const T &N) { return (N && (!(N&(N-1)))); }

template<typename T>
T lcm(T a, T b) { return ((a*b)/gcd(a, b)); }
// --------------------------------------------------------------------------------

class Child {
public:
    int val;
    int index;

    Child(int const &v = 0, int const &ind = 0);

    bool operator>(const Child &rhs) { return (val > rhs.val); }
    bool operator<(const Child &rhs) { return (val < rhs.val); } 
    bool operator>=(const Child &rhs) { return (val >= rhs.val); }
    bool operator<=(const Child &rhs) { return (val <= rhs.val); }
    bool operator==(const Child &rhs) { return (val == rhs.val); }
};

Child::Child(int const &v, int const &ind) 
    :val {v}, index {ind} {}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // cout.setf(std::ios::boolalpha);
    cout<<std::fixed<<std::setprecision(10); 
    // --------------------------------------
    
    int n {};
    cin >> n;
    vtr<Child> arr (n);
    for (int i {}; i < n; ++i) {
        int val {};
        cin >> val;
        arr.at(i).val = val;
        arr.at(i).index = i+1;
    }
    sort(rall(arr));
    
    // TEST
    for (auto const &ch: arr) {
        deb(ch.val);
        deb(ch.index);
        cout<<"--------------"<<endl;
    }
    
    int l {1}, r {n};
    int ans {};

    for (auto const &ch: arr) {
        if (abs(ch.index - l) > abs(ch.index - r)) {     
            ans += ch.val * abs(ch.index - l);
            ++l;
        } else {
            ans += ch.val * abs(ch.index - r);
            --r;
        }
    }

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