def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = [[i,x,0,0] for i,x in enumerate(a)]
    for i in b:
        i[2] = i[1] * i[0]
        i[3] = i[1] * (n-i[0])
    b.sort(key=lambda x:(x[2],x[3]),reverse=True)
    start = 0
    end = n-1
    ans = 0
    for i,k,p,s in b:
        if i - start > end - i:
            ans += (i-start)*k
            start += 1
        else:
            ans += (end-i)*k
            end -=1
    print(ans)

if __name__ == '__main__':
    main()
