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: Accepted

Solution code:
n = int(input())

e = []
curr = 0
for _ in range(n):
    a, b = map(int, input().split())
    e.append(a + b)
    curr -= b

e.sort(reverse=True)
print(curr + sum(e[::2]))

Submission status: Accepted

Solution code:
#料理が2個の時を考え、どちらを取れば良いか考える　これで得られた基準でソート
import sys
input = sys.stdin.readline

N = int(input())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda k: k[0]+k[1], reverse=True)
takahashi, aoki = 0, 0

for i in range(N):
    if i%2==0:
        takahashi += AB[i][0]
    else:
        aoki += AB[i][1]

print(takahashi-aoki)

Submission status: Accepted

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

A = []
C = []
for a, b in AB:
  A.append(a)
  C.append(a + b)
#print("C:", C)

C.sort()
C = C[::-1]

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

Submission status: Accepted

Solution code:
N=int(input())
A=[0]*N
B=[0]*N
diff=[0]*N
for i in range(N):
  A[i],B[i]=map(int,input().split())
  diff[i]=[A[i]+B[i],i]
  
diff=sorted(diff,key=lambda x:x[0])[::-1]
taka=0
aoki=0
for i in range(len(diff)):
  if i%2==0:
    taka+=A[diff[i][1]]
  else:
    aoki+=B[diff[i][1]]

print(taka-aoki)

Submission status: Accepted

Solution code:
N = int(input())
AB = []
ans = 0
for _ in range(N):
  a,b = map(int,input().split())
  AB.append(a+b)
  ans -= b
AB.sort(reverse=True)

print(ans+sum(AB[::2]))

Submission status: Accepted

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

C.sort(reverse=1)

ans = 0
for i in range(N):
    _, a, b = C[i]
    if i % 2 == 0:
        ans += a
    else:
        ans -= b
print(ans)

Submission status: Accepted

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

scores = [(A+B, A, B) for A,B in AB]
scores.sort(key=lambda x:x[0], reverse=True)
takahashi = 0
aoki = 0
takahashi_turn = True
for score in scores:
    if takahashi_turn:
        takahashi += score[1]
    else:
        aoki += score[2]
    takahashi_turn = not takahashi_turn
print(takahashi-aoki)

Submission status: Accepted

Solution code:
N = int(input())
cuisine = []
ans = 0
for _ in range(N):
    a, b = map(int, input().split())
    ans -= b
    cuisine.append(a+b)
cuisine = sorted(cuisine, reverse=True)
for i in range(N):
    if i % 2 == 1:
        continue
    ans += cuisine[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
l=[]
for i in range(N):
    A,B=map(int,input().split())
    l.append((A,B))
l.sort(reverse=True,key=lambda x:(x[0]+x[1],x[0]))
ans=0
for i,e in enumerate(l):
    if i%2==0:
        ans+=e[0]
    elif i%2==1:
        ans+=-e[1]
print(ans)

Submission status: Accepted

Solution code:
def examC():
    N = I()
    A = [0]*N; B = [0]*N
    for i in range(N):
        A[i], B[i] = LI()
    taka = 0; aoki = 0
    value = [[i] for i in range(N)]
    for i in range(N):
        cur = A[i]+B[i]
        value[i].append(cur)
    value.sort(key=lambda x:x[1],reverse = True)
#    print(value)
    for i in range(N):
        if i%2==0:
#            print(A[value[i][0]])
            taka += A[value[i][0]]
        else:
#            print(B[value[i][0]])
            aoki += B[value[i][0]]
    ans = taka-aoki
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = []
    for i in range(N):
        a, b = map(int, input().split())
        A.append((a+b, a, b))
    A.sort(reverse=True)
    takahashi = aoki = 0
    for i, (_, a, b) in enumerate(A):
        if i % 2 == 0:
            takahashi += a
        else:
            aoki += b
    print(takahashi - aoki)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())
A, B = list(zip(*[map(int, sys.stdin.buffer.readline().split()) for _ in range(N)]))

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)

order = (A + B).argsort()[::-1]
a = A[order[::2]].sum()
b = B[order[1::2]].sum()
print(a - b)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    AB=[]
    BA=[]
    for i in range(N):
        a,b = MI()
        AB.append((a,b,i))
        BA.append((b,a,i))
    AB.sort(key = lambda x:x[0]+x[1],reverse = False)
    BA.sort(key = lambda x:x[0]+x[1],reverse = False)
    takahashi = 0
    aoki = 0
    eaten = set()
    for i in range(N):  
        if i&1:#青木
            a,b,c = BA.pop()
            while c in eaten:
                a,b,c = BA.pop()
            aoki += a
        else:
            a,b,c = AB.pop()
            while c in eaten:
                a,b,c = AB.pop()
            takahashi += a
        eaten.add(c)
    ans = takahashi-aoki
    print(ans)

    

    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
h = []
for _ in range(N) :
    A, B = map(int, input().split())
    h.append((A + B, A, B))
    
h.sort(reverse=True)
print(sum([h[i][1] for i in range(0, N, 2)]) - sum([h[i][2] for i in range(1, N, 2)]))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n=inp()
    ab=[]
    for _ in range(n):
        x,y=inpm()
        ab.append((x+y,x,y))
    ab.sort(reverse=True)
    pa=0
    pb=0
    for i in range(n):
        if i%2 == 0:
            pa+=ab[i][1]
        else:
            pb+=ab[i][2]
    print(pa-pb)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()  # ignore trailing spaces
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)

Submission status: Accepted

Solution code:
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_)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#"""
#Created on Wed Nov 20 17:37:14 2019
#
#@author: manakamura
#"""

#https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_c

n= int(input())
#s= 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])

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

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
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()

Submission status: Accepted

Solution code:
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)
# print(ab)
x = 0
y = 0
for i,j in enumerate(ab):
    if i%2:
        y += j[2]
    else:
        x += j[1]
print(x-y)

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

#output
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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 )

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)