#region Header #!/usr/bin/env python3 # from typing import * import sys import io import math import collections import decimal import itertools from queue import PriorityQueue import bisect import heapq def input(): return sys.stdin.readline()[:-1] sys.setrecursionlimit(1000000) #endregion # _INPUT = """# paste here... # """ # sys.stdin = io.StringIO(_INPUT) def main(): N = int(input()) Y = [-10**12] + sorted(list(map(int, input().split()))) + [10**12] i = 1 v = 0 prev = Y[0] while i < N+1: if Y[i] - prev < Y[i+1] - Y[i]: # 下の方が近い v += Y[i] - prev i += 1 else: # 上の方が近い v += Y[i+1] - Y[i] prev = Y[i+1] i += 2 print(v) if __name__ == '__main__': main()