from operator import itemgetter

def get_new_position(old_index):
    global new_positions
    min_pos = new_positions[0]
    max_pos = new_positions[len(new_positions) - 1]
    if abs(min_pos - old_index) > abs(max_pos - old_index):
        return new_positions.pop(0)
    else:
        return new_positions.pop(len(new_positions) - 1)

numbers = int(input())
infants = list(map(lambda x: int(x), input().split(' ')))

data = [[0, 0] for i in range(numbers)]
for i, infant in enumerate(infants):
    data[i] = [i, infant]

data.sort(key=itemgetter(1), reverse=True)

new_positions = [i for i in range(numbers)]

point = 0
for infant in data:
    point += (abs(infant[0] - get_new_position(infant[0])) * infant[1])

print(point)