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:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N = input()
ls = []
for _ in range(N):
    A, B = map(int, raw_input().split())
    ls.append((A + B, A, B))
ls.sort(reverse = True)

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

print ans

Submission status: Accepted

Solution code:
N=int(input())
B=[0 for i in range(N)]
sum_AB=[0 for i in range(N)]
for i in range(N):
    A,B[i]=map(int,input().split())
    sum_AB[i]=A+B[i]
sum_AB.sort(reverse=True)
sum_B=sum(B)
sum_takahashi=-sum_B
i=0
while i<N:
    if i%2==0:
        sum_takahashi+=sum_AB[i]
    i+=1
print(sum_takahashi)

Submission status: Accepted

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

ab.sort(key=sum, reverse=True)

sol = int(0)

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

print(sol)

Submission status: Accepted

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

ab.sort(key=sum, reverse=True)

sol = 0

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

print(sol)

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,key=lambda x:x[0])
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:
def get_ans(pairs):
    pairs = sorted(pairs, key=lambda x: -sum(x))
    score = 0
    flg = 0
    for a,b in pairs:
        score += a*(1-flg) - b*flg
        flg = 1- flg
    return score

if __name__ == '__main__':
    n = int(input())
    p = []
    for i in range(n):
        p.append(tuple(map(int, input().split())))

    print(get_ans(p))

Submission status: Accepted

Solution code:
N = int(input())
A = [[0 for i in range(2)] for j in range(N)]

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


sum = [[0 for i in range(2)]for j in range(N)]
for i in range(N):
    sum[i][0]=A[i][0]+A[i][1]
    sum[i][1] = i

##最大値を取るiの番号を特定                                                                                                                                                                                 
sum = sorted(sum)
sum.reverse()
max = 0
for i in range(N):
    if(i%2==0):
        max = max + A[sum[i][1]][0]
    else:
        max = max - A[sum[i][1]][1]

print(max)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = []
sumB = 0
for i in range(N):
    tmp = list(map(int, input().split()))
    l.append(tmp[0] + tmp[1])
    sumB += tmp[1]
l.sort(reverse=True)
A = 0
for i in range(N):
    if i % 2 ==0:
        A += l[i]
print(A - sumB)

Submission status: Accepted

Solution code:
n = int(input())
B = []
total = []
count = 0

for i in range(n):
	a,b = map(int, input().split())
	B.append(b)
	total.append(a+b)

total_new = sorted(total, reverse=True)

for i in range(0, n, 2):
	count += total_new[i]

count -= sum(B)

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
c=[]
for i in range(n):
    ai,bi=map(int,input().split())
    a.append(ai)
    b.append(bi)
    c.append(ai+bi)
c=sorted(c,reverse=True)
print(sum(c[::2])-sum(b))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
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()
A, B = [0]*N, [0]*N
sm = [[0]*2 for i in range(N)]
for i in range(N):
    A[i], B[i] = MAP()
    sm[i][0] = A[i] + B[i]
    sm[i][1] = i

sm.sort(key=itemgetter(0), reverse=True)

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    B, AandB = [], []
    for n in range(N):
        a, b = map(int, input().split())
        B.append(b)
        AandB.append(a+b)

    AandB.sort(reverse=True)
    ans = -sum(B)

    for i in range(0, N, 2):
        ans += AandB[i]

    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque


n=int(sys.stdin.readline())
M = deque(maxlen=n)
for i in range(n):
    a,b = map(int,sys.stdin.readline().split())
    M.append([a,b])

M = sorted(M,key=lambda x:sum(x))
cnt = 0
for i in range(n):
    if i%2==0:
        cnt += M.pop()[0]
    else:
        cnt -= M.pop()[1]
print(cnt)

Submission status: Accepted

Solution code:
import sys

n=int(sys.stdin.readline())
M = []
for i in range(n):
    a,b = map(int,sys.stdin.readline().split())
    M.append([a,b])

M = sorted(M,key=lambda x:sum(x), reverse=True)
cnt = 0
for i in range(n):
    if i%2==0:
        cnt += M[i][0]
    else:
        cnt -= M[i][1]
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
total = []
a_list = []
b_list = []
for i in range(0,N):
  a,b = map(int,input().split())
  a_list += [a]
  b_list += [b]
  total += [a+b]

total.sort(reverse = True)

taka = 0

for j in range(0,N):
  if j % 2 == 0:
    taka += total[j]
  
print(-sum(b_list) + taka)

Submission status: Accepted

Solution code:
N=int(input())
LL=[]
for i in range(N):
    a,b=list(map(int,input().strip().split(' ')))
    LL+=[(a,b)]
LL=sorted(LL,key=lambda x:sum(x),reverse=True)
ans=0
for i in range(len(LL)):
    if i%2==0:
        ans+=LL[i][0]
    else:
        ans-=LL[i][1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

D = sorted(C, reverse=True)

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = []#料理Nに対する高橋くんと青木さんの幸福度の和？
bs = 0#全ての料理を青木さんが食べたと仮定した時の青木さんの幸福度
for i in range(n):
    a,b = map(int,input().split())
    bs += b
    c.append(a+b)

c.sort()#Cを昇順ソート
c.reverse()#降順に直す

ans = -bs
for i in range(n):
    if i%2==0: ans += c[i]#高橋くんが食べる時、引いた青木さんの幸福度(食べてないのでいらない)と高橋くんの幸福度を足す
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
items = [list(map(int,input().split())) for i in range(N)]
items.sort(key=lambda x: x[0]+x[1], reverse=True)
taka = sum([items[i][0] for i in range(0,N,2)])
aoki = sum([items[i][1] for i in range(1,N,2)])
print(taka - aoki)

Submission status: Accepted

Solution code:
n = int(input())
a, b = zip(*sorted((tuple(map(int, input().split())) for _ in range(n)), key=sum))
print(sum(a[-1::-2]) - sum(b[-2::-2]))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N, = [int(x) for x in input().split()]
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
  A[i], B[i] = [int(x) for x in input().split()]
  C[i] = A[i]+B[i]
jti = list(range(N))
jti.sort(key=lambda i: C[i])
print(sum([A[i] for i in jti[-1::-2]])-sum([B[i] for i in jti[-2::-2]]))

Submission status: Accepted

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

c.sort()

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

Submission status: Accepted

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

c.sort()

ans = -bs
for i in reversed(range(n)):
  cnt = n-1-i
  if cnt%2==0: ans += c[i]
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  a1,a2 = map(int, input().split())
  a.append([a1+a2,a1,a2])
a.sort()
a.reverse()
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:
import numpy as np
import sys
input = sys.stdin.readline

N = int(input())

arr = np.empty((N, 3), dtype='int64')
for i in range(N):
    arr[i, 0], arr[i, 1] = input().split()
    arr[i, 2] = arr[i, 0] + arr[i, 1]

arr = arr[np.argsort(arr[:, 2])][::-1]

print(np.sum(arr[::2, 0]) - np.sum(arr[1::2, 1]))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

arr = np.empty((N, 3), dtype='int64')
for i in range(N):
    arr[i, 0], arr[i, 1] = input().split()
    arr[i, 2] = arr[i, 0] + arr[i, 1]

arr = arr[np.argsort(arr[:, 2])][::-1]

print(np.sum(arr[::2, 0]) - np.sum(arr[1::2, 1]))

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

N = int(input())

arr = np.array([input().split() + [0] for _ in range(N)], dtype='int64')
for i in range(N):
    arr[i, 2] = arr[i, 0] + arr[i, 1]

arr = arr[np.argsort(arr[:, 2])][::-1]

print(np.sum(arr[::2, 0]) - np.sum(arr[1::2, 1]))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

arr = np.array([input().split() + [0] for _ in range(N)], dtype='int64')
for i in range(N):
    arr[i, 2] = arr[i, 0] + arr[i, 1]

arr = arr[np.argsort(arr[:, 2])][::-1]

print(np.sum(arr[::2, 0]) - np.sum(arr[1::2, 1]))

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
B = [0] * N
Sum = [[0] * 2 for i in range(N)]

for i in range(N):
    A[i], B[i] = map(int, input().split())
    Sum[i][0] = i
    Sum[i][1] = A[i] + B[i]
    
a = 0
b = 0
turn = 0

Sum.sort(key=lambda x:x[1], reverse=True)
    
for i in range(N):
    if turn == 0:
        a += A[Sum[i][0]]
        turn = 1
    else:
        b += B[Sum[i][0]]
        turn = 0

print(a - b)

Submission status: Accepted

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

N = int(input())
A = [0] * N
B = [0] * N
Sum = [[0] * 2 for i in range(N)]

for i in range(N):
    A[i], B[i] = map(int, input().split())
    Sum[i][0] = i
    Sum[i][1] = A[i] + B[i]
    
a = 0
b = 0
turn = 0

Sum.sort(key=lambda x:x[1], reverse=True)
    
for i in range(N):
    if turn == 0:
        a += A[Sum[i][0]]
        turn = 1
    else:
        b += B[Sum[i][0]]
        turn = 0

print(a - b)

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

count_a=0
count_b=0

C.sort(reverse=True)

for j in range(n):
    if(j%2==0):
        count_a+=C[j][1]
    else:
        count_b+=C[j][2]

print(count_a-count_b)

Submission status: Accepted

Solution code:
N = int(input())

X = []
for i in range(N):
    x, y = map(int, input().split())
    
    X.append([x+y,x,y])   


X.sort(reverse=True)

ansA = 0
ansB = 0

for index, i in enumerate(X):
    if index%2 == 0:
        ansA += i[1]
    else:
        ansB += i[2]

print(ansA - ansB)

Submission status: Accepted

Solution code:
N = int(input())

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

C = []
for index, i in enumerate(range(N)):
    C.append([index, A[i] + B[i]])

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

ansA = 0
ansB = 0

for index, i in enumerate(C):
    if index%2 == 0:
        ansA += A[i[0]]
    else:
        ansB += B[i[0]]

print(ansA - ansB)

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python

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

A = [ x[0] for x in inp_list]
B = [ x[1] for x in inp_list]

sum_list = [ A[i] + B[i] for i in range(N)] 

w_sum = sorted([ (sum_list[i], A[i], B[i]) for i in range(N)], reverse=True)

takahashi_sum = sum( [x[1] for x in w_sum[::2]] , 0)
aoki_sum =      sum( [x[2] for x in w_sum[1::2]], 0)

print(takahashi_sum - aoki_sum)

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python

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


ans = 0
for e,t in enumerate(sorted(inp_list, key=lambda x : x[0] + x[1], reverse = True)):
    if e % 2 == 0:
        ans += t[0]
    else:
        ans -= t[1]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    dishes = list()
    ans = 0

    # See:
    # https://poporix.hatenablog.com/entry/2019/01/28/222905
    # https://misteer.hatenablog.com/entry/NIKKEI2019qual?_ga=2.121425408.962332021.1548821392-1201012407.1527836447
    for i in range(n):
        ai, bi = map(int, input().split())
        dishes.append((ai, bi))

    for index, dish in enumerate(sorted(dishes, key=lambda x: x[0] + x[1], reverse=True)):
        if index % 2 == 0:
            ans += dish[0]
        else:
            ans -= dish[1]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
ls=[list(map(int,input().split())) for i in range(n)]
ls=sorted(ls,key=lambda x:sum(x),reverse=True)
taka=sum([ls[i][0] for i in range(0,len(ls),2)])
aoki=sum([ls[i][1] for i in range(1,len(ls),2)])

print(taka-aoki)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    dishes = list()
    ans = 0

    # See:
    # https://poporix.hatenablog.com/entry/2019/01/28/222905
    # https://misteer.hatenablog.com/entry/NIKKEI2019qual?_ga=2.121425408.962332021.1548821392-1201012407.1527836447
    for i in range(n):
        ai, bi = map(int, input().split())
        dishes.append(tuple([ai + bi, ai, bi]))

    for index, dish in enumerate(sorted(dishes, reverse=True)):
        if index % 2 == 0:
            ans += dish[1]
        else:
            ans -= dish[2]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a_=[]
b_=[]

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

sa_ = sorted(
  a_,
  key = lambda x: (x[0], x[1]),
)
sb_ = sorted(
  b_,
  key = lambda x: (x[0], x[1]),
)

i_={}
res=0

for i in range(n):
    if i % 2 :
        item = sb_.pop()
        while ( item[2] in i_ ) :
            item = sb_.pop()
        res -= item[1]
        i_[item[2]]=1
    else :
        item = sa_.pop()
        while ( item[2] in i_ ) :
            item = sa_.pop()
        res += item[1]
        i_[item[2]]=1

print(res)

Submission status: Accepted

Solution code:
from operator import itemgetter
N = int(input())
A = []
for i in range(N):
  a,b = map(int, input().split())
  A.append([a+b,a,b])
  
A.sort(key=itemgetter(0),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 = int(input())
ab = []
ans = 0
for i in range(n):
    a,b = map(int,input().split())
    ab.append([a+b,b])
    ans -= b
ab.sort(reverse=True)
for i in range(n):
    if i%2 == 0:
        ans += ab[i][0]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

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

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:
import sys
from operator import itemgetter
input = sys.stdin.readline

N = int(input())
IN = []
for _ in range(N):
    s = input().split()
    A = int(s[0])
    B = int(s[1])
    R = A + B
    IN.append((A,B,R))

sortedIN = sorted(IN, key=itemgetter(2), reverse=True)
sortedIN.append((0,0,0))

sum_A = 0
sum_B = 0

for i in range(0, N, 2):
    sum_A += sortedIN[i][0]
    sum_B += sortedIN[i+1][1]

print(sum_A - sum_B)

Submission status: Accepted

Solution code:
N = int(input())
A = [[0, 0, 0]for i in range(N)]
for i in range(N):
  a, b = list(map(int, input().split()))
  A[i] = a+b, a, b
A.sort(reverse=True)
x = 0
for i in range(N):
    x += (-1)**i * A[i][1+i%2]
print(x)

Submission status: Accepted

Solution code:
import sys

N = int(input())
aAB = [[int(_) for _ in sLine.split()] for sLine in sys.stdin.readlines()]
aAB = sorted([a+b,a,b] for a,b in aAB)[::-1]
iA = 0
iB = 0
for i in range(N):
    if i % 2:
        iB+=aAB[i][2]
    else:
        iA+=aAB[i][1]
print(iA-iB)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = []
    B = []
    C = {}
    for i in range(n):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)
        C[str(i)] = a + b
    ans = 0
    isA = True
    for k, v in sorted(C.items(), key=lambda x: -x[1]):
        if isA:
            ans += A[int(k)]
        else:
            ans -= B[int(k)]
        isA = not isA
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

dicts = {}
for i in range(N):
    dicts[i] = sum(inputs[i])

ans = 0
count = 0
for key, value in sorted(dicts.items(), key=lambda x: -x[1]):
    if count % 2 == 0:
        ans += inputs[key][0]
    else:
        ans -= inputs[key][1]
    count += 1

print(ans)

Submission status: Accepted

Solution code:
N =int(input())
l=[[int(i)for i in input().split()]for j in range(N)]
l =sorted(l,key=lambda x:sum(x),reverse=True)
ans =[0,0]
nu = 0
for i in l:
    ans[nu] += i[nu]
    if nu == 0:
        nu = 1
    else:
        nu = 0
print(ans[0]-ans[1])

Submission status: Accepted

Solution code:
n = int(input())

dish_list = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    dish_list.append([ai, bi, ai+1+bi])


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


a, b =0, 0
turn = 1
for i in range(n):
    if turn ==1:
        a+=dish_list[i][0]
    else:
        b+=dish_list[i][1]
    turn*=-1


print(a-b)

Submission status: Accepted

Solution code:
n = int(input())

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

gou.sort()
gou.reverse()

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n =int(input())
takahashi = 0
aoki = 0
ab = []
a = []
for i in range(n):
 tmp1,tmp2 = (int(i) for i in input().split())
 a.append([tmp1,tmp2])
 ab.append(tmp1+tmp2)
 aoki += tmp2
#a2 = sorted(a, key=lambda x: x[0])
#a3 = sorted(a, key=lambda x: x[1])
ab = sorted(ab,reverse = True)
start = 0
for i in range(n):
  if i%2 == 0:
    takahashi += ab[i]
  else:
    continue


print(takahashi-aoki)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
T = [line.split() for line in sys.stdin.readlines()]
res = 0
for i, t in enumerate(sorted([(-int(a) - int(b), int(b)) for a, b in T])):
	if i % 2 == 0:
		res -= t[0]
	res -= t[1]

print(res)

Submission status: Accepted

Solution code:
N=int(input())
sub=[]
AB=[]
for _ in range(N):
    a,b=map(int,input().split())
    AB.append((a,b,a+b))
    sub.append(a+b)
ta=0
ao=0
AB.sort(key=lambda x:-x[2])
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:
import math
import sys
from operator import itemgetter

# N, K = map(int, input().split())
# h = list(map(int, input().split()))
# S = [input() for _ in range(H)]

N = int(input())
r = [[0 for i in range(3)] for j in range(N)]
rsorted = [[0 for i in range(3)] for j in range(N)]

for i in range(N):
	r[i][0], r[i][1] = map(int, input().split())
	r[i][2] = r[i][0] + r[i][1]

rsorted = sorted(r, key = itemgetter(2), reverse=True)

a = 0
b = 0
turn = True

for i in range(N):
	if turn:
		a += rsorted[i][0]
	else:
		b += rsorted[i][1]
	turn = not(turn)
print(a-b)

Submission status: Accepted

Solution code:
n = int(input())
a_b = []
b_sum = 0
for i in range(n):
    ai, bi = map(int, input().split())
    a_b.append(ai+bi)
    b_sum += bi
a_b.sort(reverse=True)
ans = 0
for i in range(len(a_b)):
    if i%2==0:
        ans += a_b[i]
print(ans-b_sum)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    vals = []
    scores = []

    for i in range(n):
        a, b = [int(strn) for strn in input().split()]
        vals.append([a, b, True])
        scores.append([a + b, i])

    scores.sort(key=lambda x: x[0])

    total = 0
    count = 0
    while True:
        # Turn A
        candidate = scores[-1][1]
        total += int(vals[candidate][count % 2] * (count % 2 - 0.5) * -2)
        count += 1
        vals[candidate][2] = False
        del scores[-1]
        if count == len(vals):
            break

    print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    vals = []
    scores_a = []
    scores_b = []

    for i in range(n):
        a, b = [int(strn) for strn in input().split()]
        vals.append([a, b, True])
        scores_a.append((a + b, i))
        scores_b.append((a + b, i))

    scores_a.sort(key=lambda x: x[0])
    scores_b.sort(key=lambda x: x[0])

    # print(scores_a)
    # print(scores_b)

    total = 0
    count = 0
    while True:
        # Turn A
        candidate = scores_a[-1][1]
        candidate_live = vals[candidate][2]
        # print(candidate, candidate_live)
        while not candidate_live:
            # print("####1{}".format(scores_a))
            del scores_a[-1]
            # print("####2{}".format(scores_a))
            candidate = scores_a[-1][1]
            candidate_live = vals[candidate][2]

        total += vals[candidate][0]
        count += 1
        vals[candidate][2] = False
        del scores_a[-1]

        if count == len(vals):
            break

        # Turn B
        candidate = scores_b[-1][1]
        candidate_live = vals[candidate][2]
        while not candidate_live:
            # print("####3{}".format(scores_b))
            del scores_b[-1]
            # print("####4{}".format(scores_b))
            candidate = scores_b[-1][1]
            candidate_live = vals[candidate][2]

        total -= vals[candidate][1]
        count += 1
        vals[candidate][2] = False

        if count == len(vals):
            break

    print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ab = sorted([x[0]+x[1] for x in AB], reverse=True)
ans = 0
for i in range(N):
    ans -= AB[i][1]

if N % 2 == 0:
    K = N // 2
else:
    K = N // 2 + 1
for i in range(K):
    ans += ab[2*i]


print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n = int(input())
a = [0 for i in range(n)]
b = [0 for i in range(n)]
s = []

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

s.sort(key=lambda e: e[1], reverse=True)

turn = True
taka, aoki = 0, 0
for subs in s:
    if turn:
        taka += a[subs[0]]
    else:
        aoki += b[subs[0]]
    turn = not turn

print(taka - aoki)

Submission status: Accepted

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

X = []
for (a,b) in AB:
    X.append((a+b,b))

X.sort(reverse=True)
pa,pb = 0,0
for i in range(N):
    ab,b = X[i]
    pb += b
    if i%2==0:
        pa += ab

print(pa-pb)