program main
   integer, allocatable :: j(:), k(:)
   read(*, *) N
   allocate(j(N))
   read(*, *)(j(i), i=1,N)
   k = iqsort(j)
   write(*, *)sum(k(1:N:2)) - sum(k(2:N:2))
   deallocate(j)
   deallocate(k)
   stop
contains
recursive function iqsort(a) result(r)
  integer, intent(in) :: a(:)
  integer, allocatable :: r(:)
  logical, allocatable :: q(:)
  integer:: p
  if(size(a) < 2)then
    r = a
  else
    p = a(1)
    q = a(2:) < p
    r = [iqsort(pack(a(2:), .not. q)), p, iqsort(pack(a(2:), q))]
  endif
end function iqsort
end