Problem:
Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();
    let a = read_vec::<usize>();
    let mut a = a.into_iter().collect::<PriorityQueue<_>>();
    let mut b = Vec::new();
    while let Some(x) = a.pop() {
        b.push(x);
    }
    b.reverse();
    debug!(b);
    let mut alice = 0;
    let mut bob = 0;
    for i in 0..n {
        if i % 2 == 0 {
            alice += b[i];
        } else {
            bob += b[i];
        }
    }
    println!("{}", alice - bob);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Clone, Debug)]
struct PriorityQueue<T>
where
    T: std::cmp::Ord + std::clone::Clone + std::marker::Copy,
{
    heap: Vec<T>,
}

impl<T> PriorityQueue<T>
where
    T: std::cmp::Ord + std::clone::Clone + std::marker::Copy,
{
    fn new() -> PriorityQueue<T> {
        PriorityQueue {
            heap: Vec::<T>::new(),
        }
    }

    fn push(&mut self, x: T) {
        let mut i = self.heap.len() as i64;
        self.heap.push(x);
        while i > 0 {
            let p = (i as usize - 1) / 2;
            if self.heap[p] <= x {
                break;
            }
            let temp = self.heap[i as usize];
            self.heap[i as usize] = self.heap[p];
            self.heap[p] = temp;
            i = p as i64;
        }
    }

    fn is_empty(&self) -> bool {
        self.heap.is_empty()
    }

    fn pop(&mut self) -> Option<T> {
        if self.is_empty() {
            return None;
        }
        let ret = self.heap[0];
        let x = self.heap[self.heap.len() - 1];
        let mut i = 0;
        let new_len = self.heap.len() - 1;
        while i * 2 + 1 < new_len {
            let mut a = i * 2 + 1;
            let b = i * 2 + 2;
            if b < new_len && self.heap[b] < self.heap[a] {
                a = b;
            }
            if self.heap[a] >= x {
                break;
            }
            self.heap[i] = self.heap[a];
            i = a;
        }
        self.heap[i] = x;
        self.heap.swap_remove(new_len);
        Some(ret)
    }

    fn peek(&self) -> Option<T> {
        if self.is_empty() {
            return None;
        }
        Some(self.heap[0])
    }
}

impl<T> std::iter::FromIterator<T> for PriorityQueue<T>
where
    T: std::cmp::Ord + std::clone::Clone + std::marker::Copy,
{
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut c = PriorityQueue::new();
        for i in iter {
            c.push(i);
        }
        c
    }
}