import numpy as np
from collections import deque

def main():
    args = input().rstrip().split(' ')
    N = int(args[0])
    args = input().rstrip().split(' ')
    A = np.array([int(val) for val in args])
    que = deque([int(val) for val in range(N)])
    top = np.argsort(-A)
    
    Ans = 0
    for i in top:
        l, r = abs(que[0]-i), abs(que[-1]-i)
        if(l > r):
            Ans += A[i] * l
            que.popleft()
        else:
            Ans += A[i] * r
            que.pop()
    print(Ans)
    
if __name__ == "__main__":
    main()