#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

int main()
{
    int N;
    std::cin >> N;

    std::vector<long> A;
    A.reserve(N);

    std::copy(std::istream_iterator<long>{std::cin}, std::istream_iterator<long>{},
        std::back_inserter(A));

    int bound;
    {
        std::vector<long> sums;
        sums.reserve(N);
        std::partial_sum(A.cbegin(), A.cend(), std::back_inserter(sums));

        bound = 1 + static_cast<int>(std::lower_bound(sums.cbegin(), sums.cend(), sums.back() / 2) - sums.cbegin());
    }

    std::vector<int> idxs(N);
    std::iota(idxs.begin(), idxs.end(), 0);

    std::sort(idxs.begin(), idxs.begin() + bound,
        [&](auto lhs, auto rhs) {
            return A.at(lhs) > A.at(rhs);
        });
    std::sort(idxs.begin() + bound, idxs.end(),
        [&](auto lhs, auto rhs) {
            return A.at(lhs) > A.at(rhs);
        });

    long result = 0;
    for (auto i = 0; i < bound; i++) {
        auto idx = idxs.at(i);

        result += std::abs((N - 1 - i) - idx) * A.at(idx);
    }
    for (auto i = bound; i < N; i++) {
        auto idx = idxs.at(i);

        result += std::abs((i - bound) - idx) * A.at(idx);
    }

    std::cout << result << std::endl;

    return 0;
}