import numpy
N = int( input())
a = list(map(int, input().split(' ')))
l = [0]*N
ain = list(set(a))
p = numpy.argsort(a)[::-1]
a = numpy.sort(a)[::-1]
j = []
for _, x in enumerate(ain):
    j = numpy.sort([p_x for i, p_x in enumerate(p) if a[p_x] == x])[::-1]
    i = numpy.sort([i for i, p_x in enumerate(p) if a[p_x] == x])[::-1]
    p[i] = j
    
    

maxmin = [1, N]
s = 0
for i, a_i in enumerate(a):
    if p[i] + 1 > N/2 :
        s += (p[i] + 1  - maxmin[0])*a_i
        maxmin[0] = maxmin[0] + 1
        
    else:
        s += (maxmin[1]-p[i]-1)*a_i
        maxmin[1] = maxmin[1] - 1
        
print(s)
        