Problem:
Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import javax.swing.plaf.basic.BasicViewportUI;
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        /*new Thread(null, new Runnable() {
            @Override
            public void run() {
                try {
                    new Solver().solve();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }, "Solver", 1l << 30).start();*/

        new Solver().solve();
    }
}

class Solver {
    IO io = new IO(System.in, System.out);

    int N;
    TreeSet<Dish> Asorted, Bsorted;

    Comparator<Dish> com1 = new Comparator<Dish>() {
        @Override
        public int compare(Dish a, Dish b) {
            if (a.aval-  a.bval != b.aval - b.bval)
                return Long.compare(a.aval - a.bval, b.aval - b.bval);

            if (a.aval != b.aval) return -Long.compare(a.aval, b.aval);
            else if (a.bval != b.bval) return Long.compare(a.bval, b.bval);
            else return Integer.compare(a.ind, b.ind);
        }
    };
    Comparator<Dish> com2 = new Comparator<Dish>() {
        @Override
        public int compare(Dish a, Dish b) {
            if (a.bval - a.aval != b.bval - b.aval)
                return Long.compare(a.bval - a.aval, b.bval - b.aval);

            if (a.bval != b.bval) return -Long.compare(a.bval, b.bval);
            else if (a.aval != b.aval) return Long.compare(a.aval, b.aval);
            else return Integer.compare(a.ind, b.ind);
        }
    };


    void solve() throws Exception {
        int i, j, k;

        N = io.nextInt();
        Asorted = new TreeSet<>(com1);
        Bsorted = new TreeSet<>(com2);

        for (i = 0; i < N; ++i) {
            Dish d = new Dish(i, io.nextLong(), io.nextLong());
            Asorted.add(d);
            Bsorted.add(d);
        }

        long Asum = 0, Bsum = 0;
        if (Asorted.size() != N || Bsorted.size() != N)
            System.exit(1);

        Dish selected;
        for (i = 0; i < N; ++i) {
            if (i % 2 == 0) {
                Dish a = Asorted.first(), b = Asorted.last();
                selected = a.aval - a.bval <= b.bval - b.aval ? b : a;
                Asum += selected.aval;
            } else {
                Dish a = Bsorted.first(), b = Bsorted.last();
                selected = a.bval - a.aval <= b.aval - b.bval ? b : a;
                Bsum += selected.bval;
            }

            Asorted.remove(selected); Bsorted.remove(selected);
        }
        if (!Asorted.isEmpty() || !Bsorted.isEmpty())
            System.exit(1);

        io.println(Asum - Bsum);

        io.flush();
    }
}

class Dish {
    int ind; long aval, bval;

    Dish(int i, long a, long b) {
        ind = i; aval = a; bval = b;
    }
}


class IO {
    static byte[] buf = new byte[2048];
    static int index, total;
    static InputStream in;
    static BufferedWriter bw;


    IO(InputStream is, OutputStream os) {
        try {
            in = is;
            bw = new BufferedWriter(new OutputStreamWriter(os));
        } catch (Exception e) {
        }
    }

    IO(String inputFile, String outputFile) {
        try {
            in = new FileInputStream(inputFile);
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outputFile)));
        } catch (Exception e) {
        }
    }

    int scan() throws Exception {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0)
                return -1;
        }
        return buf[index++];
    }

    String next() throws Exception {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        StringBuilder sb = new StringBuilder();
        for (; c > 32; c = scan())
            sb.append((char) c);
        return sb.toString();
    }

    int nextInt() throws Exception {
        int c, val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+')
            c = scan();
        for (; c >= '0' && c <= '9'; c = scan())
            val = (val << 3) + (val << 1) + (c & 15);
        return neg ? -val : val;
    }

    long nextLong() throws Exception {
        int c;
        long val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+')
            c = scan();
        for (; c >= '0' && c <= '9'; c = scan())
            val = (val << 3) + (val << 1) + (c & 15);
        return neg ? -val : val;
    }

    void print(Object a) throws Exception {
        bw.write(a.toString());
    }

    void println() throws Exception {
        bw.write("\n");
    }

    void println(Object a) throws Exception {
        print(a);
        println();
    }

    void flush() throws Exception {
        bw.flush();
        bw.close();
    }
}