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 (Python):
import sys
import numpy as np

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  
sys.setrecursionlimit(10 ** 7)

N = ri()
A = []
B = []

for i in range(N):
    a, b = rl()
    A.append(a)
    B.append(b)

A = np.array(A)
B = np.array(B)
diff = A + B
D = []
for i, x in enumerate(diff):
    D.append((x, i))

D.sort(reverse=True)
A = list(A)
B = list(B)
ta = 0
ao = 0
for x, i in D[::2]:
    ta += A[i]
for x, i in D[1::2]:
    ao += B[i]

print(ta-ao)

Solution (Python):
N=int(input())
A=[0]*N
B=[0]*N
C=[0]*N
for i in range(N):
    a, b = map(int,input().split())
    A[i], B[i], C[i] = a, b, a+b

index = sorted(range(len(C)), key=lambda k: C[k], reverse=True)

sum_=0
for i,idx in enumerate(index):
    if i % 2 ==0:
        sum_+=A[idx]
    else:
        sum_-=B[idx]

print(sum_)

Solution (Python):
n = int(input())
b_sum = 0
data = []
for i in range(n):
    a, b = map(int, input().split())
    b_sum += b
    data.append(a + b)
data = sorted(data, reverse = True)
print(sum(data[0:n:2]) - b_sum)

Solution (Python):
n=int(input())
ans=0
c=[]
for i in range(n):
	x,y=map(int,input().split())
	ans+=x
	c.append(x+y)
c.sort()
c.reverse()
for i in range(n):
	if i%2!=0:
		ans-=c[i]
print(ans)

Solution (Python):
N = int(input())
AB = [list(map(int,input().split())) for _ in range(N)]
ans = 0
S = []
for i in AB:
  ans -= i[1]
  S.append(i[0] + i[1])
S = sorted(S)[::-1]
for i in range(0,N,2):
  ans += S[i]
print(ans)

Solution (Python):









n= int(input())

tup = []
for i in range(0,n):
    tmp = input().split()
    tmpa,tmpb = list(map(lambda a: int(a), tmp))
    tup.append([tmpa+tmpb,tmpa,tmpb])


tup=sorted(tup, reverse=True, key=lambda x:x[0])


total=0
for i in range(0,n):
    if (i%2==0):
       total+=tup[i][1]
    else:
       total-=tup[i][2]
          
print(total)

Solution (Python):
n=int(input())
c=[list(map(int, input().split())) for i in range(n)]

for i in range(n):
    c[i].append(c[i][0]+c[i][1])

c.sort(key=lambda x:x[2],reverse=True)

ans1=0
ans2=0

for i in range(n):
    if i%2==0:
        ans1+=c[i][0]
    else:
        ans2+=c[i][1]

print(ans1-ans2)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random

import os
 




 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    A = []
    B = []
    sai = []
    sai2 = []
    for i in range(N):
        a,b = inputMap()
        A.append(a)
        B.append(b)
        sai.append(a+b)
        sai2.append((a+b, i))
        
    sai2.sort()
    sai2 = sai2[::-1]
    
    takahashi = 0
    aoki = 0
    for i,val in enumerate(sai2):
        val, no = val
        if i == 0:
            takahashi += A[no]
        else:
            if i % 2 == 1:
                aoki += B[no]
            else:
                takahashi += A[no]
            
    print(takahashi - aoki)
                
    
if __name__ == "__main__":
	main()

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
ab = [0] * n
for i in range(n):
    a,b = inpl()
    ab[i] = (a+b,a,b)
ab.sort(reverse=True)

x = 0
y = 0
for i,j in enumerate(ab):
    if i%2:
        y += j[2]
    else:
        x += j[1]
print(x-y)

Solution (Python):
n=int(input())
l=[list(map(int,input().split())) for _ in range(n)]
l.sort(key=lambda x:x[0]+x[1],reverse=True)

print(sum([l[i][0] for i in range(n) if i % 2 == 0]) - sum([l[i][1] for i in range(n) if i % 2 == 1]))

Solution (Python):
import sys
input = sys.stdin.readline


N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())


C = [a+b for a, b in zip(A, B)]
C.sort(reverse = True)
accum = 0
for i in range(N):
    if i % 2 == 0:
        accum += C[i]
answer = accum - sum(B)
print(answer)

Solution (Python):
import numpy as np

n = int(input())
arr = []

for i in range(n):
    arr.append(list(map(int, input().split(' '))))

arr = np.array(arr)
arr = arr[np.argsort(np.sum(arr, axis=1))[::-1]]
takahashi = np.sum(arr[::2, 0], axis=0)
aoki = np.sum(arr[1::2, 1], axis=0)

print(takahashi - aoki)

Solution (Python):
N = int(input())
AB = [list(map(int, input().split())) for i in range(N)]
sum_B = sum([b for a, b in AB])
ans = sum(sorted([a+b for a, b in AB], reverse=True)[::2]) - sum_B
print(ans)

Solution (Python):
N = int(input())
AB = [list(map(int,input().split())) for _ in range(N)]
C = [[A+B, A, B] for A, B in AB]
C.sort(reverse=True)
ans = 0
for i, (c, a, b) in enumerate(C):
    if i % 2 == 0:
        ans += a
    else:
        ans -= b
print(ans)

Solution (Python):
N=int(input())
C=[list(map(int,input().split())) for i in range(N)]
C.sort(key=lambda x:sum(x), reverse=True)
ans=0
for i in range(N):
  if i%2==0:
    ans+=C[i][0]
  else:
    ans-=C[i][1]
print(ans)

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append((a+b,a,b))
l.sort(reverse=True)
c1=0
c2=0
for i in range(n):
    if i%2==0:
        c1+=l[i][1]
    else:
        c2+=l[i][2]
print(c1-c2)

Solution (Python):
import numpy as np
 
N = int(input())
 
A = [0 for i in range(N)]
B = [0 for i in range(N)]
C = [0 for i in range(N)]
 
for i in range(N):
    A[i], B[i] = map(int, input().split())
    C[i] = A[i] + B[i]
 
C.sort(reverse=True)
 
ans = 0
 
for i in range(N):
    if i % 2 == 0:
        ans += C[i]
 
print(ans - sum(B))

Solution (Python):
n=int( input())
a=[0]*n
b=[0]*n
for i in range(n):
    a[i],b[i] = map(int,input().split() )
c=[[a[i],b[i],a[i]+b[i]] for i in range(n)]
c.sort(reverse=True,key=lambda x:x[2])
tsum=sum(c[i*2][0] for i in range((n+1)//2))
asum=sum(c[i*2+1][1] for i in range(n//2))
print( tsum- asum )

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
N = int(input())
CAB = []
for _ in range(N):
    A, B = inpl()
    CAB.append((A+B,A,B))
CAB.sort(reverse=True)
ans = sum(CAB[i][1] for i in range(0,N,2)) - sum(CAB[i][2] for i in range(1,N,2))
print(ans)

Solution (Python):
def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n)]

    ab.sort(reverse=True, key=lambda x: x[0] + x[1])

    t = sum(map(lambda x: x[0], ab[::2]))
    a = sum(map(lambda x: x[1], ab[1::2]))

    ans = t - a

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n)]

    ab.sort(reverse=True, key=lambda x: x[0] + x[1])

    t = 0
    a = 0

    for i in range(0, n, 2):
        t += ab[i][0]
    for i in range(1, n, 2):
        a += ab[i][1]

    ans = t - a

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
H = []
for i in range(n):
    a,b = input().split()
    H += [[int(a)+int(b),int(a),int(b)]]
H.sort(reverse=True)
ans = 0
for i in range(n):
    if i%2 == 0:
        ans += H[i][1]
    else:
        ans -= H[i][2]
print(ans)

Solution (Python):


N=int(input())

A=[]
B=[]
C=[]
for _ in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(a+b)

s_A=sum(A)
s_B=sum(B)
C.sort(reverse=True)

ans=s_A
for i in range(N)[1::2]:
    ans-=C[i]

print(ans)

Solution (Python):
n = int(input())
x = []
y = 0
for i in range(n):
    a, b = map(int, input().split())
    y += b
    x.append(a + b)
X = sorted(x)[::-1]
print(sum(X[::2]) - y)

Solution (Python):
import heapq
N=int(input())
ab=[list(map(int,input().split())) for _ in range(N)]
sab=[[-ab[i][0]-ab[i][1],i] for i in range(N)]
heapq.heapify(sab)

ans=0
for i in range(N):
  if i%2==0:
    ind=heapq.heappop(sab)[1]
    ans+=ab[ind][0]
  else:
    ind=heapq.heappop(sab)[1]
    ans-=ab[ind][1]
    
print(ans)

Solution (Python):
import sys
from operator import itemgetter
read = sys.stdin.read


def main():
    N, *AB = map(int, read().split())
    A, B = zip(*sorted(zip(*[iter(AB)] * 2), key=sum, reverse=True))
    print(sum(A[::2]) - sum(B[1::2]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from operator import itemgetter
read = sys.stdin.read


def main():
    N, *AB = map(int, read().split())
    A = AB[::2]
    B = AB[1::2]
    ab = [a + b for a, b in zip(A, B)]
    ab, A, B = zip(*sorted(zip(ab, A, B), key=itemgetter(0), reverse=True))

    print(sum(A[::2]) - sum(B[1::2]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from operator import itemgetter
read = sys.stdin.read

N, *AB = map(int, read().split())
A = AB[::2]
B = AB[1::2]
ab = [a + b for a, b in zip(A, B)]
ab, A, B = zip(*sorted(zip(ab, A, B), key=itemgetter(0), reverse=True))
print(sum(A[::2]) - sum(B[1::2]))

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
d = [list(map(int, input().split())) for _ in range(N)]
d.sort(key = lambda x: (x[0] + x[1]))
t = 0
a = 0
for i in range(N):
  if i % 2:
    a += d.pop()[1]
  else:
    t += d.pop()[0]
print(t - a)

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
l=sorted(l,key=lambda x:-x[0]-x[1])
a=0
b=0
for i in range(n):
    if i%2==0:
        a+=l[i][0]
    else:
        b+=l[i][1]
print(a-b)

Solution (Python):
n=int(input())
la=[]
lb=[]
for i in range(n):
  a,b=map(int,input().split())
  la.append(a)
  lb.append(b)
l=[]
for i in range(n):
  l.append([la[i]+lb[i],i])
l=sorted(l)
l=l[::-1]
now=1
countt=0
counta=0
for i in range(n):
  if now==1:
    countt+=la[l[i][1]]
  else:
    counta+=lb[l[i][1]]
  now*=-1
print(countt-counta)

Solution (Python):
N=int(input())
AB=[list(map(int, input().split())) for _ in range(N)]

AB.sort(key=lambda x:(x[0]+x[1]),reverse=True)

s=0
for i,v in enumerate(AB):
    if i%2:
        s = s - v[1]
    else:
        s = s + v[0]
print(s)

Solution (Python):
n = int(input())
dish = [tuple(map(int, input().split())) for i in range(n)]
dishNum = [[i, sum(dish[i])] for i in range(n)]
dishNum.sort(key=lambda x: x[1])
ans = 0
for i in range(n):
    w = dishNum.pop()
    y = w[0]
    if i % 2 == 0:
        ans += dish[y][0]
    else:
        ans -= dish[y][1]
print(ans)

Solution (Python):
import sys
from heapq import *

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1

def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n)]
    hp = []
    for a, b in ab:
        heappush(hp, [-a - b, a, b])
    ans = 0
    turn = 1
    while hp:
        mx, a, b = heappop(hp)
        if turn:
            ans += a
        else:
            ans -= b
        turn = 1 - turn
    print(ans)

main()

Solution (Python):
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x: sum(x), reverse=True)
s = sum(a for a, _ in ab)
s -= sum(sum(dish) for dish in ab[1::2])
print(s)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n)]
for i in range(n):
    ab[i].append(ab[i][0]+ab[i][1])
ab.sort(key=lambda x:x[2],reverse=True)
ta=0
ao=0
for i in range(n):
    if i%2==0:
        ta+=ab[i][0]
    else:
        ao+=ab[i][1]

print(ta-ao)

Solution (Python):
from operator import itemgetter

N = int(input())
L = [[int(a) for a in input().split()] for _ in range(N)]

for i in range(N):
    L[i].append(L[i][0]+L[i][1])
    
L.sort(reverse=True, key=itemgetter(2))
a = 0
b = 0
for i in range(N):
    if i%2 == 0:
        a += L[i][0]
    else:
        b += L[i][1]
        
print(a-b)

Solution (Python):
import numpy as np
N = int(input())
data = [list(map(int,input().split())) for _ in range(N)]
list2 = []
for i in range(N):
    happy = data[i][0]+data[i][1]
    list2.append(happy)
S = np.array(list2)
S2 = S.argsort()[::-1]
glad = 0
for i in range(N):
    if i%2==0:
        glad += data[S2[i]][0]
    else:
        glad -= data[S2[i]][1]
print(glad)

Solution (Python):
import sys
from collections import Counter, deque, defaultdict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
sumlist = [(0,0)]*n
res = 0
for i in range(n):
    aa,bb = MAP()
    sumlist[i] = (aa, bb)

sumlist = sorted(sumlist, key = lambda x:x[0]+x[1], reverse = True)
for i in range(n):
    res += (-1)**i * sumlist[i][i%2]

print(res)

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append([a+b,a,b])
l.sort(reverse=True)
t=0
a=0
for i in range(n):
    if i%2==0:
        t+=l[i][1]
    else:
        a+=l[i][2]
print(t-a)

Solution (Python):
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]

BSUM = sum([i[1] for i in AB])

AplsB = [i + j for i, j in AB]

AplsB.sort(reverse=True)

ans = 0
for i, v in enumerate(AplsB):
  if i % 2 == 0:
    ans += v

print(ans - BSUM)

Solution (Python):
import sys
n = int(input())
ls = []
raw_ls = []
for i in range(n):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    ls.append([a + b, a, b])
res = 0
for i, x in enumerate(sorted(ls, key=lambda x:x[0], reverse=True)):
    if i % 2 == 0:
        res += x[1]
    else:
        res -= x[2]
print(res)

Solution (Python):
from heapq import heappush, heappop
X = []
N = int(input())
dic = {}
for i in range(N):
  a, b = map(int, input().split())
  heappush(X,(-(a+b),a,b))
count = 0
T = 0
A = 0
while X:
  m, a, b = heappop(X)
  if count%2==0:
    T += a
  else:
    A += b
  count += 1

print(T-A)

Solution (Python):
from operator import itemgetter

N = int(input())
score = []
for _ in range(N):
    a, b = map(int, input().split())
    score.append((a, b, a+b))

score.sort(key=itemgetter(2), reverse=True)

ans = 0
for i in range(N):
    if i%2 == 0:
        ans += score[i][0]
    else:
        ans -= score[i][1]
print(ans)

Solution (Python):
n,*L=map(int,open(0).read().split())
s=sorted((a+b,a,-b)for a,b in zip(*[iter(L)]*2))
print(sum(s[~i][i%2+1]for i in range(n)))

Solution (Python):
n,*L=map(int,open(0).read().split())
s=sorted((a+b,a,b) for a,b in zip(*[iter(L)]*2))
ans=0
for i,(_,a,b) in enumerate(s[::-1]):
	if i%2:
		ans-=b
	else:
		ans+=a
print(ans)

Solution (Python):

N = int(input())

lis = []

for i in range(N):

    A,B = map(int,input().split())

    lis.append([A+B,A,B])

lis.sort()
lis.reverse()

T = 0
K = 0

for i in range(N):

    if i % 2 == 0:
        T += lis[i][1]
    else:
        K += lis[i][2]

print (T-K)

Solution (Python):
n=int(input())
l=[]
c=0
for i in range(n):
    a,b=map(int,input().split())
    c-=b
    l.append(a+b)
l.sort()
l=l[::-1]
for i in range(0,n,2):
    c+=l[i]
print(c)

Solution (Python):
n=int(input())
lists=[]
lists1=[]
for i in range(n):
    a,b=map(int,input().split())
    lists.append((a+b,a,b))
listed=sorted(lists,key=lambda x:x[0],reverse=True)

ans=0
for n in range(n):
    if n%2==0:
        ans+=listed[n][1]
    
    elif n%2!=0:
        ans-=listed[n][2]
print(ans)

Solution (Python):
n = int(input())
l = [list(map(int, input().split())) for _ in range(n)]
l.sort(key=lambda x:x[0]+x[1], reverse=True)
ans = 0
for i in range(n):
  if i % 2:
    ans -= l[i][1]
  else:
    ans += l[i][0]
print(ans)

Solution (Python):
n=int(input())
li=[list(map(int,input().split())) for _ in range(n)]
c=[0 for i in range(n)]
ans=0
j=0
for i in li:
    c[j]=sum(i)
    ans-=i[1]
    j+=1
c.sort(reverse=True)
ans+=sum(c[::2])
print(ans)

Solution (Python):
def main():
    n = int(input())
    ab = []
    for i in range(n):
        a, b = map(int, input().split(' '))
        ab.append([a+b, a, b])
    ab.sort(reverse = True)
    result = 0
    for i in range(n):
        if i%2 == 0:
            result += ab[i][1]
        else:
            result -= ab[i][2]
    print(result)
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
ab = []
for i in range(n):
    a, b = map(int, input().split(' '))
    ab.append([a+b, a, b])
ab.sort(reverse = True)
result = 0
for i in range(n):
    if i%2 == 0:
        result += ab[i][1]
    else:
        result -= ab[i][2]
print(result)

Solution (Python):
def main():
    n = int(input())
    df = []
    for _ in range(n):
        a, b = map(int, input().split(' '))
        df.append([a+b, a, b])
    df.sort(reverse = True)
    result = 0
    for i in range(n):
        if i%2 == 0:
            x = df.pop(0)
            result += x[1]
        else:
            x = df.pop(0)
            result -= x[2]
    print(result)
if __name__ == '__main__':
    main()

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()


N = int(input())
CAB = []
for i in range(N):
	A, B = list(map(int, input().split()))
	C = A + B
	CAB.append((C, A, B))

CAB.sort(reverse=True)

ans = 0
for i in range(N):
	if i % 2 == 0:
		ans += CAB[i][1]
	else:
		ans -= CAB[i][2]

print(ans)

Solution (Python):
n=int(input())
li=[]
for i in range(n):
  a,b=map(int,input().split())
  li.append([a,b,a+b])
lst=sorted(li,key=lambda x:x[2],reverse=True)

i=0
ans=0
try:
  while True:
    if i%2==0:ans+=lst[i][0]
    else:ans-=lst[i][1]
    i+=1
except:
  pass
    
    
print(ans)

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

Solution (Python):
n = int(input())

food = []
for i in range(n):
    a,b = map(int, input().split())
    food.append([a+b,a,b])
food.sort()
food.reverse()

ans = 0
for i in range(n):
    ans += (-1)**i * food[i][i%2+1]
print(ans)

Solution (Python):
N = int(input())

L = []
for i in range(N):
  A, B = list(map(int, input().split()))
  L.append([A + B, A, B])
L = sorted(L)
a = 0
b = 0
for i in range(N):
  if i % 2 == 0:
    a += L[-1 - i][1]
  else:
    b += L[-1 - i][2]
print(a - b)

Solution (Python):

def main():
    
    
    N, *AB = map(int, open(0).read().split())
    A = [(i, j) for i, j in zip(*[iter(AB)] * 2)]
    A.sort(key=sum, reverse=1)
    ans = sum(i for i, j in A[::2]) - sum(j for i, j in A[1::2])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
ab = []
for _ in range(n):
  a,b = map(int,input().split())
  ab.append([a,b])
ab.sort(key = lambda x:x[0]+x[1] ,reverse = True)
ans = sum([x[0]+x[1] for x in ab[0:n:2]])-sum([x[1] for x in ab])
print(ans)

Solution (Python):
N = int(input())
ABs = []
for i in range(N):
  A, B = map(int, input().split())
  ABs.append((A, B))
ABs.sort(key=lambda AB: sum(AB), reverse=True)
r = sum([AB[0] for AB in ABs[0::2]])-sum([AB[1] for AB in ABs[1::2]])
print(r)

Solution (Python):
N = int(input())
H = []
for i in range(N):
  A,B = map(int,input().split())
  H.append([A+B,[A,B]])
H.sort(reverse=True)

ans = 0
factor = 1
for i in range(N):
  ans += H[i][1][i%2] * factor
  factor *= -1
print(ans)

Solution (Python):
N = int(input())

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

takahashi = 0
aoki = 0
dish.sort(reverse=True)

for i, (diff, a, b) in enumerate(dish):
    if i % 2 == 0:
        takahashi += a
    else:
        aoki += b

print(takahashi - aoki)

Solution (Python):
N = int(input())

dish = []
for _ in range(N):
    A, B = map(int, input().split())
    dish.append((A, B))

dish = sorted(dish, key=lambda x: x[0]+x[1], reverse=True)

ans = 0
for i in range(N):
    if i % 2 == 0:
        ans += dish[i][0]
    else:
        ans -= dish[i][1]
print(ans)

Solution (Python):
n=int(input())
meal=[]
snk=0
for _ in range(n):
    a,b=map(int,input().split())
    snk-=b
    meal.append(a+b)

meal.sort(reverse=True)

ans=sum(meal[0::2])+snk
print(ans)

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque


def main():
    num = int(input())
    data = [list(map(int, input().split())) for i in range(num)]

    aaa = [0 for i in range(num)]
    for i in range(num):
        aaa[i] = (data[i][0] + data[i][1], i)
    aaa.sort(reverse=True)

    ans = 0
    
    for i in range(num):
        ind = aaa[i][1]
        if i % 2:
            ans -= data[ind][1]
        else:
            ans += data[ind][0]

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):

def main():

    N = int(input())
    food1, food2 = [], []
    for i in range(N):
        a, b = map(int, input().split())
        food1.append([a+b, a, b, i])
        food2.append([-a-b, a, b, i])
    food1.sort()
    food2.sort(reverse = True)
    used = set()
    ans = 0
    while food1 or food2:
        while food1 and food1[-1][3] in used:
            food1.pop()
        if food1:
            _, a, _, i = food1.pop()
            used.add(i)
            ans += a
        while food2 and food2[-1][3] in used:
            food2.pop()
        if food2:
            _, _, b, i = food2.pop()
            used.add(i)
            ans -= b
    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline
n = int(input())
abn = []
for i in range(n):
  a,b = map(int,input().split())
  abn.append([a+b,a,b])
abn.sort()
cn = np.array(abn)
print(sum(cn[-1::-2,1])-sum(cn[-2::-2,2]))

Solution (Python):
N=int(input())
AB=[list(map(int,input().split())) for _ in range(N)]
print(sum([-b for a, b in AB])+sum(sorted([a+b for a,b in AB],reverse=True)[::2]))

Solution (Python):
from functools import lru_cache
N = int(input())

A = []
B = []
food = []

for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    food.append((a + b, i))

food.sort(reverse=True)
X = 0
Y = 0
cnt = 0

for _, i in food:
    if cnt % 2 == 0:
        X += A[i]
    else:
        Y += B[i]
    cnt += 1

print(X - Y)

Solution (Python):
n,*t=map(int,open(0).read().split());t=sorted(zip(t[::2],t[1::2]),key=lambda v:v[0]+v[1]);print(sum(a for a,_ in t[::-2])-sum(b for _,b in t[-2::-2]))

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
ab = [list(li()) for _ in range(n)]
ab.sort(key=lambda x: x[0] + x[1], reverse=True)

first = 0
second = 0

for i, (ai, bi) in enumerate(ab):
    if i%2 == 0:
        first += ai
    else:
        second += bi

print(first - second)

Solution (Python):
n=int(input())
l=[0 for i in range(n)]
for i in range(n):
    a,b=map(int,input().split())
    l[i]=a,b,a+b
l=sorted(l,key=lambda x:-x[2])
ans=0
for num,i in enumerate(l):
    if num%2==0:ans+=i[0]
    else:ans-=i[1]
print(ans)

Solution (Python):

def main():
    N = int(input())
    A = sorted([tuple(map(int, input().split())) for _ in range(N)], key=sum, reverse=1)
    ans = sum(i for i, j in A[::2]) - sum(j for i, j in A[1::2])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    
    
    N = int(input())
    A = sorted([tuple(map(int, input().split())) for _ in range(N)], key=sum, reverse=1)
    ans = sum(i for i, j in A[::2]) - sum(j for i, j in A[1::2])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    N = int(input())
    A = tuple(tuple(map(int, input().split())) for _ in range(N))
    a = sorted(A, key=sum, reverse=1)
    ans = sum(i for i, j in a[::2]) - sum(j for i, j in a[1::2])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
l = [tuple(map(int,input().split())) for _ in range(n)]
l.sort(key=lambda t:-sum(t))
ta = sum(x[0] for x in l[::2])
ao = sum(x[1] for x in l[1::2])
print(ta-ao)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
ab=[list(map(int,input().split())) for i in range(n)]
newab=[ab[i][0]+ab[i][1] for i in range(n)]
newab.sort()

ans=0
for i in range(n):
    if i%2==0:
        ans+=newab[-i-1]

for i in range(n):
    ans-=ab[i][1]
print(ans)

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
ABs = []
for _ in range(N):
    A,B = inpl()
    ABs.append([A+B,A,B])

ABs.sort(reverse=True)
t = a = 0

for i,[AB,A,B] in enumerate(ABs):
    if i%2 == 0:
        t += A
    else:
        a += B

print(t-a)

Solution (Python):

N = int(input())
AB = [list(map(int,input().split())) for _ in range(N)]

C = [0] * N
for i in range(N):
    C[i] = sum(AB[i])   
D = sorted(zip(C,range(len(C))),reverse = True)
a = 0
b = 0
for i in range(N):
    if i%2 == 0:a += AB[D[i][1]][0]
    if i%2 == 1:b += AB[D[i][1]][1]
    
print(a-b)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

N = int(input())
A,B = np.array([input().split() for _ in range(N)],dtype=np.int64).T

answer = A.sum() 
improve = A+B 
improve.sort()
answer -= improve[::-1][1::2].sum()
print(answer)

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

AB.sort(reverse = True)
ans = 0
for i in range(N):
    if i%2 == 0:
        ans += AB[i][1]
    else:
        ans -= AB[i][2]
print(ans)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
ab = [LI() for _ in range(n)]
ab = sorted(ab,key = lambda x:x[0]+x[1],reverse = True)
ans = 0
for i in range(n):
    a,b = ab[i]
    if i % 2:
        ans -= b
    else:
        ans += a
print(ans)

Solution (Python):
N = int(input())

lst  = []
l_append = lst.append
ans = 0
for i in range(N):
    A, B = map(int, input().split())
    l_append(A + B)
    ans -= B

lst.sort(reverse = True)

for i in range(N):
    if i % 2 == 0:
        ans += lst[i]
    else:
        pass

print (ans)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())

lst_A = []
l_A_append = lst_A.append
lst_B = []
l_B_append = lst_B.append
for i in range(N):
    A, B = map(int, input().split())
    l_A_append([A + B, A, B, i])
    l_B_append([A + B, B, A, i])
    
lst_A.sort(reverse = True)
lst_B.sort(reverse = True)

ans = 0
check_lst = [False] * N

index_A = 0
index_B = 0
count = 0
while count < N:
    if count % 2 == 0: 
        while check_lst[lst_A[index_A][3]]: 
            index_A += 1
        ans += lst_A[index_A][1]
        check_lst[lst_A[index_A][3]] = True
        index_A += 1
    else: 
        while check_lst[lst_B[index_B][3]]: 
            index_B += 1
        ans -= lst_B[index_B][1]
        check_lst[lst_B[index_B][3]] = True
        index_B += 1
    count += 1
print (ans)

Solution (Python):
n=int(input())
a=[list(map(int,input().split())) for _ in range(n)]
b=[(i,sum(j)) for i,j in enumerate(a)]
b.sort(key=lambda x:x[1],reverse=True)
ans=0
for i in range(n):
	ans+=a[b[i][0]][i%2]*(-1)**(i%2)
print(ans)

Solution (Python):
N = int(input())
A, B, C = [0]*N, [0] * N, [0] * N
bsum = 0
for i in range(N):
    A[i], B[i] = map(int, input().split())
    C[i] = A[i] + B[i]
    bsum += B[i]

C = sorted(C, reverse=True)
ans = -bsum
for i in range(len(C)):
    if(i % 2 == 0):
        ans += C[i]
    else:
        continue
print(ans)