// #define STOPIT
#include<bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;

typedef int FLOW;                // フローを表す型、今回は int 型
typedef int COST;                // コストを表す型、今回は int 型
const int MAX_V = 2000;           // グラフの最大ノード数
const COST INF = 100000000;      // 十分大きい値

#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;

void debug_impl() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
    #define debug(...)\
    do {\
        cerr << boolalpha << "[" << #__VA_ARGS__ << "]:";\
        debug_impl(__VA_ARGS__);\
        cerr << std::noboolalpha;\
    } while (false)
#else
    #define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, enable_if_t<!is_same< Container, string >::value, nullptr_t> = nullptr>
istream& operator>> (istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, enable_if_t<!is_same< Container, string >::value, nullptr_t> = nullptr >
ostream& operator<< (ostream& os, Container const& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}
struct Baby
{
    int act;
    int from;
    int to;
    Baby(ll act, int from, int to):act(act), from(from), to(to){};
};

int main() 
{
    int n;
    cin >> n;
    vector<int> activity(n);
    vector<Baby> babies;
    rep(i, 0, n)
    {
        ll a;
        cin >> a;
        babies.emplace_back(a, i, i);
    }
    for(;;)
    {
        bool update = false;
        rep(i, 0, n)
            rep(j, 0, n)
            {
                Baby &baby1 = babies[i];
                Baby &baby2 = babies[j];

                ll now = 0;
                now += baby1.act*abs(baby1.from-baby1.to);
                now += baby2.act*abs(baby2.from-baby2.to);
                
                ll next = 0;
                next += baby1.act*abs(baby1.from-baby2.to);
                next += baby2.act*abs(baby2.from-baby1.to);
                // debug(baby1.from, baby1.to, baby2.from, baby2.to, now, next);
                if (now<next)
                {
                    swap(baby1.to, baby2.to);
                    update = true;
                }
            }
        if (!update) break;
    }

    ll ans = 0;
    for (auto baby: babies)
        ans += baby.act*abs(baby.from-baby.to);

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