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: Runtime Error)

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		final int N = sc.nextInt();
		sc.nextLine();
		final Map<Integer, Long> A = new HashMap<>();
		final Map<Integer, Long> B = new HashMap<>();
		final List<Integer> sortedKeysA = new LinkedList<>();
		final List<Integer> sortedKeysB = new LinkedList<>();
		final boolean[] selected = new boolean[N];

		for (int i = 0; i < N; i++) {
			final long prefA = sc.nextLong();
			final long prefB = sc.nextLong();
			sc.nextLine();

			A.put(i, prefA);
			B.put(i, prefB);
			selected[i] = false;
			
			if (i == 0) {
				sortedKeysA.add(i);
				sortedKeysB.add(i);
				continue;
			}

			boolean aInserted = false;
			boolean bInserted = false;
			for (int j = 0; j < i; j++) {
				final int keyA = sortedKeysA.get(j);
				final int keyB = sortedKeysB.get(j);

				final long valueA = A.get(keyA);
				final long valueB = B.get(keyB);

				if (!aInserted && prefA >= valueA) {
					sortedKeysA.add(j, i);
					aInserted = true;
				}
				if (!bInserted && prefB >= valueB) {
					sortedKeysB.add(j, i);
					bInserted = true;
				}

				if (aInserted && bInserted) {
					break;
				}
			}
		}

		long takahashi = 0;
		long aoki = 0;
		int tIndex = 0;
		int aIndex = 0;

		int numTrials = 0;
		while (numTrials < N) {
			boolean t = (numTrials % 2) == 0;

			final int maxTId = getHighest(N, tIndex, A, sortedKeysA, selected);
			final long prefMaxT= A.get(maxTId);
			final int nextTId = getSecondHighest(N, tIndex, A, sortedKeysA, selected);
			final long prefNextT = A.get(maxTId);
			
			long expT = prefMaxT - prefNextT;

			final int maxAId = getHighest(N, aIndex, B, sortedKeysB, selected);
			final long prefMaxA= B.get(maxTId);
			final int nextAId = getSecondHighest(N, aIndex, B, sortedKeysB, selected);
			final long prefNextA = B.get(maxTId);
			
			long expA = prefMaxA - prefNextA;

			if (t) {
				if (expT > expA) {
					takahashi += prefMaxT;
					selected[maxTId] = true;
					tIndex++;
				} else {
					takahashi += A.get(maxAId);
					selected[maxAId] = true;
					aIndex++;
				}
			} else {
				if (expA > expT) {
					aoki += prefMaxA;
					selected[maxAId] = true;
					aIndex++;
				} else {
					aoki += B.get(maxTId);
					selected[maxTId] = true;
					tIndex++;
				}
			}

			numTrials++;
		}

		System.out.println(takahashi - aoki);
	}

	private static int getHighest(int N, int index, Map<Integer, Long> prefs,
			List<Integer> sortedKeys, boolean[] selected) {
		for (int i = index; i < N; i++) {
			final int key = sortedKeys.get(i);
			final boolean sel = selected[key];

			if (!sel) {
				return key;
			}
		}

		return -1;
	}
	
	private static int getSecondHighest(int N, int index, Map<Integer, Long> prefs,
			List<Integer> sortedKeys, boolean[] selected) {
		boolean firstFound = false;
		for (int i = index; i < N; i++) {
			final int key = sortedKeys.get(i);
			final boolean sel = selected[key];

			if (!sel) {
				if (firstFound) {
					return key;
				} else {
					firstFound = true;
				}
			}
		}

		return -1;
	}

}

class ValueComparator implements Comparator<Long> {

	@Override
	public int compare(Long o1, Long o2) {
		return Long.compare(o2, o1);
	}

}