def main(): n = int(input()) lst = list(map(int,input().split())) left = 0 right = len(lst)-1 answer = 0 for _ in range(n): if (lst[left] - lst[left+1]) >= (lst[right] - lst[right-1]): answer += lst[left] - lst[left+1] left += 2 else: answer += lst[right] - lst[right-1] right -= 2 print(answer) if __name__ == '__main__': main()