N = int(input()) ax = list(map(int, input().split())) mm = 0 while len(ax) != 0: left = ax[0] - ax[1] right = ax[-1] - ax[-2] if left > right: mm += left ax.pop(0) ax.pop(0) else: mm += right ax.pop() ax.pop() print(mm)