import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

alias C = Tuple!(int, "i", long, "a");

void main()
{
    auto N = readln.chomp.to!int;
    long[] AS;
    int[] cs;
    foreach (i, a; readln.split.to!(long[])) {
        AS ~= a;
        cs ~= i.to!int;
    }
    auto ii = new int[](N);
    ii[] = -1;

    int try_swap(int j, int k) {
        auto i = ii[j];
        if (i == -1) return -1;
        if (abs(i-j) * AS[i] < abs(k-j) * AS[k]) {
            return i;
        } else {
            return -2;
        }
    }

    while (!cs.empty) {
        auto c = cs[0];
        cs = cs[1..$];
        int l, r = N-1;
        for (;;) {
            if (c-l > r-c) {
                auto i = try_swap(l, c);
                if (i != -2) {
                    ii[l] = c;
                    if (i != -1) cs ~= i;
                    break;
                }
                ++l;
            } else if (c-l < r-c) {
                auto i = try_swap(r, c);
                if (i != -2) {
                    ii[r] = c;
                    if (i != -1) cs ~= i;
                    break;
                }
                --r;
            } else {
                auto i = try_swap(l, c);
                if (i != -2) {
                    ii[l] = c;
                    if (i != -1) cs ~= i;
                    break;
                }
                i = try_swap(r, c);
                if (i != -2) {
                    ii[r] = c;
                    if (i != -1) cs ~= i;
                    break;
                }
                ++l;
                --r;
            }
        }
    }

    long r;
    foreach (j, i; ii) {
        r += abs(i.to!long - j.to!long) * AS[i];
    }
    writeln(r);
}