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:
import sys
fin = sys.stdin.readline
# from functools import lru_cache

N = int(fin())
happinesses = [tuple(int(elem) for elem in fin().split()) for _ in range(N)]

happinesses.sort(key=lambda x: sum(x), reverse=True)
score = sum(A for A, _ in happinesses[::2]) - sum(B for _, B in happinesses[1::2])
print(score)

Submission status: Accepted

Solution code:
N = int(input())

dishes = []
for _ in range(N):
  a, b = map(int, input().split())
  dishes.append([a,b,a+b])
  
start = 0
end = N // 2
  
dishesA = sorted(dishes, key = lambda x:x[2])[::-1]

pointA = 0
pointB = 0

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

Submission status: Accepted

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

A.sort(reverse=1)

B=[]
for i in range(n):
    B.append([i,A[i][1]+A[i][0]])
    
B.sort(reverse=1,key=lambda x:x[1])




#print(B)
#print(C)


S=[0,0]

for i in range(n):
    S[i%2]+=A[B[i][0]][i%2]


print(S[0]-S[1])

Submission status: Accepted

Solution code:
a = []
b = []
sa = []
wa = []
n = int(input())
for i in range(n):
    p, y = map(int, input().split())
    a.append((p, y))

sa = sorted(a, key=lambda x: x[0] - x[1])
wa = sorted(sa, key=lambda x: x[0]+x[1], reverse=True)
print(sum(x[0] for x in wa[::2])-sum(x[1] for x in wa[1::2]))

Submission status: Accepted

Solution code:
n = int(input())
A = [[int(i) for i in input().split()] for j in range(n)]

C = []
D = []
i = 0

for a, b in A:
    C.append((a + b, i))
    i += 1

C.sort(reverse=True)
ans = 0

for i, c in enumerate(C):
    if i % 2 == 0:
        ans += A[c[1]][0]
    else:
        ans -= A[c[1]][1]

print(ans)

Submission status: Accepted

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

table = [[0] * 4 for i in range(N)]
for i, (Ai, Bi) in enumerate(AB):
    table[i][0] = Ai
    table[i][1] = Bi 
    table[i][2] = Ai + Bi
    table[i][3] = i

    
table_A = list(table)
table_A.sort(key=lambda x: -x[0])
table_A.sort(key=lambda x: -x[2])

table_B = list(table)
table_B.sort(key=lambda x: -x[1])
table_B.sort(key=lambda x: -x[2])

ans = 0
idx_a = 0
idx_b = 0
state = [0] * N
for i in range(N):
    # print("#", ans, idx_a, idx_b)
    # print("#", state)
    if i % 2:
        while True:
            A_tmp, B_tmp, score_tmp, i_tmp = table_B[idx_b]
            if state[i_tmp] < 1:
                # print("## b", B_tmp)
                ans -= B_tmp # inverted
                state[i_tmp] = 1
                idx_b += 1
                break
            idx_b += 1
    else:
        while True:
            A_tmp, B_tmp, score_tmp, i_tmp = table_A[idx_a]
            if state[i_tmp] < 1:
                # print("## a", A_tmp)
                ans += A_tmp
                state[i_tmp] = 1
                idx_a += 1
                break
            idx_a += 1

print(ans)

Submission status: Accepted

Solution code:
#!/Users/takahiro.kida/git/atcorder/venv/bin/python

import numpy as np
import scipy

from collections import defaultdict
from pprint import pprint

N = int(input())
A = []
B = []
diff = []
points = [] # points[i] = [A,B,diff]
for i in range(N):
    a,b = [int(x) for x in input().split(" ")]
    points.append([a,b,a+b])

sorted_points = sorted(points, key=lambda x: x[2], reverse=True)
point_a, point_b = 0,0

turn_A = True
for p in sorted_points:
    if turn_A:
        point_a += p[0]
        turn_A = False
    else:
        point_b += p[1]
        turn_A = True

print(point_a - point_b)

Submission status: Accepted

Solution code:
N = int(input())
P = []

for i in range(N):
    A, B = map(int, input().split())
    P.append((A + B, -A, B))

P.sort()
P.reverse()

ans = 0
turn = 1
for i in range(N):
    ans -= P[i][turn]
    turn = 3 - turn

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
sums = [0 for i in range(N)]
sum_dic = {}
for i in range(N):
    A[i], B[i] = map(int, input().split())
    sums[i] = A[i] + B[i]
    if sums[i] in sum_dic.keys():
        sum_dic[sums[i]].append(i)
    else:
        sum_dic[sums[i]] = [i]
    
sums.sort(reverse=True)
ans = 0
for i in range(len(sums)):
    if i % 2 == 0:
        ans += A[sum_dic[sums[i]].pop()]
    else:
        ans -= B[sum_dic[sums[i]].pop()]

#print(diff)
#print(a_dic)
#print(b_dic)
print(ans)

Submission status: Accepted

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

takahashi = 0
aoki = 0

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=[0 for i in range(N)]
B=[0 for i in range(N)]
for i in range(N):
    A[i],B[i]=map(int,input().split())
X=sorted([(B[i]+A[i],A[i],B[i],i) for i in range(N)])
Y=sorted([(A[i]+B[i],B[i],A[i],i) for i in range(N)])
used=[0 for i in range(N)]
tkhs=0
aoki=0
while(len(X)>0 and len(Y)>0):
    while(True):
        if len(X)>0 and used[X[-1][3]]==1:
            X.pop()
        else:
            break
    if len(X)==0:
        break
    tkhs+=X[-1][1]
    used[X[-1][3]]=1
    X.pop()
    while(True):
        if len(Y)>0 and used[Y[-1][3]]==1:
            Y.pop()
        else:
            break
    if len(Y)==0:
        break
    aoki+=Y[-1][1]
    used[Y[-1][3]]=1
    Y.pop()
print(tkhs-aoki)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

#print(AB)
point = np.zeros(N)
for i in range(N):
    point[i] = AB[i][0] + AB[i][1]
#for Ai, Bi in AB:
 #   point.append(Ai+Bi)

#print(point)

pointMaxIndex = np.argsort(-point)
#print(pointMaxIndex)

Tpoint = 0
Apoint = 0
for i in range(N):
    if i % 2 == 0:
        Tpoint += AB[pointMaxIndex[i]][0]
    else:
        Apoint += AB[pointMaxIndex[i]][1]

ans = Tpoint - Apoint

print(ans)

Submission status: Accepted

Solution code:
N = int(input().strip())

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

pair_sum = sorted(pair_sum, reverse=True)
pair_sum_first = 0

for i in range(1, N, 2):
  pair_sum_first += pair_sum[i]
print("%d" % (sumA - pair_sum_first))

Submission status: Accepted

Solution code:
N = int(input())

An = []
Bn = []

results = dict()
for i in range(N):
    a, b = map(int, input().split())
    hyoka = (a, b)
    wa = a + b
    if wa in results:
        results[wa].append(hyoka)
    else:
        results[wa] = [hyoka]

sa = 0
turn = 1
for _, v in sorted(results.items(), reverse=True):
    for hyoka in v:
        if turn % 2 == 1:
            sa += hyoka[0]
        else:
            sa -= hyoka[1]
        turn += 1
print(sa)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
A = [None] * N
B = [None] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())

C = [[A[i] + B[i], i] for i in range(N)]
C.sort(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:
N = int(input())

scores = []

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

scores.sort(reverse=True)
result = 0
for i in range(0, N):
    if i % 2 == 0:
        result += scores[i][1]
    else:
        result -= scores[i][2]

print(result)

Submission status: Accepted

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

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

ab_sum.sort(reverse = True)

a = 0

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A_list = []
B_list = []
sum_list = []
ans = 0


for i in range(N):
	A, B = map(int, input().split())
	A_list.append(A)
	B_list.append(B)
	sum_list.append([A+B, i])

sum_list.sort()

for i in range(N):
	point, j = sum_list.pop()
	if i%2 == 0:
		ans += A_list[j]
	else:
		ans -= B_list[j]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array([0 for i in range(N)], np.int64)
B = np.array([0 for i in range(N)], np.int64)
TA = 0
AO = 0
for i in range(N):
    A[i], B[i] = map(int, input().split())
AmB = np.array([A[i] + B[i] for i in range(N)])
# sort_index = A.argsort()[::-1]
# A = A[sort_index]
# B = B[sort_index]
# AmB = AmB[sort_index]
index = np.argsort(AmB)[::-1]
print(np.sum(A[index[::2]]) - np.sum(B[index[1::2]]))

Submission status: Accepted

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

T = []
A = []
eaten = [False]*N

happiness1 = sorted(happiness, key=lambda x:(x[2], x[0], x[1]))
happiness2 = sorted(happiness, key=lambda x:(x[2], x[1], x[0]))

for i in range(N):
    if i % 2 == 0: # Takahashi
        while True:
            a, _, _, idx = happiness1.pop()
            if not eaten[idx]:
                eaten[idx] = True
                T.append(a)
                break
    else: # Aoki
        while True:
            _, b, _, idx = happiness2.pop()
            if not eaten[idx]:
                eaten[idx] = True
                A.append(b)
                break

print(sum(T) - sum(A))

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)

t = 0
for ab in AB[0::2]:
    t += ab[1]

a = 0
for ab in AB[1::2]:
    a += ab[2]
    
print(t - a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, AB):
    
    t = []
    for a, b in AB:
        t.append((a+b, a, b))
    
    t.sort(reverse=True)
    A = 0
    B = 0
    for i, (_, a, b) in enumerate(t):
        if i % 2 == 0:
            A += a
        else:
            B += b
    return A- B


def main():
    N = read_int()
    AB = [read_int_n() for _ in range(N)]
    print(slv(N, AB))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
dinner=[]
for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    dinner.append([a,b])

dinner=sorted(dinner,key=lambda x:x[1]+x[0])
takahashi=0
aoki=0

while True:
    d=dinner.pop(-1)
    takahashi+=d[0]
    if len(dinner)==0:
        break
    d=dinner.pop(-1)
    aoki+=d[1]
    if len(dinner)==0:
        break

print(takahashi-aoki)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import heapq
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n = II()
src = []
for i in range(n):
    a, b = LI()
    src.append((a + b, a, b))
src.sort()


# print(*src, sep='\n')


score_a, score_b = 0, 0
for i in range(n):
    if i % 2:
        x, a, b = src.pop()
        score_b += b

    else:
        x, a, b = src.pop()
        score_a += a

print(score_a - score_b)

Submission status: Accepted

Solution code:
n = int(input())
ab = [list(map(int,input().split())) for i in range(n)]
c = [[ab[k][0] + ab[k][1], ab[k][0], ab[k][1]] for k in range(n)]
c.sort()
c.reverse()
taka = 0
aoki = 0
for i in range(n): # 第iターン (i=0, 1,..., n-1)
    if i%2 ==0: # 高橋くんのターン
        taka += c[i][1]
    else: # 青木くんのターン
        aoki += c[i][2]

print(taka - aoki)

Submission status: Accepted

Solution code:
#import pandas as pd
n=int(input())
O=[]
#A=[]
#B=[]
for i in range(n):
    a,b=map(int, input().split(" "))
    O.append([a+b,a,b])
  
#O=sorted(O, key=lambda x: (x[0],x[1]))[::-1]
O=sorted(O, key=lambda x: (x[0]))[::-1]
#print(O)

count=0
for num in range(n):
    if num%2==0:
        count+=O[num][1]
    else:
        count-=O[num][2]

print(count)

Submission status: Accepted

Solution code:
n = int(input())
h = [[int(i) for i in input().split()] for j in range(n)]
s = [[h[i][0]+ h[i][1], i] for i in range(n)]
s.sort(reverse=True)
ans = 0
flag = 0
for i in range(n):
    if flag == 0:
        ans += h[s[i][1]][0]
        flag = 1
    else:
        ans -= h[s[i][1]][1]
        flag = 0
print(ans)

Submission status: Accepted

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

diff = [[0] for i in range(n)]
sorted_diff = [[0] for i in range(n)]

for i in range(n):
  diff[i][0] = info[i][0]+info[i][1] 

for i in range(n):
  info[i].extend(diff[i])
#print(info)

sorted_diff = sorted(info, key=lambda x: x[2])

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
S = {}

for i in range(N):
    Ai, Bi = map(int, input().split())
    A.append(Ai)
    B.append(Bi)
    S[i] = (Ai + Bi)

A_total = 0
B_total = 0
i = 0
for k, v in sorted(S.items(), key=lambda x: -x[1]):
    if i % 2 == 0:
        A_total += A[k]
    else:
        B_total += B[k]
    i += 1

print(A_total - B_total)

Submission status: Accepted

Solution code:
N = int(input())
ABlist = []

for n in range(N):
    a, b = input().split()
    a = int(a)
    b = int(b)
    winP = a + b
    ABlist.append((a, b, winP))

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

# 勝ち点の強いやつから取っていく？
Acount = 0
Bcount = 0

for i,x in enumerate(sortedList):
    if i % 2 == 0:
        Acount = Acount + x[0]
    elif i % 2 == 1:
        Bcount = Bcount + x[1]

print(Acount - Bcount)

Submission status: Accepted

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

T = []
for i in range(N):
  T += [(-A[i]-B[i], i)]
T.sort()

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

Submission status: Accepted

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

B = [[a[0],a[1],a[0]+a[1]] for a in A]
B = sorted(B, key=lambda x: x[2], reverse=True)
Taka=0
Aoki=0

for i, b in enumerate(B):
    if i%2==0:
        Taka+=b[0]
    else:
        Aoki+=b[1]
    

print(Taka-Aoki)

Submission status: Accepted

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


for i in range(n):
    c=[]
    c=[int(j) for j in input().split()]
    absa.append([c[0],c[1],c[1]+c[0]])


from operator import itemgetter
absa.sort(key=itemgetter(2))

t=0
k=0
for i in range(n):
    if i%2==0:
        t+=absa[-1][0]
        del absa[-1]
    else:
        k+=absa[-1][1]
        del absa[-1]
    
    

print(t-k)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    s = sum(dat[::2])
    a = [dat[i*2] + dat[i*2+1] for i in xrange(n)]
    a.sort(reverse=True)
    print s - sum(a[1::2])
main()

Submission status: Accepted

Solution code:
l = []
c = int(input())
for i in range(c):
    a,b = map(int, input().split())
    l.append([a,b,abs(a+b)])
l.sort(key=lambda x:x[2])
k = l[::-1]
s = 0
for i in range(c):
    if i%2==0:
        s +=k[i][0]
    else:s-=k[i][1]
print(s)

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])
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:
# coding: utf-8
n=int(input())
data=[]
for i in range(n):
    data.append(list(map(int,input().split())))
data=sorted(data,key=lambda x:-x[0]-x[1])
a=0
b=0
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())
Ascore = 0
Bscore = 0
A, B = [], []
for i in range(n):
    a, b = map(int, input().split())
    c = a + b
    A.append((a,b,c))
A.sort(key=lambda x:x[2], reverse=True)
Aturn = True
for a in A:
    if Aturn:
        Ascore += a[0]
        Aturn = False
    else:
        Bscore += a[1]
        Aturn = True
print(Ascore-Bscore)

Submission status: Accepted

Solution code:
N = int(input())
C = [list(map(int, input().split())) for _ in range(N)]
C = sorted(map(lambda x: ((x[0] + x[1]), x), C), reverse=True)
#print(C)
#print(C[::2])
#print(C[1::2])
print(sum(c[1][0] for c in C[::2]) - sum(c[1][1] for c in C[1::2]))

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

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

print(t - a)

Submission status: Accepted

Solution code:
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
n=I()
dl=[]
taka=[]
aoki=[]
for i in range(n):
    a,b=IL()
    dl.append([i,a,b,a+b])
dl.sort(key=lambda x:-x[3])
t=1
ans=0
for i in range(n):
    if t==1:
        taka.append(i)
        t=-1
    else:
        aoki.append(i)
        t=1
for i in taka:
    ans+=dl[i][1]
for i in aoki:
    ans-=dl[i][2]
print(ans)

Submission status: Accepted

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

count = 0
ans = 0
for i in sorted(a_b,reverse=True):
    if count % 2 == 0:
        ans += a[i[1]]
    else:
        ans -= b[i[1]]
    count += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np

n =int(input())

# n行の複数列ある数値を二次元配列へ。x[n][2]
ab = [[int(i) for i in input().split()] for i in range(n)] 

#print(ab)

ab.sort(key = lambda x: -x[1]-x[0])
#print(ab)
answer = 0
for i in range(n):
    if i % 2 == 0:
        answer += ab[i][0]
    else:
        answer -= ab[i][1]
print(answer)

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
taka = []
aoki = []
sum_of_both = {}
for i in range(N):
    a, b = map(int, input().split())
    taka.append(a)
    aoki.append(b)
    sum_of_both[i] = a + b

p_taka = 0
p_aoki = 0
turn = 0
for k, v in sorted(sum_of_both.items(), key=lambda x: x[1], reverse=True):
    if turn % 2 == 0:
        p_taka += taka[k]
    else:
        p_aoki += aoki[k]
    turn += 1

print(p_taka - p_aoki)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
a_b = []
point_a = 0
point_b = 0
for i in range(n):
    tmp1 , tmp2 = map(int,input().split())
    a.append(tmp1)
    b.append(tmp2)
    a_b.append((tmp1+tmp2,i))
a_b.sort()
while a_b:
    point_a += a[a_b.pop(-1)[1]]
    if a_b:
        point_b += b[a_b.pop(-1)[1]]
    else:
        break
print(point_a - point_b)

Submission status: Accepted

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

ABC = [[a, b, a+b] for a, b in AB]
ABC = sorted(ABC, key=itemgetter(2), reverse=True)
ans = 0
flag = True
for a, b, c in ABC:
    if flag:
        ans += a
    else:
        ans -= b
    flag = not flag
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
takahashi_u = []
aoki_u = []
sum_u = []
takahashi_eat = []
aoki_eat = []
for i in range(n):
  a, b = map(int, input().split())
  takahashi_u.append(a)
  aoki_u.append(b)
  sum_u.append(a+b)
arr = np.array([sum_u, takahashi_u, aoki_u])
sort_index = np.argsort(arr[0])[::-1]
sort_arr = arr[:, sort_index]
takahashi_arr = sort_arr[1, 0::2]
aoki_arr = sort_arr[2, 1::2]
print(sum(takahashi_arr) - sum(aoki_arr))

Submission status: Accepted

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

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

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

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

print(takahashi-aoki)

Submission status: Accepted

Solution code:
# S = input()
N = int(input())

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

ans = 0

P = [[0,0,0] for _ in range(N)]

for i in range(N):
    P[i][0] = AB[i][0] + AB[i][1]
    P[i][1] = AB[i][0]
    P[i][2] = AB[i][1]

P.sort()
P.reverse()

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = []
b = []
c = []
for i in range(n):
  a_i, b_i = map(int,input().split())
  a.append(a_i)
  b.append(b_i)
  c.append(a_i+b_i)
c.sort(reverse=True)
k = int((n+1)/2)
print(np.sum(c[::2])-np.sum(b))

Submission status: Accepted

Solution code:
N = int(input())
arr = []
for i in range(N):
    a,b = list(map(int, input().split()))
    arr.append([a,b])
arr = sorted(arr, key=lambda x: x[0]+x[1], reverse=True)
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:
from string import ascii_lowercase
def inpl(): return list(map(int, input().split()))
tkao = [0, 0]
t = 0

N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
    A[i], B[i] = inpl()
C = [A, B]

S = sorted([((B[i] + A[i]), i) for i in range(N)], key=lambda x: x[0])

for i in range(N):
    tkao[i%2] += C[i%2][S.pop()[1]]

print(tkao[0] - tkao[1])

Submission status: Accepted

Solution code:
N = int(input())
AB = [[int(i) for i in input().split()] for _ in range(N)]
C = [0]*N
tk = 0
ao = 0
for i in range(N):
    C[i] = (sum(AB[i]),i)
for i,j in enumerate(sorted(C)[::-1]):
    tmp = j[1]
    if i % 2 == 0:
        tk+=AB[tmp][0]
    else:
        ao+=AB[tmp][1]
print(tk-ao)

Submission status: Accepted

Solution code:
from math import ceil
N = int(input())
b = []
c = []

for i in range(N):
    A,B = map(int,input().split())
    b.append(B)
    c.append(-1*(A+B))

c.sort()
ans = 0 
for i in range(ceil(N/2)):
    ans -= c[2*i]

ans -= sum(b)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
cost = [[0, 0] for _ in range(n)]
for i in range(n):
    a[i], b[i] = [int(i) for i in input().split()]
    cost[i] = [a[i]+b[i], i]
# a[i]-b[i]がmaxのものから取っていく
cost.sort()
cost.reverse()
scoreA = 0
scoreB = 0
for i in range(len(cost)):
    if i % 2 == 0:
        scoreA += a[cost[i][1]]
    else:
        scoreB += b[cost[i][1]]
print(scoreA -scoreB)

Submission status: Accepted

Solution code:
N=int(input())
dish=[]
for i in range(N):
  a,b=map(int,input().split())
  dish.append([a,b])
d2=sorted(dish,key=lambda x:-x[1]-x[0])
ans=0
for i in range(0,N,2):
  ans+=d2[i][0]
for i in range(1,N,2):
  ans-=d2[i][1]
print(ans)

Submission status: Accepted

Solution code:
import collections

def solve():
    N = int(input())
    A = []
    B = []
    for i in range(N):
        (a, b) = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)

    pq = sorted([(a,b) for a, b in zip(A, B)], key=lambda x: sum(x))
    pq = collections.deque(pq)
    # print(pq)

    score = [0, 0]
    idx = 0
    while pq:
        x = pq.pop()
        score[idx] += x[idx]
        idx = (idx + 1) % 2
    # print(score)

    return score[0] - score[1]


def main():
    ans = solve()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

P = sorted(P, key=lambda p: - p[0] - p[1])

ret = 0

for i, p in enumerate(P):
    if i % 2 == 0:
        ret += p[0]
    else:
        ret -= p[1]

print(ret)

Submission status: Accepted

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

dam = sorted(dam, key=lambda x: x[1], reverse=True)
aok = dam[0::2]
tak = dam[1::2]

asum = 0
for i in aok:
    asum = asum + A[i[0]]

bsum = 0
for i in tak:
    bsum = bsum + B[i[0]]

print(asum - bsum)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    points = []
    for i in range(n):
        a, b = map(int, input().split())
        points.append((a, b, a + b))

    # print(points)
    points = sorted(points, key=lambda x: x[2], reverse=True)
    # print(points)

    t = 0
    a = 0
    for i, p in enumerate(points):
        if i == 0 or i % 2 == 0:
            t += p[0]
        else:
            a += p[1]

    print(t - a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

dishes = []
n = getN()
for i in range(n):
    dishes.append(getMN())

dishes.sort(key=lambda x: -(x[0] + x[1]))
#print(dishes)
ans = 0
for i, d in enumerate(dishes):
    if i % 2 == 0:
        ans += d[0]
    else:
        ans -= d[1]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
AB = []
for i in range(N):
    AB.append(list(map(int,input().split())))
AB = np.array(AB)
scores = np.argsort(np.sum(AB,axis=1))[::-1]
# diff = np.zeros(1).astype("int64")
ascore = AB[scores[0::2]][:,0].sum()
bscore = AB[scores[1::2]][:,1].sum()
print(ascore-bscore)

Submission status: Accepted

Solution code:
n = int(input())
a,b = [],[]
lisC = []
for i in range(n):
    tmp = list(int(i) for i in input().split())
    a.append(tmp[0])
    b.append(tmp[1])
    lisC.append((sum(tmp),i))
lisC = sorted(lisC)
res = 0
for i in range(n):
    if i%2==0:
        res += a[lisC[n-1-i][1]]
    else:
        res -= b[lisC[n-1-i][1]]
print(res)