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:
# nikkei2019-qualC - Different Strokes
def main():
    # player gets ai <-> point spread increases by ai + bi -> sort A by ai + bi
    # player gets Ai whose index is even / rival gets Ai whose index is odd
    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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)
#print(H)
ans = 0
factor = 1
for i in range(N):
  ans += H[i][1][i%2] * factor
  factor *= -1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
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
    # print(aaa)
    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()
    # test()

Submission status: Accepted

Solution code:

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())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# nikkei2019-qualC - Different Strokes
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()

Submission status: Accepted

Solution code:
# nikkei2019-qualC - Different Strokes
def main():
    # player gets ai <-> point spread increases by ai + bi -> sort A by ai + bi
    # player gets Ai whose index is even / rival gets Ai whose index is odd
    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()

Submission status: Accepted

Solution code:
# nikkei2019-qualC - Different Strokes
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()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()
# print(newab)
ans=0
for i in range(n):
    if i%2==0:
        ans+=newab[-i-1]
# print(ans)
for i in range(n):
    ans-=ab[i][1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

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

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)
ans = 0
for i in range(N):
    if i%2 == 0:
        ans += AB[i][1]
    else:
        ans -= AB[i][2]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#全国統一プログラミング王決定戦予選 C

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)
# print (lst)
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を増やす
            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を増やす
            index_B += 1
        ans -= lst_B[index_B][1]
        check_lst[lst_B[index_B][3]] = True
        index_B += 1
    count += 1
print (ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#%%
import time

import numpy as np

N = int(input())
A = [0] * N
B = [0] * N
C = [[] for _ in range(N) ]
takahashi = []
aoki = []

start = time.time()

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


D = np.array(A) + np.array(B)
for i in range(N):
  C[i] = [i, D[i]]


elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
start = time.time()

C = sorted(C, key=lambda x:x[1])


elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
start = time.time()


if N %2 == 0 :
  for i in range(1,N,2):
    tmp_max = C[-i][0]
    takahashi.append(A[tmp_max])
    tmp_max = C[-i-1][0]
    aoki.append(B[tmp_max])

elif N %2 != 0 :
  for i in range(1,N-1,2):
    tmp_max = C[-i][0]
    takahashi.append(A[tmp_max])
    tmp_max = C[-i-1][0]
    aoki.append(B[tmp_max])
  tmp_max = C[-N][0]
  takahashi.append(A[tmp_max])

print(sum(takahashi)-sum(aoki))

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
    A[i], B[i] = map(int, input().split())

a = np.array(A)
b = np.array(B)
c = np.zeros(N)

# cはaとbの要素をすべて足したもの
c = a + b
# 降順にソート
csorted = np.sort(c)[::-1]
# bの総和
bsum = np.sum(b)
# csortedの奇数番目を足していって最後にbsumの値を引けば求まる。
csum = 0
for i in range(N):
    if i % 2 == 0:
        csum += int(csorted[i])

ans = csum - bsum

print(ans)

Submission status: Accepted

Solution code:

import numpy as np
N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
    A[i], B[i] = map(int, input().split())
 
a = np.array(A)
b = np.array(B)
 
# cはaとbの要素をすべて足したもの
c = a + b
# 降順にソート
csorted = np.sort(c)[::-1]
# bの総和
bsum = np.sum(b)
# csortedの奇数番目を足していって最後にbsumの値を引けば求まる。
csum = 0
for i in range(N):
    if i % 2 == 0:
        csum += int(csorted[i])
 
ans = csum - bsum
 
print(ans)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    a = []
    b = []
    s = []
    for i in range(n):
        atmp,btmp = map(int,input().split())
        a.append(atmp)
        b.append(btmp)
        s.append([atmp+btmp,i])


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

    rem = [1 for i in range(n)]

    takahashi = 0
    aoki = 0

    for e,i in enumerate(s):
        if not e % 2:
            takahashi += a[i[1]]
        else:
            aoki += b[i[1]]
        

    print(takahashi-aoki)




main()

Submission status: Accepted

Solution code:
N = int(input())
AB = sorted((a+b,a,b) for a,b in [map(int,input().split()) for _ in [0]*N])[::-1]
ans = sum(map(lambda x:x[0],AB[::2]))-sum(map(lambda x:x[2],AB))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
value = []
for i in range(N):
    A, B = map(int, input().split())
    value.append([A+B, A, B])

value.sort(reverse=True)
takahashi = 0
aoki = 0
flag = 1
for i in range(N):
    if flag == 1:
        takahashi += value[i][1]
    else:
        aoki += value[i][2]
    flag *= -1
print(takahashi - aoki)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
AB=[]
append=AB.append
for i in range(n):
  a,b=map(int,input().split())
  append((a+b,a,b))
AB.sort(key=lambda x:x[0],reverse=True)
ans=0
sign=0
for i in range(n):
  ab,a,b=AB[i]
  if sign==0:
    ans+=a
    sign=1
  else:
    ans-=b
    sign=0
print(ans)

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

#n,m = ips()
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(m)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#a.pop(0)は10^5あるときTLEしやすい!注意!抜くなら逆ソート!
b = []
for i in range(n):
    b.append(iprow())
a = []
for i in range(n):
    c = [sum(b[i]),b[i][0],b[i][1]]
    a.append(c)
a.sort(reverse=True)
s = 0
ct = 0
for i in range(n):
    a[i][2]*=-1
for i in a:
    s+=i[ct+1]
    ct+=1
    ct%=2
print(s)

Submission status: Accepted

Solution code:
n=int(input())

dish=[]

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

dish.sort()
dish.reverse()

ans=0
takahashi=True

for turn in dish:
    if takahashi:
        takahashi=False
        ans+=turn[1]
    else:
        takahashi=True
        ans-=turn[2]

    #print(ans)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
l=[]
ans=0
for i in range(n):
    x,y=map(int,input().split())
    a+=[x]
    b+=[y]
    l+=[(x,y,i)]
l.sort(key=lambda x:x[0]+x[1])
for x in l[-1::-2]:
    ans+=a[x[2]]+b[x[2]]
print(ans-sum(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
C=[]
ans=0
for i in range(N):
    a,b=map(int,input().split())
    C+=[(a,b,a+b)]
C.sort(key=lambda x:x[2],reverse=True)
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())

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

    AB.append(A+B)
    X += B * -1

AB.sort(reverse=True)


for i in range(0, N, 2):
    X += AB[i]


print(X)

Submission status: Accepted

Solution code:
n = int(input())
ab = [[int(item) for item in input().split()] for _ in range(n)]
ab.sort(key=lambda x: x[0] + x[1], reverse=True)

ans = 0
for i, (a,b) in enumerate(ab):
    if i % 2 == 0:
        ans += a
    else:
        ans -= b
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
dish = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N):
    ans -= dish[i][1]
def f(l):
    a, b = l
    return a+b
dish.sort(key = f)
cnt = 0
while dish:
    a, b = dish.pop()
    if cnt % 2 == 0:
        ans += a+b
        cnt += 1
    else:
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
l = []
for i in range(n):
    x,y = map(int,input().split())
    ans-=y
    l.append(x+y)
l.sort()
for i in l[::-2]:
    ans+=i
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
l = []
for i in range(n):
    x,y = map(int,input().split())
    ans-=y
    l.append(x+y)
l.sort()
l = l[::-1]
for i in l[::2]:
    ans+=i
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
li = [ [int(it) for it in input().split()] for i in range(N)]

po = [0]*N
for i in range(N):
  po[i] = ( sum(li[i]) , i )
po.sort ( key = lambda x: -x[0] )

s = 0
it = 0
for i in range(N):
  s += li[ po[i][1] ][it] * ( 1 - it * 2  )
  it = 1-it           
print (s)

Submission status: Accepted

Solution code:
N = int(input())
ABs = [list(map(int,input().split())) for i in range(N)]
li = []
for A,B in ABs:
  li.append([A+B,A,B])
  
li.sort(reverse=True)
flag = True
ans1 = ans2 = 0
for S,A,B in li:
  if flag:
    ans1 += A
  else:
    ans2 += B
  flag = not flag
  
print(ans1-ans2)

Submission status: Accepted

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x:sum(x), reverse=True)
tk = 0
ak = 0
for i, (a, b) in enumerate(AB):
    if i % 2 == 0:
        tk += a
    else:
        ak += b
print(tk-ak)

Submission status: Accepted

Solution code:
n = int(input())
ab = []
bsum = 0
for _ in range(n):
  a, b = map(int, input().split())
  ab.append(a + b)
  bsum += b

tans = 0
for i, _ab in enumerate(sorted(ab, key=lambda x: -x)):
  if i % 2 == 0:
    tans += _ab

print(tans - bsum)

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)
ans = 0
for i in range(n):
    if i%2:
        ans -= ab[i][1]
    else:
        ans += ab[i][0]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
l=[]
x=0
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b])
    x-=b

print(sum(map(sum,sorted(l,reverse=1,key=lambda x:sum(x))[::2]))+x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    C = [0] * N
    T = 0
    for i in range(N):
        A, B = map(int, input().split())
        C[i] = A + B
        T += A
    C.sort(reverse=True)
    print(T - sum(C[1::2]))


if __name__ == "__main__":
    main()

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    tmp = []
    for i in range(N):
        tmp.append([A[i], B[i], A[i] + B[i]])
    tmp.sort(reverse=True, key=lambda x: x[2])
    ret = 0
    for i in range(N):
        t = tmp[i][0] if i % 2 == 0 else -tmp[i][1]
        ret += t
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]" 
    B = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
N=int(input())
data=[]
for i in range(N):
    a,b=map(int,input().split())
    data.append((a,b,a+b))
A=0
B=0
data.sort(key=lambda x:x[2],reverse=True)
for i in range(N):
    if i%2==0:
        A+=data[i][0]
    else:
        B+=data[i][1]
print(A-B)

Submission status: Accepted

Solution code:
N = int(input())
ab = []
for i in range(N):
    a,b = map(int,input().split())
    c = a+b
    ab.append([c,a,b])
ab.sort(reverse=True)
t = 1
A,B = 0,0
for i in range(N):
    if t == 1:
        A += ab[i][1]
        t *= -1
    else:
        B += ab[i][2]
        t *= -1
print(A-B)

Submission status: Accepted

Solution code:
n = int(input())
listP = []
i=0
while i < n:
    a, b = map(int, input().split())
    listP.append([a+b, a, b])
    i+=1
listP.sort()
listP.reverse()

takaP=0
aokiP=0
index=0
while index < len(listP):
    if index%2==0:
        takaP+=listP[index][1]
    else:
        aokiP+=listP[index][2]
    index+=1
print(takaP-aokiP)

Submission status: Accepted

Solution code:
N=int(input())
Dinner=[[int(i) for i in input().split()] for i in range(N)]
C=[Dinner[i][0]+Dinner[i][1] for i in range(N)]
C.sort(reverse=True)
ans=0
for i in range(N):
  if i%2==0:
    ans+=C[i]
  ans-=Dinner[i][1]
print(ans)

Submission status: Accepted

Solution code:
s=[list(map(int,input().split()))for i in range(int(input()))];print(sum(sorted([x[1]+x[0] for x in s])[::-2])-sum([x[1]for x in s]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C
n = int(input())
if n==1:
    a, b =map(int, input().split())
    print(a)
else:
    def return_a(n):
        return n[0]
    def return_b(n):
        return n[1]
    ab = [[0] * 3 for i in range(n)]
    for i in range(n):
        ab[i][0], ab[i][1] = map(int, input().split())
        ab[i][2] = ab[i][0] + ab[i][1]
    ab = sorted(ab, key=lambda x: x[2], reverse=True)
    print(sum(map(return_a, ab[0::2])) - sum(map(return_b, ab[1::2])))

Submission status: Accepted

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

n = int(input())
ab = []
for i in range(n):
    a = list(map(int, input().split()))
    ab.append([a[0]+a[1], a[0], a[1]])

ab = sorted(ab, reverse=True)

a = sum(list(map(lambda x: x[1], ab[::2])))
b = sum((list(map(lambda x: x[2], ab[1::2]))))

print(a-b)

Submission status: Accepted

Solution code:
from math import ceil
N = int(input())
D = [list(map(int, input().split())) for _ in range(N)]
D.sort(key=lambda x: sum(x), reverse=True)
ans = 0
for i in range(N):
    ans -= D[i][1]
    if i < ceil(N / 2):
        ans += D[i * 2][0] + D[i * 2][1]
print(ans)

Submission status: Accepted

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

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

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

print(T-A)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = -sum([i[1] for i in ab])
sum_l = list(map(sum, ab))
sum_l.sort(reverse=True)

for i in sum_l[::2]:
    ans += i
print(ans)

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])
print(sum([a[0] for a in AB[::2]]) - sum([a[1] for a in AB[1::2]]))

Submission status: Accepted

Solution code:
N = int(input())
X = [list(map(int, input().split())) for i in range(N)]
X.sort(key=lambda x: x[0] + x[1], reverse=True)

ans = 0
for i, (a, b) in enumerate(X):
    if i % 2 == 0:
        ans += a
    else:
        ans -= b

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
score = []
for i in range(n):
    a,b = map(int,input().split())
    score.append([a,b,a+b])
score.sort(key = lambda x: x[2])
score.reverse()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = input()
dishes = []
for i in range(int(n)):
    tmp = [int(each) for each in input().split()]
    dishes.append((abs(tmp[0] + tmp[1]), tmp[0], tmp[1]))

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

point = [0, 0]
player = 0
while len(dishes) > 0:
    dish = dishes.pop(0)
    point[player] += dish[player + 1]
    player = int(not player)
print(point[0] - point[1])

Submission status: Accepted

Solution code:
N = int(input())
L = []
K = []
S = 0
for i in range(N):
  L.append(list(map(int,input().split())))
for i in range(N):
  S += -L[i][1]
for i in range(N):
  K.append(L[i][0]+L[i][1])
K.sort()
K.reverse()
if N%2 == 0:
  for i in range(N//2):
    S += K[i*2]
else:
  for i in range(N//2+1):
    S += K[i*2]
print(S)

Submission status: Accepted

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

Cs = [(A+B, i) for i, (A, B) in enumerate(zip(As, Bs))]
Cs.sort(reverse=True)

hA = sum([As[i] for C, i in Cs[::2]])
hB = sum([Bs[i] for C, i in Cs[1::2]])
print(hA - hB)

Submission status: Accepted

Solution code:
#C問題
N=int(input())
A=[]
B=[]
C=[]
for i in range(0,N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(a+b)
C.sort()
C.reverse()
point=-sum(B)
#print(point)
i=0
while i<N:
    point=point+C[i]
    i=i+2
print(point)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    takahashi, aoki = 0, 0
    for i, (t, a, b) in enumerate(sorted(score, reverse=True)):
        if i % 2 == 0:
            takahashi += a
        else:
            aoki += b
    print(takahashi - aoki)


if __name__ == '__main__':
    main()