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())
prefs = []
for i in range(N):
    a, b = map(int, input().split())
    prefs.append((a + b, a, b))
prefs.sort(reverse=True)
ans = 0
for i, (p, a, b) in enumerate(prefs):
    if i % 2:
        ans -= b
    else:
        ans += a
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

D = []

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

D.sort(reverse=True)

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

print(ans)

Submission status: Accepted

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

C.sort(reverse=True)
ans=0
for i in range(N):
    if i%2==0:
        ans+=C[i]

bs=sum(B)
ans-=bs
print(ans)

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
ab = []
for i in range(n):
    a,b = inm()
    ab.append((a+b,a,b))
ab.sort(reverse=True)
sm = 0
for i in range(n):
    if i%2==0:
        sm += ab[i][1]
    else:
        sm -= ab[i][2]
print(sm)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
ab = [list(map(int, readline().split())) for i in range(n)]
memo = [0] * n
ans = 0
for i, ab_ in enumerate(ab):
    memo[i] = ab_[0] + ab_[1]
    ans -= ab_[1]
memo.sort(reverse=True)
for i in range(n):
    if i % 2 == 0:
        ans += memo[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a, b, c = [], [], []
for i in range(n):
    av, bv = map(int, input().split())
    a.append(av)
    b.append(bv)
    c.append((i, av + bv))

c = sorted(c, key=lambda x: x[1], reverse=True)
takahaship, aokip = 0, 0
i = 0
for _ in c:
    if i % 2 == 0:
        takahaship += a[c[i][0]]
    else:
        aokip += b[c[i][0]]
    i += 1
print(takahaship - aokip)

Submission status: Accepted

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

print(sum(sorted([d[0] + d[1] for d in D], reverse=True)[::2])
      - sum([d[1] for d in D]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
b=sorted([v[0]+v[1] for v in a],reverse=1)
ans=0
for i in a:
    ans-=i[1]
for i in range(n):
    if i%2==0:
        ans+=b[i]
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
n = int(readline())
A = []
B = []
AB = []
for i in range(n):
    a, b = map(int, readline().split())
    A.append(a)
    B.append(b)
    AB.append(a+b)
AB.sort()
ans = -sum(B) + sum(AB[::-2])
print(ans)

Submission status: Accepted

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

AB = sorted(AB, key = lambda el:sum(el), reverse = True)
#print(AB)

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

Submission status: Accepted

Solution code:
n = int(input())
A, B, C = [], [], []
for i in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append([a + b, i])
C = sorted(C, reverse=True)

suma, sumb = 0, 0
for i in range(n):
    if i % 2:
        sumb += B[C[i][1]]
    else:
        suma += A[C[i][1]]
print(suma - sumb)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from bisect import bisect_left,bisect_right
from collections import deque

def main():
    n = int(input())
    food = []
    ans = 0
    for _ in range(n):
        a,b = map(int,input().split())
        ans -= b
        food.append(a + b)
    
    food.sort(reverse = True)
    for i in range(n):
        if i%2 == 0:
            ans += food[i]
    print(ans)
if __name__ =='__main__':
    main()

Submission status: Accepted

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

a_b=[]
x=0
for a,b in ab:
    a_b.append(a+b)
    x-=b
a_b.sort(reverse=True)
ans=0
for i in range(0,n,2):
    ans+=a_b[i]

print(ans+x)

Submission status: Accepted

Solution code:
def main():
    # 解説AC
    N = int(input())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    AB.sort(reverse=True, key=lambda p: p[0]+p[1])
    X = sum(-b for a, b in AB)
    for i in range(0, N, 2):
        X += sum(AB[i])
    print(X)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

AB = [[0, 0, 0] for i in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    AB[i][0] = a+b
    AB[i][1] = a
    AB[i][2] = b

AB.sort()
ans = 0
for i in range(n):
    _, a, b = AB.pop()
    if i%2 == 0:
        ans += a
    else:
        ans -= b
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
res = []

ans = 0
for i in range(n):
    a,b = map(int,input().split())
    res.append(a+b)
    ans-=b

res.sort(reverse = True)
for i in range(0,n,2):
    ans+=res[i]
print(ans)

Submission status: Accepted

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

l = sorted([[a + b, a, b] for a, b in ab])[::-1]
res = 0
for idx, i in enumerate(l):
    if idx % 2 == 0:
        res += i[1]
    else:
        res -= i[2]
print(res)

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    T, A = [None] * N, [None] * N
    used = [False] * N
    for i in range(N):
        a, b = map(int, input().split())
        T[i] = (a + b, a, i)
        A[i] = (a + b, b, i)
    T.sort(reverse = True)
    A.sort(reverse = True)
    hapiness = 0
    ti, ai = 0, 0
    for i in range(N):
        if i % 2 == 0:
            while used[T[ti][2]]: ti += 1
            hapiness += T[ti][1]
            used[T[ti][2]] = True
            ti += 1           
        else:
            while used[A[ai][2]]: ai += 1
            hapiness -= A[ai][1]
            used[A[ai][2]] = True
            ai += 1
    print(hapiness)
   
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    answer = 0
    dish = []
    for _ in range(n):
        a, b = map(int, input().split())
        dish.append(a + b)
        answer -= b
    dish.sort(reverse=True)
    for i in range(0, n, 2):
        answer += dish[i]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
n = int(input())
dish = []
for i in range(n):
    a,b = map(int,input().split())
    dish.append((a,b, a+b))
dish.sort(key=lambda x:x[2])
dish.reverse()
tk = dish[0:n:2]
ao = dish[1:n:2]
tkp = 0
aop = 0
for i in range(len(tk)):
    tkp += tk[i][0]
for i in range(len(ao)):
    aop += ao[i][1]
print(tkp-aop)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    N=int(input())
    AB=[tuple(map(int, input().split())) for _ in range(N)]
    scoresA = [(a+b,a,i) for i,(a,b) in enumerate(AB)]
    scoresB = [(b+a,b,i) for i,(a,b) in enumerate(AB)]
    scoresA.sort(reverse=True)
    scoresB.sort(reverse=True)
    ans=0
    used=[False]*N
    ai=bi=0
    for i in range(N):
        if i&1:
            while used[scoresB[bi][2]]: bi+=1
            ans-=scoresB[bi][1]
            used[scoresB[bi][2]]=True
        else:
            while used[scoresA[ai][2]]: ai+=1
            ans+=scoresA[ai][1]
            used[scoresA[ai][2]]=True
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# どうせ何を喰っても自分のポイントは入るので、
# 高橋くんは青木くんの得られるポイントの最大のやつを,青木くんは高橋くんのvice versa
# 同じポイントならその中でも自分が得られるものが多いほうがいい
# ↑ ぜーーーーーーーーーーーーーーーーーんぜんだめでした

# 解説みた ぜんぜんわからん 無理 

n = int(input())
arr = []
used = [False for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    arr.append([i, a, b])

lis = sorted(arr, key=lambda x: x[1] + x[2], reverse=True)
takp, aokp = 0, 0

c = 0

while c < n:
    if c % 2 == 0:
        # 高橋くんのターン
        takp += lis[c][1]
    else:
        # 青木くんのターン
        aokp += lis[c][2]
    c += 1
print(takp - aokp)

Submission status: Accepted

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

Submission status: Accepted

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

net.sort(reverse=True)

t=sum(net[i] for i in range(0,N) if i%2==0)
print(t-y)

Submission status: Accepted

Solution code:
N = int(input())
ABs = []
sum_B = 0
for i in range(N):
    a,b = map(int, input().split())
    ABs.append(a+b)
    sum_B += b 
ABs.sort(reverse=True)
ans = sum(ABs[::2])-sum_B
print(ans)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


n = onem()

po = []
su = 0
for i in range(n):
    a,b = m()
    po.append([a+b,a])
po.sort(reverse = True)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import defaultdict
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))


def main():
    N = INT()
    AB = [LI() for _ in range(N)]
    AB.sort(key=lambda x: -(x[0]+x[1]))

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

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
x=[]
ans=0
for _ in range(n):
    a,b=map(int,input().split())
    x.append(a+b)
    ans-=b
x.sort(reverse=True)
ans+=sum(x[i] for i in range(n) if i%2==0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
abc =[]
for i in range(n):
    a,b = [int(x) for x in input().split()]
    abc.append([a,b,a+b])
abc = sorted(abc,key=lambda x: x[2],reverse=True)
t=0
a=0
for i in range(n):
    if i%2==0:
        t+=abc[i][0]
    else:
        a+=abc[i][1]
print(t-a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())

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

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

t, a = 0, 0
for i in range(0, N, 2):
    t += a_b_ab_list[i][0]

for i in range(1, N, 2):
    a += a_b_ab_list[i][1]

print(t - a)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  sb  = 0
  lab = [0] * n
  for i in range(n):
    a, b = map(int, input().split())
    sb += b
    lab[i] = a+b
  lab.sort(reverse=True)
  print(sum(lab[::2]) - sb)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N = II()
    S = []
    for i in range(N):
        a,b = LMIIS()
        S.append((a+b,a,b))
    S.sort(reverse=True)
    # print(S)
    ans = 0
    for i, item in enumerate(S):
        sumab,a,b = item
        if i % 2 == 0:
            ans += a
        else:
            ans -= b
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = []
for i in range(N):
    A,B = [int(x) for x in stdin.readline().rstrip().split()]
    data.append((A+B,A,B))
data.sort()
a = 0
b = 0
for ind,(i,j,k) in enumerate(data[::-1]):
    if ind % 2 == 0:
        a += j
    else:
        b += k
        
print(a-b)

Submission status: Accepted

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

AB = sorted(AB, key=lambda x:x[2], reverse=True)

a_eat = 0
b_eat = 0

for i in range(n):
    if i%2 == 0:
        a_eat += AB[i][0]
    else:
        b_eat += AB[i][1]

ans = a_eat - b_eat
print(ans)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
d = [tuple(MI()) for _ in range(N)]
d.sort(reverse=True, key=lambda x: x[0] + x[1])

T = 0
A = 0
for i, (a, b) in enumerate(d):
    if i % 2 == 0:
        T += a
    else:
        A += b
print(T - A)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
AB = [tuple(map(int,input().split())) for i in range(n)]
AB.sort(key = lambda tup:tup[0]+tup[1],reverse  = True)
for i in range(n):
  if i % 2 == 0:
    ans += AB[i][0]
  else:
    ans -= AB[i][1]
print(ans)
# print(AB)

Submission status: Accepted

Solution code:
def main():
    dishes = int(input())
    aoki_happiness = []
    difference = []
    for i in range(dishes):
        t, a = map(int, input().split())
        aoki_happiness.append(a)
        difference.append(t + a)
    difference.sort(reverse=True)
    answer = -sum(aoki_happiness)
    for i in range(dishes):
        if i % 2 == 0:
            answer += difference[i]
    print(answer)


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 = sorted(AB, key=lambda x:x[2]*(-1))

A = 0
B = 0
for i in range(N):
    if i%2==0:
        A += AB[i][2]
    B += AB[i][1]
        
print(A-B)

Submission status: Accepted

Solution code:
# coding: utf-8
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)
Ta=0
Ao=0

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

print(Ta-Ao)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [None]*n
    for i in range(n):
        x, y = map(int, input().split())
        a[i] = [x, y]
    a.sort(key=lambda x: (x[0]+x[1]), reverse=True)
    score = 0
    for i in range(n):
        if i % 2 == 0:
            score += a[i][0]
        else:
            score -= a[i][1]
    print(score)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
AB=[]
B=[]
for _ in range(n):
    a,b=map(int,input().split())
    AB.append(a+b)
    B.append(b)
AB=sorted(AB,reverse=True)
#print(AB)
X=-sum(B)
for i in range(0,n,2):
    X+=AB[i]
print(X)

Submission status: Accepted

Solution code:
N=int(input())
A=[list(map(int,input().split())) for i in range(N)]
B=[]
c=0
for i in range(N):
  a=A[i][0]+A[i][1]
  c-=A[i][1]
  B.append(a)
B=sorted(B)[::-1]
d=(N+1)//2
for i in range(d):
  c+=B[2*i]
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce

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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
AB = [LIST() for _ in range(N)]

X = -sum([x[1] for x in AB])

for i, (A, B) in enumerate(sorted(AB, key=lambda x:x[0]+x[1], reverse=True)):
	if i%2 == 0:
		X += A+B
print(X)

Submission status: Accepted

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

def main():
    N = int(input())
    food = []
    sc = 0
    for _ in range(N):
        a,b = map(int,input().split())
        sc -= b
        food.append(a+b)
    
    food.sort(reverse= True)
    tf = sum(food[::2])
    print(tf+sc)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
AB=[tuple(map(int, input().split()))for _ in range(N)]
AB.sort(key=lambda ab: ab[0]+ab[1], reverse=True)
print(sum(a for a,b in AB[0::2])-sum(b for a,b in AB[1::2]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n = int(input())
l = []
for _ in range(n):
    tmp = list(map(int,input().split()))
    sm = tmp[0] + tmp[1]
    l.append([tmp[0],tmp[1],sm])

l.sort(key=lambda x:x[2])
l.reverse()

t_sm=0
a_sm=0
cnt=0
for dish in l:
    if cnt%2==0:
        t_sm+=dish[0]
    else:
        a_sm+=dish[1]
    cnt+=1
print(t_sm-a_sm)

Submission status: Accepted

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

for i in range(N):
    a,b = nums[i]
    nums[i].append(a + b)
nums =sorted(nums,key = lambda x:-x[2])

takahasi = [nums[i][0] for i  in  range(N) if i % 2 == 0]
aokis= [nums[i][1] for i  in  range(N) if i % 2 != 0]
print(sum(takahasi) - sum(aokis))

Submission status: Accepted

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

A,B=[],[]
for i,j in AB:
    A.append(i+j)
    B.append(-j)

ans=sum(B)
A=sorted(A)[::-1]
for i in range(0,n,2):
    ans +=A[i]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[0]*n
t=0
for i in range(n):
  a,b=map(int,input().split())
  s[i]=a+b
  t+=b
s.sort()
s=s[::-1]
print(sum(s[::2])-t)

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int,input().split())) for i in range(n)]
x = []
for i in range(n):
  x.append((a[i][0]+a[i][1],i))
x.sort(reverse=True)
tk,ao = 0,0
for i in range(n):
  if i%2 == 0:
    tk += a[x[i][1]][0]
  else:
    ao += a[x[i][1]][1]
print(tk-ao)

Submission status: Accepted

Solution code:
import bisect
import math
import sys
from collections import Counter, defaultdict, deque
from copy import copy, deepcopy
from heapq import heapify, heappop, heappush
from itertools import combinations, permutations
from queue import Queue

read = sys.stdin.read
readline = sys.stdin.readline 
readlines = sys.stdin.readlines 

def SI():
    return int(readline())
def MI():
    return map(int, readline().split())
def MLI():
    return map(int, open(0).read().split())

inf = float("inf")


def main():
    N, *AB = MLI()
    
    happiness = defaultdict(list) 
    for A, B in zip(*[iter(AB)]*2):
        happiness[A+B].append((A, B))
        
    turn = -1
    score_A = 0
    score_B = 0
    for key in sorted(happiness.keys())[::-1]:
        values = deque(sorted(happiness[key]))
        while len(values) > 0:
            if turn == -1:
                A, B = values.pop()
                score_A += A
            else:
                A, B = values.popleft()
                score_B += B
            turn *= -1
            
    print(score_A - score_B)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
AB = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]
kouhuku = [i[0]+i[1] for i in AB]
kouhuku.sort(reverse=True)
aoki = sum(i[1] for i in AB)
takahashi = sum(kouhuku[::2])
print(takahashi-aoki)

Submission status: Accepted

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

Submission status: Accepted

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

li = []
sumb = 0
for a,b in ab:
    li.append(a+b)
    sumb += b

li.sort(reverse = True)


print(sum(li[::2]) - sumb)

Submission status: Accepted

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

print(t-a)

Submission status: Accepted

Solution code:
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n)]
b=[y for x,y in ab]
c=[x+y for x,y in ab]
c.sort(reverse=True)
print(sum(c[::2])-sum(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
AB = sorted([list(map(int,input().split())) for i in range(N)],key = lambda x:-x[0]-x[1])
Tsum = sum(AB[i][0] for i in range(N) if i%2 == 0)
Asum = sum(AB[i][1] for i in range(N) if i%2 == 1)
print(Tsum-Asum)

Submission status: Accepted

Solution code:
n = int(input())
AB = [list(map(int, input().split())) for _ in range(n)]
C = sorted([(i+j, i, j) for i,j in AB], reverse=True)
ans = 0
for i in range(n):
  s, t, a = C[i]
  if i%2 == 0:
    ans += t
  else:
    ans -= a
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
base = 0
L = []
for _ in range(N):
	ta,tb = map(int,input().split())
	base -= tb
	L.append(ta+tb)

L.sort(reverse = True)

takahashi = 0

for i in range(0,N,2):
	takahashi += L[i]

print(takahashi + base)

Submission status: Accepted

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

A = [a for a, b in AB]
C = [a + b for a, b in AB]
C.sort()
C = C[::-1]
print(sum(A) - sum(C[1::2]))

Submission status: Accepted

Solution code:

n = int(input())
aoki, takahashi = [], []
for i in range(n):
    t, a = [ int(v) for v in input().split() ]
    aoki.append((-t-a, -a, i))
    takahashi.append((-a-t, -t, i))
dish_list = [1] * n
aoki.sort(), takahashi.sort()

a_score, t_score = 0, 0
a_counter, t_counter = 0, 0
for i in range(n):
    stop = 0
    if i % 2 == 0:
        while not stop:
            if dish_list[takahashi[t_counter][2]] == 1:
                t_score -= takahashi[t_counter][1]
                dish_list[takahashi[t_counter][2]] = 0
                stop = 1
            t_counter += 1
    else:
        while not stop:
            if dish_list[aoki[a_counter][2]] == 1:
                a_score -= aoki[a_counter][1]
                dish_list[aoki[a_counter][2]] = 0
                stop = 1
            a_counter += 1
print(t_score - a_score)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    ls = [[int(x) for x in input().split()] for _ in range(N)]
    ans = 0
    for i in range(N):
        ls[i].append(ls[i][0]+ls[i][1])
    ls = sorted(ls,key=lambda x:-x[2])
    if N==1:
        print(ls[0][1])
        exit()
    for i in range(N):
        ans -= ls[i][1]

    for i in range(N//2):
        ans += ls[2*i][2]

    if N%2==1:
        ans += ls[2*i+2][2]
    
    print(ans)
if __name__ == '__main__':
    main()

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 = sorted(A,key=lambda x:x[2],reverse=True)
a = 0
for i in range(N):
    if i%2==0:
        a += A[i][0]
    else:
        a -= A[i][1]
print(a)

Submission status: Accepted

Solution code:
N = int(input())
foods = []
for i in range(N):
    food = list(map(int,input().split()))
    foods.append(food)
foods.sort(key=lambda x: -(x[0]+x[1]))
#print(foods)
ans = 0
for i in range(N):
    if i%2 == 0:
        ans += foods[i][i%2]
    else:
        ans -= foods[i][i%2]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
foods = []
for i in range(N):
    food = list(map(int,input().split()))
    foods.append(food)
foods.sort(key=lambda x: -(x[0]+x[1]))
#print(foods)
ans = 0
for i in range(N):
    if i%2 == 0:
        ans += foods[i][0]
    else:
        ans -= foods[i][1]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  l[i]=[a,b]
l.sort(key=lambda x:x[0]+x[1], reverse=True)
#print(l)
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())
AB=[list(map(int,input().split()))+[i] for i in range(N)]
A=sorted(AB,key=lambda x:x[1]+x[0],reverse=True)
value=0
for i in range(N):
    if i%2==0:
        value+=A[i][0]
    else:
        value-=A[i][1]
print(value)