import itertools
import numpy as np

N = int(input())
x = list(map(int, input().split()))

x_max = x.pop(-1)
x_left = x
length = len(x_left)

x_left_list = list(itertools.permutations(x_left, length))
x_left_list = [list(v) for v in x_left_list]

# numpy 配列に。
x_left_array = np.array(x_left_list).reshape(-1, length)
x_max_array = np.array([x_max for i in range(length)]).reshape(-1, 1)
x_all = np.hstack((x_max_array, x_left_array))

x_diff = np.array(np.abs(x_all[:, :-1] - x_all[:, 1:])).reshape(-1)

X = np.sum(x_diff)

print(X)