n = int(input())
a = []
for ind, i in enumerate(input().split()):
    a.append((ind, int(i)))

def search_next(x):
    if abs(x-l)>abs(x-r):
        return 'r'
    elif abs(x-l)<abs(x-r):
        return 'l'
    else:
        raise RuntimeError()

a.sort(key=lambda x:x[1]*max(x[0], n-x[0]), reverse=True)
l=0
r=n-1
ans = 0
for ind, (x, i) in enumerate(a):
    if abs(x-l)>abs(x-r) or ind==n-1:
        ans += i*abs(x-l)
        l += 1
    elif abs(x-l)<abs(x-r):
        ans += i*abs(x-r)
        r -= 1
    elif search_next(a[ind+1][0])=='l':
        ans += i*abs(x-l)
        l += 1
    else:
        ans += i*abs(x-r)
        r -= 1
print(ans)