def main():
    N = int(input())

    A = {}
    for i, a in enumerate(map(int, input().split(' '))):
        if a in A:
            A[a].append(i)
        else:
            A[a] = [i]

    B = list(A.keys())
    B.sort(reverse=True)
    L = 0
    R = N - 1
    S = 0
    for i in range(len(B)):
        a = B[i]
        J = A[a]
        for j in range(len(J)):
            p = J[j]
            d = abs(L - p)
            e = abs(R - p)
            if d < e:
                S += e * a
                R -= 1
            else:
                S += d * a
                L += 1
    print(S)


if __name__ == '__main__':
    main()
