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)

extern crate core;

use std::io::{self, Read};
use lib::*;

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    let output = run(input.trim().to_string());
    println!("{}", output);
}

fn run(input: String) -> String {
    let mut list = input
        .split_whitespace()
        .skip(1)
        .map(|x| x.parse::<i32>().unwrap())
        .collect::<Vec<_>>();
    list.sort();

    let mut a = 0;
    let mut b = 0;

    while list.len() != 0 {
        a += list.pop().unwrap();
        b += list.pop().unwrap_or(0);
    }

    (a - b).to_string()
}

#[test]
fn test() {
    let tests = vec![
        (
            "2
3 1",
            "2",
        ),
        (
            "3
2 7 4",
            "5",
        ),
        (
            "4
20 18 2 18",
            "18",
        ),
    ];
    for (i, (input, output)) in tests.into_iter().enumerate() {
        println!("test:{}", i);
        assert_eq!(run(input.to_string()), output.to_string());
    }
}

mod lib {
    //https://qiita.com/edo_m18/items/0588d290a19f2afc0a84
    mod graph {
        #[derive(Debug, PartialEq, Clone)]
        pub struct Node {
            pub edges: Vec<(usize, f64)>,
            pub done: bool,
            pub cost: f64,
            pub id: usize,
            pub previous_node: Option<usize>,
        }

        impl Node {
            pub fn new(id: usize) -> Node {
                Node {
                    edges: Vec::new(),
                    done: false,
                    cost: -1.0,
                    id: id,
                    previous_node: None,
                }
            }

            pub fn add_node(&mut self, id: usize, cost: f64) {
                self.edges.push((id, cost));
            }
        }

        pub fn min_route(mut nodes: Vec<Node>) -> (f64, Vec<Node>) {
            nodes[0].cost = 0.0;

            loop {
                let mut process_node: Option<usize> = None;

                for node in &nodes {
                    // 訪問済み or まだコストが未設定
                    if node.done || node.cost < 0.0 {
                        continue;
                    }

                    match process_node {
                        Option::None => process_node = Some(node.id),
                        Option::Some(pn) => {
                            if node.cost < nodes[pn].cost {
                                process_node = Some(node.id);
                            }
                        }
                    }
                }

                match process_node {
                    Option::Some(pn) => {
                        nodes[pn].done = true;

                        for edge in nodes[pn].edges.clone() {
                            let node = edge.0;
                            let cost = nodes[pn].cost + edge.1;

                            // コストが未設定 or コストの少ない経路がある場合はアップデート
                            let needs_update =
                                (nodes[node].cost < 0.0) || (nodes[node].cost > cost);
                            if needs_update {
                                nodes[node].cost = cost;
                                nodes[node].previous_node = Some(pn);
                            }
                        }
                    }
                    None => {
                        break;
                    }
                }
            }

            let goal_node = nodes.last().unwrap();

            let mut path: Vec<Node> = Vec::new();
            let mut current_node = goal_node;
            loop {
                let next_node = current_node.previous_node;
                match next_node {
                    Option::None => {
                        break;
                    }
                    Option::Some(next_node) => {
                        path.push(nodes[next_node].clone());
                        current_node = &nodes[next_node];
                    }
                }
            }

            let sum = path.clone()
                .into_iter()
                .filter_map(|x| {
                    x.previous_node
                        .map(|id| x.edges.iter().find(|x| x.0 == id).unwrap().1)
                })
                .sum::<f64>() + path[0].edges.last().unwrap().1;

            (sum, path)
        }
    }

}