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, math
from functools import lru_cache
import heapq
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    A, B = i2(N)
    ans = 0

    C = [(a + b, i) for a, b, i in zip(A, B, range(N))]
    C.sort(reverse=True)

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
ans, a, b = 0, [], []
for _ in range(n):
    x, y = map(int, input().split())
    a.append(x+y)
    b.append([x, y])
num = [x for x in range(n)]
a, a0 = zip(*sorted(zip(a, num)))
a, a0 = list(a), list(a0)

for i in range(n):
    val = a0.pop()
    a.pop()
    if i%2 == 0:
       ans += b[val][0]
    else:
       ans -= b[val][1]
print(ans)

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    dishes = []
    for i in range(n):
        a, b = input_int_list()
        dishes.append((a, b, a + b))
    # a+bの合計値でソートする
    dishes = sorted(dishes, key=lambda x: x[2])
    t_sum = 0
    a_sum = 0
    is_takahashi = True  # True => Tさんのターン
    while dishes:
        a, b, _ = dishes.pop()
        if is_takahashi:
            t_sum += a
        else:
            a_sum += b
        is_takahashi = not is_takahashi
    print(t_sum - a_sum)

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

l.sort()
l.reverse()
s=0
for i in range(0,n,2):
  s+=l[i][1]
for i in range(1,n,2):
  s-=l[i][2]
print(s)

Submission status: Accepted

Solution code:
N = int(input())

AB_list = [[0,0,0] for i in range(N)]

for i in range(N):
    a,b = map(int,input().split())
    AB_list[i][0] = a
    AB_list[i][1] = b
    AB_list[i][2] = a+b
AB_list.sort(reverse=True,key=lambda x:x[2])

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

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

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.append(b)
  
l = []
for i in range(N):
  l.append(A[i]+B[i])
l.sort(reverse=True)

ans = 0
for i in range(N//2):
  ans += l[2*i]
if N % 2 == 1:
  ans += l[-1]
ans -= sum(B)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

aoki = 0
for _ in range(N):
    a, b = map(int, input().split())
    cooking.append([a + b, a, b])
chokudai = 0
cooking.sort(reverse=True)
for i in range(0, N):
    if i % 2 == 0:
        chokudai += cooking[i][1]
    else:
        aoki += cooking[i][2]
ans = chokudai - aoki

print(ans)

Submission status: Accepted

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

aoki = 0
for _ in range(N):
    a, b = map(int, input().split())
    cooking.append([a + b, a, b])
    aoki += b
chokudai = 0
cooking.sort(reverse=True)
for i in range(0, N , 2):
    chokudai += cooking[i][1]
    aoki -= cooking[i][2]
ans = chokudai - aoki

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l=[list(map(int,input().split())) for i in range(N)]
l_after=sorted(((i+j,i,j) for i,j in l),reverse=True)
print(sum(b for a,b,c in l_after[::2])-sum(c for a,b,c in l_after[1::2]))

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))
    B.append((i,a+b))
B.sort(key=lambda x:x[1], reverse=True)
res = 0
for i, _ in B[::2]:
    res += A[i][0]
for i, _ in B[1::2]:
    res -= A[i][1]
print(res)

Submission status: Accepted

Solution code:

N = int(input())

ab = [tuple(map(int,input().split())) for _ in range(N)]

diff = sorted(((a+b,a,b) for a,b in ab), reverse=True)

print(sum(a for _,a,b in diff[::2]) - sum(b for _,a,b in diff[1::2]))

Submission status: Accepted

Solution code:
n = int(input())
a, b = [0]*n, [0]*n
for i in range(n):
  x, y = map(int, input().split())
  a[i], b[i] = x+y, y
ans = sum(sorted(a, reverse=True)[::2])-sum(b)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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

tak_score=0
aok_score=0
turn=0
while pablist:
  p,a,b=pablist.pop()
  if turn&1==0:
    tak_score+=a
  else:
    aok_score+=b
  turn+=1
  
print(tak_score-aok_score)

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
n= ni()
ab = []
dif = []
for i in range(n):
    a,b = ma()
    ab.append((a,b))
    dif.append((a+b,i))
dif.sort(reverse=True)
tak = 0
ao = 0
for i in range(n):
    d,idx = dif[i]
    if i%2==0:
        tak+=ab[idx][0]
    else:
        ao+=ab[idx][1]
print(tak-ao)
#print(tak,ao)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
ABs = []
As = []
Bs = []

tk = 0
ao = 0

for i in range(N):
  a,b = map(int, input().split())
  As.append(a)
  Bs.append(b)
  ABs.append((a+b, i))
  
ABs.sort(key=lambda x:x[0], reverse=True)

for i in range(N):
  if i%2 == 0:
    tk += As[ABs[i][1]]
  else:
    ao += Bs[ABs[i][1]]
    
print(tk-ao)

Submission status: Accepted

Solution code:
n=int(input())
a,b=[],0
for i in range(n):
  p,q=map(int,input().split())
  a+=[p+q]
  b-=q
print(sum(sorted(a)[::-2])+b)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
li = []
l = []
di = {}
for i in range(n):
    a, b = map(int, input().split())
    li.append((a+b, -a, i))
    l.append((b+a, -b, i))
    di[i] = False
li.sort(reverse=True)
l.sort(reverse=True)
li = deque(li)
l = deque(l)
left, right = 0, 0
while li:
    while True and li:
        p = li.popleft()
        if di[p[2]]:
            continue
        di[p[2]] = True
        left += -p[1]
        break
    if len(l) != 0:
        while True and l:
            q = l.popleft()
            if di[q[2]]:
                continue
            di[q[2]] = True
            right += -q[1]
            break
print(left-right)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N

ans = 0
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a + b
    ans -= b
A = sorted(A, reverse=True)
for i in range(N):
    if i % 2 == 0:
        ans += A[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=[]
for i in range(n):
    a,b=MI()
    lis.append([a,b,a+b])
lis.sort(key=lambda x:x[2],reverse=True)
ans=0
for i in range(n):
    if i%2==0:
        ans+=lis[i][0]
    else:
        ans-=lis[i][1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

c1 = 0
c2 = 0
for i,[X,Y,Z] in enumerate(x):
    if i%2 == 0:
        c1 += X
    else:
        c2 += Y
print(c1-c2)

Submission status: Accepted

Solution code:
import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


def main():
    n = ii()
    x = []  
    ans = 0     
    for i in range(n):
        a, b = mi()
        x.append(a+b)
        ans -= b
    x.sort(reverse=True)
    ans += sum(x[::2])    
    print(ans)






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.sort(reverse=True)
t,a=0,0
for i in range(0,n,2):
    t+=l[i][1]
for i in range(1,n,2):
    a+=l[i][2]
print(t-a)

Submission status: Accepted

Solution code:
n=int(input())

score=[]
bsum=0

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

score.sort(reverse=True)

for i,v in enumerate(score):
    if i%2==0:
        bsum+=v

print(bsum)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline

sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    X = 0
    AB = []
    for _ in range(N):
        A, B = map(int, readline().split())
        X -= B
        AB.append(A + B)
    AB.sort(reverse=True)
    for ab in AB[::2]:
        X += ab
    print(X)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

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

taka = 0
aoki = 0
ABC.sort(key=lambda x:x[2])
while ABC:
    
    taka += ABC.pop()[0]
    if len(ABC)==0:
        break
    aoki += ABC.pop()[1]
print(taka-aoki)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    AB = [LI() for _ in range(N)]
    
    AB.sort(key=lambda x: x[0] + x[1], reverse=True)

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

    print(score_t - score_a)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    AB = []
    for _ in range(N):
        A, B = read_ints()
        AB.append((A+B, A, B))
    AB.sort()
    scores = [0, 0]
    p = 0
    while AB:
        _, *s = AB.pop()
        scores[p] += s[p]
        p ^= 1
    return scores[0]-scores[1]


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
N = int(input())
AB = [0 for _ in range(N)]
SB = 0
for i in range(N):
  A, B = map(int, input().split())
  SB += B
  AB[i] = A + B
AB.sort(reverse=True)
SAB = 0
for i in range(N):
  if i % 2 == 0:
    SAB += AB[i]
print(SAB-SB)

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])
l1 = sorted(l)[::-1]
bit = 0
ans = 0
for i,j,k in l1:
  if bit==0:
    ans += j
    bit = 1
  else:
    ans -= k
    bit = 0
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    mat = []
    for _ in range(n):
        a, b = map(int, readline().split())
        c = a + b
        mat.append([c, a, b])

    mat.sort(reverse=True)

    a_sum, b_sum = 0, 0

    for i in range(n):
        if i % 2 == 0:
            a_sum += mat[i][1]
        else:
            b_sum += mat[i][2]

    print(a_sum - b_sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    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


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
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 make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
ab = []
for i in range(n):
    a,b = lr()
    ab.append([a,b])
ab.sort(key=lambda x:x[0]+x[1], reverse=True)
ans = 0
for i,num in enumerate(ab):
    if i%2 == 0:
        ans+=num[0]
    else:
        ans-=num[1]
print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8

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


SUM.sort(reverse=True)

sumA = 0
sumB = 0
for i in range(N):
    idx = SUM[i][1]
    if i % 2 == 0:
        sumA += A[idx]
    else:
        sumB += B[idx]

print(sumA - sumB)

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  cuisine =  []
  for i in range(N):
    a,b = map(int, input().split())
    cuisine.append([a+b, a, b])
  cuisine.sort(key = lambda x:x[0], reverse = True)
  happy_t = 0
  happy_a = 0
  for i in range(N):
    if i % 2 == 0:
      happy_t += cuisine[i][1]
    else:
      happy_a += cuisine[i][2]
  print(happy_t-happy_a)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = []
for i in range(n):
    A, B = MAP()
    a.append([A+B, A, B])

a.sort(reverse=True)
tmp_a = 0
tmp_b = 0

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

ans = tmp_a - tmp_b
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    input_list = [list(map(int, input().split())) for _ in range(N)]
    dish_list = [(sum(row), row[0], row[1], n) for n, row in enumerate(input_list)]
    dish_list.sort(reverse=True)
    ans = sum([((- 1)**(n % 2)) * dish[1 + n % 2] for n, dish in enumerate(dish_list)])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve():
  ans = 0
  N = int(input())
  A = []
  for i in range(N):
    x,y = map(int, input().split())
    A.append([x+y,x,y])
  A.sort(reverse=True)
  for i in range(N):
    if i%2==0:
      ans += A[i][1]
    else:
      ans -= A[i][2]
  return ans
print(solve())

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
A,B = np.array([input().split() for _ in range(n)], dtype=np.int64).T

ans = A.sum() #全部先手が取る
improve = A+B #後手がとると改善する量
improve.sort()
ans -= improve[::-1][1::2].sum()
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:sum(x))

x = y = 0
i = 0
while AB:
    a,b = AB.pop()
    if i%2:
        y += b
    else:
        x += a
    i += 1
print(x-y)

Submission status: Accepted

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

c = [i+j for i, j in AB]
c.sort(reverse = True)

ans = sum(c[::2]) - sum([j for i, j in AB])
print(ans)

Submission status: Accepted

Solution code:
def solve(n, a, b):
    v = sorted(zip(a, b), key=lambda x:x[0]+x[1], reverse=True)
    return sum(x+y for x, y in v[::2]) - sum(b)

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

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
ab = list(map(lambda x: [x[0], x[1], x[0]+x[1]], ab))
ab.sort(key=lambda x:x[2], reverse=True)
ab=deque(ab)
count = 0
ans=0
while len(ab)>0:
  a, b, dif = ab.popleft()
  if count%2==0:
    ans+=a
  else:
    ans-=b
  count+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(n):
  a, b=ab[i]
  ab[i].append(a+b)
ab.sort(key=lambda x:x[2], reverse=True)
ab=deque(ab)
count = 0
ans=0
while len(ab)>0:
  a, b, dif = ab.popleft()
  if count%2==0:
    ans+=a
  else:
    ans-=b
  count+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

suml.sort(reverse=True)
q = deque(suml)

asum = 0
bsum = 0
a_turn = True
while q:
    if a_turn:
        _, poped, _ = q.popleft()
        asum += poped
    else:
        _, _, poped = q.popleft()
        bsum += poped
    a_turn = not a_turn

print(asum-bsum)

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)

C.sort(reverse=True)
print(sum(C[::2])-sum(B))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

dish = []
ans = 0
for _ in range(n):
    a, b = map(int, input().split())
    dish.append(a + b)
    ans += -1 * b

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

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, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

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

for i in range(N):
	AB[i].append(sum(AB[i]))

AB.sort(key = lambda x: -x[2])

print(sum([AB[i][0] for i in range(0, N, 2)]) - sum([AB[i][1] for i in range(1, N, 2)]))

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.append(b)

AB = sorted([s+t for s,t in zip(A,B)], reverse=True)
su = sum(AB[::2])
print(su-sum(B))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
lis = []

A = []
B = []
for i in range(N):
    Ain,Bin = map(int,input().split())
    A.append(Ain)
    B.append(Bin)
    lis.append((Ain+Bin,i))
lis.sort()
lis.reverse()
lis = deque(lis)

takacnt = 0
aocnt = 0

# print(taka)
# print(ao)

for i in range(N):
    if i % 2 == 0:
        val = lis.popleft()
        takacnt += A[val[1]]
    else:
        val = lis.popleft()
        aocnt += B[val[1]]
print(takacnt-aocnt)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
lis = []

A = []
B = []
for i in range(N):
    Ain,Bin = map(int,input().split())
    A.append(Ain)
    B.append(Bin)
    lis.append((Ain+Bin,i))
lis.sort()
lis.reverse()
lis = deque(lis)

takacnt = 0
aocnt = 0
st = set()

# print(taka)
# print(ao)

for i in range(N):
    if i % 2 == 0:
        while True:
            val = lis.popleft()
            if not val[1] in st:
                break
        st.add(val[1])
        takacnt += A[val[1]]
    else:
        while True:
            val = lis.popleft()
            if not val[1] in st:
                break
        st.add(val[1])
        aocnt += B[val[1]]
print(takacnt-aocnt)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
taka = []
ao = []
A = []
B = []
for i in range(N):
    Ain,Bin = map(int,input().split())
    A.append(Ain)
    B.append(Bin)
    taka.append((Bin+Ain,i))
    ao.append((Ain+Bin,i))
taka.sort()
taka.reverse()
ao.sort()
ao.reverse()
taka = deque(taka)
ao = deque(ao)
takacnt = 0
aocnt = 0
st = set()

# print(taka)
# print(ao)

for i in range(N):
    if i % 2 == 0:
        while True:
            val = taka.popleft()
            if not val[1] in st:
                break
        st.add(val[1])
        takacnt += A[val[1]]
    else:
        while True:
            val = ao.popleft()
            if not val[1] in st:
                break
        st.add(val[1])
        aocnt += B[val[1]]
print(takacnt-aocnt)