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.



Sample input:
3
10 10
20 20
30 30


Sample output:
20

Submission status: Runtime Error

Solution code:
import heapq


def solve(N, ABs):
    # print(N, ",", ABs)
    taken = {i: False for i in range(N)}
    As = [(ab[0], i) for i, ab in enumerate(ABs)]
    Bs = [(ab[1], i) for i, ab in enumerate(ABs)]
    heapq.heapify(As)
    heapq.heapify(Bs)
    ans = 0
    taken_count = 0
    for _ in range(N // 2 + 1):
        if taken_count >= N:
            break
        if not len(As):
            break
        am = As.pop(len(As) - 1)
        while taken[am[1]]:
            am = As.pop(len(As) - 1)

        if not len(Bs):
            ans += am[1]
            break

        bm = Bs.pop(len(Bs) - 1)
        while taken[bm[1]]:
            bm = Bs.pop(len(Bs) - 1)
        if am[1] == bm[1]:
            ans += am[0]
            taken[am[1]] = True
            taken_count += 1
            if len(Bs):
                bm = Bs.pop(len(Bs) - 1)
                while taken[bm[1]]:
                    bm = Bs.pop(len(Bs) - 1)
                ans -= bm[0]
                taken[bm[1]] = True
                taken_count += 1
        elif am[0] > bm[0]:
            ans += am[0]
            ans -= bm[0]
            taken[am[1]] = True
            taken[bm[1]] = True
            taken_count += 2
        elif am[0] < bm[0]:
            ans += bm[0]
            ans -= am[0]
            taken[am[1]] = True
            taken[bm[1]] = True
            taken_count += 2

    return ans


if __name__ == "__main__":
    N = int(input())
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, ABs))

Submission status: Runtime Error

Solution code:
n = int(input())
a = []
b = []
x = 0
y = 0
for i in range(n):
    q, w = map(int, input().split())
    a.append(q)
    b.append(w)
if a[0] > a[-1]:
    for j in range(n, 0, -1):
        if j%2:
            y += b[j]
        else:
            x += a[j]
else:
    for j in range(n):
        if j%2:
            y += b[j]
        else:
            x += a[j]
print(x-y)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import functools

def cmp1(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (t1 - a0) - (t0 - a1)

def cmp2(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (a1 - t0) - (a0 - t1)

n = int(input())
AB = [tuple((i, *(int(x) for x in input().split()))) for i in range(n)]

A = sorted(AB, key=functools.cmp_to_key(cmp1))
B = sorted(AB, key=functools.cmp_to_key(cmp2))
used = {-1}
a, b = -1, -1
ans = 0

for _ in range(n // 2):
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

    while b in used:
        b, _, s = B.pop(0)
    used.add(b)
    ans -= s

if n % 2 == 1:
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
happiness=[list(map(int,input().split())) for i in range(n)]n=int(input())
happiness=[list(map(int,input().split())) for i in range(n)]
a=0
b=0
li = [x for x in zip(*happiness)]
for i in range(n):
  if li[0][i] >= li[1][i]:
    a+=li[0][i]
  else:
    b+=li[1][i]
ans=a-b
print(str(ans))

Submission status: Runtime Error

Solution code:
import pandas as pd
n=int(input())
O=[]
#A=[]
#B=[]
for i in range(n):
    a,b=map(int, input().split(" "))
    O.append([2*a-b,a,b])
  
O=sorted(O, key=lambda x: x[0])[::-1]
#print(O)

count=0
for num in range(n):
    if num%2==0:
        count+=O[num][1]
    else:
        count-=O[num][2]

print(count)

Submission status: Runtime Error

Solution code:
import heapq
N = int(input())
A = []
B = []
for i in range(N):
    a, b = map(int, input().split())
    A.append((a, i))
    B.append((b, i))

OA = A[:]
OB = B[:]
A.sort(key=lambda x:x[0])
B.sort(key=lambda x:x[0])


used = [False]*N
x, y = 0, 0
for _ in range(N):
    if _ % 2 == 0:
        while A:
            a, i = A.pop()
            if used[i]:
                continue
            b, j = B[-1]
            if a > b:
                x += a
                used[i] = True
            else:
                x += OA[j][0]
                used[j] = True
            break
    else:
        while B:
            b, i = B.pop()
            if used[i]:
                continue
            a, j = A[-1]
            if b > a:
                y += b
                used[i] = True
            else:
                y += OB[j][0]
                used[j] = True
            break
print(x - y)

Submission status: Runtime Error

Solution code:
N=int(input())
num_list=[]
for i in range(0,N):
    a,b=map(int, input().split())
    num_list.append([a+b,a,b])
num_list.sort(key=lambda x:x[0],reverse=True)
ans=0
for i in range(0,N):
    if(i%2==0):
        ans+=num_list[i][1]
    else:
        ans-=num_list[i][2]

print(and)

Submission status: Runtime Error

Solution code:
n = int(input())

a = []
b = []
aa = []
for i in range(n):
	k,l = map(int,input().split())
	a.append(k)
	b.append(l)
	aa.append([k,l])
dp = [[0,0] for i in range(n+1)]
aa.sort(key = lambda x:x[1])
co = 0
for i in range(n):
	for j in range(n):
		if i % 2 == 0:		
			dp[i+1] = max(dp[i]+aa[j][i%2],dp[i+1])
		else:
			dp[i+1] = max(dp[i]-aa[j][i%2],dp[i+1])

print(dp[n+1])

Submission status: Runtime Error

Solution code:
import pandas as pd
n=int(input())
O=[]
A=[]
B=[]
for i in range(n):
    a,b=map(int, input().split(" "))
    O.append(a+a-b)
    A.append(a)
    B.append(b)

tmp=pd.DataFrame()
tmp["o"]=O
tmp["b"]=A
tmp["c"]=B

tmp=tmp.sort_values(by="o",ascending=False)

count=0
for num,(i,j) in enumerate(zip(tmp["b"].values,tmp["c"].values)):
    if num%2==0:
        count+=i
    else:
        count-=j
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
rec = []
for i in range(N):
    a, b = map(int, input().split())
    rec.append((a, b, i))

rec1 = sorted(rec, key=lambda x: x[0] - x[1], reverse=True)
rec2 = sorted(rec, key=lambda x: x[1] - x[0], reverse=True)
used = [-1] * N
i = 0
j = 0
k = 0
num = 0
while k < N:
    if k % 2 == 0:
        while i < N and used[i] != -1:
            i += 1
        num += rec1[i][0]
        used[rec1[i][2]] = 1
        i += 1
    else:
        while j < N and used[j] != -1:
            j += 1
        num -= rec2[j][1]
        used[rec2[j][2]] = 1
        j += 1

    k += 1

print(num)

Submission status: Runtime Error

Solution code:
N = int(input())
rec = []
for i in range(N):
    a, b = map(int, input().split())
    rec.append((a, b, i))

rec1 = sorted(rec, key=lambda x: x[0] - x[1], reverse=True)
rec2 = sorted(rec, key=lambda x: x[1] - x[0], reverse=True)
used = [-1] * N
i = 0
j = 0
k = 0
num = 0
while k < N:
    if k % 2 == 0:
        while i < N and used[i] != -1:
            i += 1
        num += rec1[i][0]
        used[rec1[i][2]] = 1
        i += 1
    else:
        while j < N and used[j] != -1:
            j += 1
        num -= rec2[i][1]
        used[rec2[i][2]] = 1
        j += 1

    k += 1

print(num)

Submission status: Runtime Error

Solution code:
N = int(input())
rec = []
for i in range(N):
    a, b = map(int, input().split())
    rec.append((a, b, i))

rec1 = sorted(rec, key=lambda x: x[0] - x[1], reverse=True)
rec2 = sorted(rec, key=lambda x: x[1] - x[0], reverse=True)
used = [-1] * N
i = 0
j = 0
k = 0
num = 0
while k < N:
    if k % 2 == 0:
        while used[i] != -1:
            i += 1
        num += rec1[i][0]
        used[rec1[i][2]] = 1
        i += 1
    else:
        while used[j] != -1:
            j += 1
        num -= rec2[i][1]
        used[rec2[i][2]] = 1
        j += 1

    k += 1

print(num)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [[int(i) for i in input().split()] for j in range(n)]

C = []
D = []
i = 0

for a, b in A:
    C.append((a - b, i))
    D.append((b - a, i))
    i += 1

C.sort()
D.sort()

used = [False] * n
ans = 0

for i in range(n):
    if i % 2 == 0:
        while used[C[-1][1]]:
            C.pop()
        ans += A[C[-1][1]][0]
        used[C[-1][1]] = True
        C.pop()
    else:
        while used[D[-1][1]]:
            D.pop()
        ans -= A[C[-1][1]][1]
        used[C[-1][1]] = True
        D.pop()

print(ans)

Submission status: Runtime Error

Solution code:
n = input()
flag = [0 for i in xrange(n)]
a = []
b = []
for i in xrange(n):
	x,y = map(int, raw_input().split())
	a.append((x,y,i))
	b.append((y,x,i))
a.sort(reverse = True)
b.sort(reverse = True)
ptr1 = 0
ptr2 = 0
turn = 0
ans1 = 0
ans2 = 0
while ptr1 < n and ptr2 < n:
	if turn == 0:
		while flag[a[ptr1][2]] == 1:
			ptr1 += 1
		if ptr1 < n:
			ans1 += a[ptr1][0]
			flag[a[ptr1][2]] = 1
			ptr1 += 1
		turn = 1
	else:
		while flag[b[ptr2][2]] == 1:
			ptr2 += 1
		if ptr2 < n:
			ans2 += b[ptr2][0]
			flag[b[ptr2][2]] = 1
			ptr2 += 1
		turn = 0
print ans1-ans2

Submission status: Runtime Error

Solution code:
#include"bits/stdc++.h"

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k,m,n) for(ll (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<< #str << " " str<<endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007;// 10^9+7

int N;
vector<ll> A, B;
vector<pair<ll, ll>> diff;

//���C���֐�
int main()
{
	cin >> N;
	A.resize(N);
	B.resize(N);
	diff.resize(N);
	REP(i, N)cin >> A[i] >> B[i];
	REP(i, N)diff[i] = make_pair(A[i] + B[i], i);
	sort(diff.begin(), diff.end());
	reverse(diff.begin(), diff.end());

	ll tak = 0, ao = 0;
	REP(i, N)
	{
		auto p = diff[i];
		if(i % 2 == 0)tak += A[p.second];
		else ao += B[p.second];
	}
	cout << tak - ao << endl;
	//cerr << tak << " " << ao << endl;

	cin >> N;
	return 0;
}