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
import heapq
import re
from heapq import heapify, heappop, heappush
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

# ここから書き始める
n = int(input())
a = [0 for i in range(n)]
b = [0 for i in range(n)]
x = [[] for i in range(n)]
y = [[] for i in range(n)]
for i in range(n):
    a[i], b[i] = map(int, input().split())
    x[i] = [a[i] + b[i], a[i], i]
    y[i] = [b[i] + a[i], b[i], i]
x.sort(reverse=True, key=lambda i : (i[0], i[1]))
y.sort(reverse=True, key=lambda i : (i[0], i[1]))
# print("x =", x)
# print("y =", y)
ans = 0
used = [0 for i in range(n)]
takahashi = True
t_cnt = 0
a_cnt = 0
cnt = 0
while cnt < n:
    if takahashi and t_cnt < n:
        if used[x[t_cnt][2]] == 0:
            ans += x[t_cnt][1]
            takahashi = False
            cnt += 1
            used[x[t_cnt][2]] = 1
        t_cnt += 1
    elif (not takahashi) and a_cnt < n:
        if used[y[a_cnt][2]] == 0:
            ans -= y[a_cnt][1]
            takahashi = True
            cnt += 1
            used[y[a_cnt][2]] = 1
        a_cnt += 1
    # print(ans)
print(ans)

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(key= lambda x:x[0])

ans = 0
for i in range(n):
    v, a, b = ab.pop()
    if i % 2 == 0:
        ans += a
    else:
        ans -= b

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

N=int(input())
A=[0]*N
B=[0]*N
C=[0]*N

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

t=0
a=0

C.sort(key= lambda x: x[2],reverse=True)
#print(C)
#C.sort(key= lambda x: x[0],reverse=True)
for i in range(N):
  if i%2==0:
    t += C[i][0]
  else:
    a += C[i][1]

print(t-a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

n = int(input())
happy = []
ans = 0
for i in range(n):
    a, b = tuple(map(int, input().split()))
    heapq.heappush(happy, - (a + b))
    ans += a
while len(happy) != 0:
    heapq.heappop(happy)
    if len(happy) != 0:
        h = heapq.heappop(happy)
        ans += h
    else:
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    prior = sorted(dish, key=lambda x: x[0] + x[1], reverse=True)
    pt_tak, pt_aok = 0, 0

    for i, (a, b) in enumerate(prior):
        if i%2 == 0:
            pt_tak += a
        else:
            pt_aok += b

    ans = pt_tak - pt_aok
    print(ans)

nikkei19q_c()

Submission status: Accepted

Solution code:
n = int(input())
atob = []
ans = 0
for i in range(n):
    x,y = map(int,input().split())
    ans += x
    atob.append(x+y)
    
atob.sort(reverse=True)

for i in range(n//2):
    ans -= atob[2*i+1]
    
print(ans)

Submission status: Accepted

Solution code:
import heapq
N = int(input())
AB = []
for i in range(N):
    ab = list(map(int, input().split()))
    AB.append([-ab[0] - ab[1], -ab[0], -ab[1]])

AB.sort(reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
sum_ab = [(i, v[0]+v[1]) for i, v in enumerate(ab)]
sum_ab.sort(key=lambda x: x[1], reverse=True)
t_res = 0
a_res = 0
for i,v in enumerate(sum_ab):
    if i % 2 == 0:
        t_res += ab[v[0]][0]
    else:
        a_res += ab[v[0]][1]
print(t_res-a_res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A, B, AB = [], [], []
for i in range(n):
    ai, bi = map(int, input().split())
    A.append(ai)
    B.append(bi)
    AB.append((ai + bi, i))
AB.sort(reverse=True)
bsum = sum(B)


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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    AB = [list(map(int, input().split())) for _ in range(n)]
    for i in range(n):
        a, b = AB[i]
        AB[i] = [a + b, a, b]
    AB.sort(key=lambda x: x[0], reverse=True)

    score_t = sum(AB[i][1] for i in range(n) if i % 2 == 0)
    score_a = sum(AB[i][2] for i in range(n) if i % 2 != 0)

    print(score_t - score_a)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    AB = [list(map(int, input().split())) for _ in range(n)]
    for i in range(n):
        a, b = AB[i]
        AB[i] = [a + b, a, b]
    AB.sort(key=lambda x: x[0], reverse=True)

    score_t, score_a = 0, 0
    for i in range(n):
        if i % 2 == 0:
            score_t += AB[i][1]
        else:
            score_a += AB[i][2]

    print(score_t - score_a)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    A, B, C = [], [], []
    for i in range(N):
        a, b = ZZ()
        A.append(a)
        B.append(b)
        C.append([a+b, i])
    C.sort(reverse=True)
    output = 0
    for i in range(N):
        _, j = C[i]
        if i%2 == 0: output += A[j]
        else: output -= B[j]
    print(output)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
A,B = LIR(N,2)

from operator import itemgetter
def index_sort(A):
    A_sort = sorted(enumerate(A),key=itemgetter(1))
    index = [a[0] for a in A_sort]
    sorted_A = [a[1] for a in A_sort]
    return index, sorted_A

C = [A[i]+B[i] for i in range(N)]
index,_ = index_sort(C)
ans = 0
for i in range(N)[::-1]:
    if i%2 == (N-1)%2:
        ans += A[index[i]]
    else:
        ans -= B[index[i]]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
t = []
for _ in range(N):
  a, b = map(int, input().split())
  t.append((a, b))
  
t.sort(key = lambda t: t[0] + t[1])
s = 0
while t:
  s += t.pop()[0]
  if t:
    s -= t.pop()[1]
    
print(s)

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    d = []
    for _ in range(n):
        a, b = ns()
        d.append((a + b, a, b))

    d.sort(reverse=True)
    turn = 1
    taka = 0
    ao = 0
    for i in range(n):
        if turn:
            taka += d[i][1]
        else:
            ao += d[i][2]
        turn ^= 1
    print(taka - ao)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
s=[None]*N
for i in range(N):
    A,B=map(int,input().split())
    s[i]=(A+B,A,B)
s.sort(reverse=1)
i=0
t=0
a=0
while i<N:
    if t:
        a-=s[i][2]
    else:
        a+=s[i][1]
    t=not t
    i+=1
print(a)

Submission status: Accepted

Solution code:
N = int(input())
l = []
for i in range(N):
    A, B = [int(_c) for _c in input().split(" ")]
    l.append({"A": A, "B": B, "sum": A + B})

l = sorted(l, key=lambda x: x["sum"], reverse=True)

a_total = 0
b_total = 0
for i, _l in enumerate(l):
    if i % 2 == 0:
        a_total += _l["A"]
    else:
        b_total += _l["B"]

print(a_total - b_total)

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

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

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

print(ans)

Submission status: Accepted

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

    info.sort(key=lambda x: -(x[0]+x[1]))
    takahashi = sum([val for i, (val, _) in enumerate(info) if i % 2 == 0])
    aoki = sum([val for i, (_, val) in enumerate(info) if i % 2 == 1])
    print(takahashi-aoki)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#problems1
N=int(input())
d=[]
for _ in range(N):
    x,y=map(int,input().split())
    d.append((x,y,x+y))
d.sort(key=lambda x:-x[2])
t=0
a=0
for i in range(N):
    if i%2==0:
        #高橋くん
        t+=d[i][0]
    else:
        a+=d[i][1]
print(t-a)

Submission status: Accepted

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

s = [(a+b, a, b) for a, b  in ab]
s.sort(reverse = True)
# print(s)

t = 0
a = 0
for i in range(n):
    if i % 2 == 0:
        t += s[i][1]
    else:
        a += s[i][2]

print(t-a)

Submission status: Accepted

Solution code:
import heapq

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

queue = []
for i in range(N):
    x, y = happiness[i]
    queue.append([-(x+y), x, y])

heapq.heapify(queue)
takahashi = 0
aoki = 0
for i in range(N):
    dish = heapq.heappop(queue)
    if i % 2 == 0:
        takahashi += dish[1]
    else:
        aoki += dish[2]

print(takahashi-aoki)

Submission status: Accepted

Solution code:
n = int(input())
ab = [list(map(int,input().split())) for _ in range(n)]
for i in range(n):
  ab[i].append(ab[i][0]+ab[i][1])
ab = sorted(ab,key = lambda x:x[2],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
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    AB=[]
    for i in range(n):
        a,b=map(int,input().split())
        AB.append((a+b,a,b))    
    AB.sort(reverse=True)
    x=sum([-b[2] for b in AB])
    sum_ab=sum([ab[0] for ab in AB[::2]])
    print(x+sum_ab)


if __name__=='__main__':
    main()

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 = []
ans = 0
for _ in range(n):
    a, b = map(int, readline().split())
    ab.append(a + b)
    ans -= b
ab.sort(reverse=True)
ans += sum(ab[::2])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b_sum = 0
c = []
for _ in range(n):
    a, b = map(int, input().split())
    b_sum += b
    c.append(a + b)
c.sort()
c = list(reversed(c))
print(sum(c[0::2]) - b_sum)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    AB = []
    for _ in range(N):
        a, b = map(int, input().split())
        ans -= b
        AB.append(a+b)
    AB.sort()

    for i in range(N):
        if i % 2 == 0:
            ans += AB.pop()
        else:
            AB.pop()
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

wa_a_b = []

for i in range(n):
    wa_a_b.append([ab[i][0]+ab[i][1],ab[i][0],ab[i][1]])
    
wa_a_b.sort(reverse=True)

ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
ab = [[0, 0, 0] for i in range(n)]
for i in range(n):
    a, b = (int(x) for x in input().split())
    ab[i][0] = a+b
    ab[i][1] = a
    ab[i][2] = 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:
N = int(input())
AB, ans = [], 0
for _ in range(N):
    a, b = map(int, input().split())
    AB.append([a+b, a, b])    
AB = sorted(AB, reverse=True)

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

Submission status: Accepted

Solution code:
d=[list(map(int,input().split()))for _ in[0]*int(input())];print(sum(sorted(sum(c)for c in d)[::-2])-sum(c[1]for c in d))

Submission status: Accepted

Solution code:
d = [list(map(int, input().split())) for _ in [0]*int(input())]
print(sum(sorted(sum(c) for c in d)[::-2]) - sum(c[1] for c in d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

abl = []

for _ in range(n):
    a,b = map(int,input().split())
    abl.append((a+b, a, b))

abl = sorted(abl, reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ab = []
for _ in range(n):
    a_, b_ = map(int, input().split())
    d = a_ + b_
    ab.append([a_, b_, d])
ab = sorted(ab, key=lambda x:x[2])[::-1]
print(sum([a[0] for a in ab[0::2]]) - sum([b[1] for b in ab[1::2]]))

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter
input = sys.stdin.readline


def main():
    N = int(input())
    AB = [None] * N
    for i in range(N):
        A, B = map(int, input().split())
        AB[i] = (A, B, A + B)

    AB.sort(reverse=True, key=itemgetter(2))
    sum_A = 0
    sum_B = 0
    for i in range(N):
        if i % 2 == 0:
            sum_A += AB[i][0]
        else:
            sum_B += AB[i][1]

    ans = sum_A - sum_B
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
D = [[i, abs(a+b)] for i, (a, b) in enumerate(AB)]
D.sort(key=lambda x:x[1], reverse=True)
ta = [0, 0]
for j, (i, d) in enumerate(D):
  ta[j%2]+=AB[i][j%2]
print(ta[0]-ta[1])

Submission status: Accepted

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

from operator import itemgetter

N = int(input())
AB = []

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

AB.sort(key=itemgetter(2), reverse=True)

takahashi = True
ans = 0

for i in range(N):
    if takahashi:
        ans += AB[i][0]
    else:
        ans -= AB[i][1]
    takahashi = not takahashi

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

c.sort(key=lambda x:x[1],reverse=True)

ans = 0
for i in range(n):
    if i%2==0:
        ans+=a[c[i][0]]
    else:
        ans-=b[c[i][0]]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = []
for i in range(n):
    a,b = map(int,input().split())
    tmp = [a+b,a,b]
    li.append(tmp)
li.sort(reverse=True)
x_sum = 0
y_sum = 0
for i,l in enumerate(li):
    if i%2 == 0:
        x_sum += l[1]
    else:
        y_sum += l[2]
print(x_sum-y_sum)
#print(li)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    N = int(input())
    LL = []
    for i in range(N):
        L = list(map(int, input().split()))
        LL.append(L + [sum(L)])

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

    cntA = 0
    cntT = 0
    for i in range(len(LL)):
        if i % 2 == 0:
            cntT += LL[i][0]
        else:
            cntA += LL[i][1]
    print(cntT - cntA)


resolve()

Submission status: Accepted

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

cnt,A=0,[]
for a,b in AB:
    cnt -=b
    A.append(a+b)

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

Submission status: Accepted

Solution code:
#全国統一プログラミング王決定戦予選-C Different Strokes
"""
問題を言い換えた時に、
高橋くんがa+bの幸福度を得られる時に、
青木さんが-a-bの妨害をできる
と言い換えても、最適な行動が取れる。
よって、a+bの降順にソートして、
高橋くんの番なら+a
青木さんの番なら-b
することで答えが得られる
"""
import sys
from operator import itemgetter
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

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

AB.sort(reverse=True)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from collections import OrderedDict

def main():
    N, *AB = map(int, open(0).read().split())
    C = [0]*N
    ans = 0
    for i in range(N):
        a = AB[i * 2 + 0]
        b = AB[i * 2 + 1]
        C[i] = a + b
        ans -= b
    C.sort(reverse=True)
    eaten = (N + 1) // 2
    for i in range(eaten):
        ans += C[2*i]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from operator import itemgetter
n, *AB = map(int, open(0).read().split())
AB = sorted(zip(AB[::2], AB[1::2]), key=sum, reverse=1)
print(sum(map(itemgetter(0), AB[::2])) - sum(map(itemgetter(1), AB[1::2])))

Submission status: Accepted

Solution code:
from operator import itemgetter
n, *AB = map(int, open(0).read().split())
A, B = AB[::2], AB[1::2]
AB = sorted([(a, b) for a, b in zip(A, B)], key=sum, reverse=1)
print(sum(map(itemgetter(0), AB[::2])) - sum(map(itemgetter(1), AB[1::2])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

dif.sort(key=lambda x: x[0])
Takahashi = 0
Aoki = 0
while dif:
    Takahashi += dif.pop()[1]
    if not dif:
        break
    Aoki += dif.pop()[2]

print(Takahashi-Aoki)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, rl().split())
    
    d = [0] * N
    for i in range(N):
        d[i] = A[i] + B[i]
    d.sort(reverse=True)
    
    ans = sum(d[::2]) - sum(B)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    A = []
    B = []
    for _ in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    V = []
    for a, b in zip(A, B):
        V.append((a + b, a, b))

    V.sort(reverse=True)
    takahashi = 0
    aoki = 0
    for i in range(N):
        if i % 2 == 0:
            takahashi += V[i][1]
        else:
            aoki += V[i][2]

    print(takahashi - aoki)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

L=sorted(L, reverse=True, key=lambda x: x[2])
taka=0
ao=0
for i in range(N):
    l=L[i]
    a,b,_=l
    if i%2==0:
        taka+=a
    else:
        ao+=b
print(taka-ao)

Submission status: Accepted

Solution code:
n = int(input())
l = [0] * n
for i in range(n):
    a, b = map(int, input().split())
    l[i] = (a + b, a, b)
ans = 0
l.sort(reverse=True)
for i in range(n):
    if i % 2:
        ans -= l[i][2]
    else:
        ans += l[i][1]
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)

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

Submission status: Accepted

Solution code:
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tup(t=int):return tuple(pin(t))
#%%code
def resolve():
    N=int(input())
    AB=[tup()for i in range(N)]
    AB.sort(key=lambda x:sum(x),reverse=True)
    #print(AB)

    n=N
    turn=0
    his=0
    her=0
    for i in range(N):
        if i%2==0:
            his+=AB[i][0]
        else:her+=AB[i][1]
    print((his-her))
            
#%%submit!
resolve()

Submission status: Accepted

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

ab.sort(key=lambda p: - p[0] - p[1])
print(sum(p[0] for p in ab[::2]) - sum(p[1] for p in ab[1::2]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
print(sum(p[1+i%2]for i,p in enumerate(sorted([(lambda a,b:(-a-b,a,-b))(*map(int,input().split()))for i in range(int(input()))]))))

Submission status: Accepted

Solution code:
print(sum(-b if i%2 else a for i,(p,a,b)in enumerate(sorted([(lambda a,b:(-a-b,a,b))(*map(int,input().split()))for i in range(int(input()))]))))