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: Wrong Answer

Solution code:
import heapq

n = int(input())
l = [list(map(int,input().split())) for i in range(n)]
a = []
b = []
used = [0]*n

for i in range(n):
    x,y = l[i]
    heapq.heappush(a,[-x,-y,i])
    heapq.heappush(b,[-y,-x,i])

ans = 0
for i in range(n):
    
    ax,ay,aind = a[0]
    while used[aind]:
        heapq.heappop(a)
        ax,ay,aind = a[0]
    bx,by,bind = b[0]
    while used[bind]:
        heapq.heappop(b)
        bx,by,bind = b[0]
        
    if i%2==0:
        if ax <= bx:
            ans -= ax
            used[aind] = 1
        else:
            if ax-bx <= -by:
                used[bind] = 1
                ans -= by
            else:
                used[aind] = 1
                ans -= ax
    else:
        if bx <= ax:
            ans += bx
            used[bind] = 1
        else:
            if bx -ax <= ay:
                used[aind] = 1
                ans += ay
            else:
                used[bind] = 1
                ans += bx            
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

n = int(input())
l = [list(map(int,input().split())) for i in range(n)]
a = []
b = []
used = [0]*n

for i in range(n):
    x,y = l[i]
    heapq.heappush(a,[-x,-y,i])
    heapq.heappush(b,[-y,-x,i])

ans = 0
for i in range(n):
    if i%2 == 0:
        x,y,ind = heapq.heappop(a)
        while used[ind]:
            x,y,ind = heapq.heappop(a)
        ans -= x
        used[ind] = 1
    else:
        x,y,ind = heapq.heappop(b)
        while used[ind]:
            x,y,ind = heapq.heappop(b)
        ans += x
        used[ind] = 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from collections import deque


def main():
    input = stdin.buffer.readline
    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n)]
    for i in range(n):
        ab[i].append(i)
    t_arr = sorted(ab, reverse=True, key=lambda x: (x[0], x[1]))
    a_arr = sorted(ab, reverse=True, key=lambda x: (x[1], x[0]))
    t_q = deque(t_arr)
    a_q = deque(a_arr)
    seen = [False] * n
    ans = 0
    count = 0
    while True:
        if count == n:
            break
        while True:
            ti = t_q.popleft()
            if not seen[ti[2]]:
                ans += ti[0]
                seen[ti[2]] = True
                count += 1
                break
        if count == n:
            break
        while True:
            ai = a_q.popleft()
            if not seen[ai[2]]:
                ans -= ai[1]
                seen[ai[2]] = True
                count += 1
                break
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
ab=[list(map(int, input().split())) for _ in range(n)]
yusendo=[]
for i in range(n):
    yusendo.append([ab[i][1]-ab[i][0], ab[i][0]+ab[i][1], i])
yusendo.sort(reverse=True)
# print(yusendo)

takahashi=0
aoki=0
for i in range(n):
    if i%2==0:
        takahashi+=ab[yusendo[i][2]][0]
    else:
        aoki+=ab[yusendo[i][2]][1]
# print(takahashi, aoki)

print(takahashi-aoki)

Submission status: Wrong Answer

Solution code:
n=int(input())
ab=[list(map(int, input().split())) for _ in range(n)]
yusendo=[]
for i in range(n):
    yusendo.append([ab[i][1]-ab[i][0], ab[i][0], i])
yusendo.sort(reverse=True)
# print(yusendo)

takahashi=0
aoki=0
for i in range(n):
    if i%2==0:
        takahashi+=ab[yusendo[i][2]][0]
    else:
        aoki+=ab[yusendo[i][2]][1]
# print(takahashi, aoki)

print(takahashi-aoki)

Submission status: Wrong Answer

Solution code:
n=int(input())
ab=[list(map(int, input().split())) for _ in range(n)]
yusendo=[]
for i in range(n):
    yusendo.append([ab[i][1]-ab[i][0], i])
# print(yusendo)
yusendo.sort(reverse=True)

total=0
takahashi=0
aoki=0
for i in range(n):
    if i%2==0:
        takahashi+=ab[yusendo[i][1]][0]
    else:
        aoki+=ab[yusendo[i][1]][1]
# print(takahashi, aoki)
print(takahashi-aoki)

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
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

#####MakeDivisors######
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

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
D = []
for _ in range(N):
  a,b = IL()
  D.append((a,b))
D = sorted(D,key = lambda x:(max(x),min(x)),reverse = True)

A = 0
B = 0
for i in range(N):
  a,b = D[i]
  if i%2:
    B+=b
  else:
    A+=a

print(A-B)

Submission status: Wrong Answer

Solution code:
exit()

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = [None]*n
b = [None]*n
for i in range(n):
    a[i],b[i] = map(int, input().split())
    
ab = [(a[i]+b[i], i) for i in range(n)]
ab.sort()
takahashi = 0
aoki = 0
for i in range(n):
    if i%2==0:
        takahashi += a[ab[-i][1]]
    else:
        aoki += b[ab[-i][1]]
print(takahashi-aoki)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = [None]*n
b = [None]*n
for i in range(n):
    a[i],b[i] = map(int, input().split())
    
ab = [(a[i]+b[i], i) for i in range(n)]
ab.sort()
takahashi = 0
aoki = 0
for i in range(n):
    if i%2==0:
        takahashi += a[ab[i][1]]
    else:
        aoki += b[ab[i][1]]
print(takahashi-aoki)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())

dab = []

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

dab.sort()
dab = np.array(dab)

a = dab[:, 1][0::2]
b = dab[:, 2][1::2]

print(a.sum() - b.sum())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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[t_cnt] == 0:
            ans += x[t_cnt][1]
            takahashi = False
            cnt += 1
            used[t_cnt] = 1
        t_cnt += 1
    elif (not takahashi) and a_cnt < n:
        if used[a_cnt] == 0:
            ans -= y[a_cnt][1]
            takahashi = True
            cnt += 1
            used[a_cnt] = 1
        a_cnt += 1
    # print(ans)
print(ans)

Submission status: Wrong Answer

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[t_cnt] == 0:
            ans += x[t_cnt][1]
            takahashi = False
            cnt += 1
        t_cnt += 1
    elif (not takahashi) and a_cnt < n:
        if used[a_cnt] == 0:
            ans -= y[a_cnt][1]
            takahashi = True
            cnt += 1
        a_cnt += 1
    # print(ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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],B[i]-A[i])

t=0
a=0

for i in range(N):
  if i%2==0: #高橋くんの番
    C.sort(key= lambda x: x[3],reverse=True)
    C.sort(key= lambda x: x[0],reverse=True)
    #print(i,C)
    t += C.pop(0)[0]
  #if i%2==1: #青木さんの番
  #  C.sort(key= lambda x: x[2],reverse=True)
  #  C.sort(key= lambda x: x[1],reverse=True)
    #print(i,C)
  #  a += C.pop(0)[1]
  #print(i,C)
print(t-a)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_max, b_max = [], []
used = [False] * (n + 1)
used[n] = True
for i in range(n):
    a, b = map(int, input().split())
    a_max.append((a, b, i))
    b_max.append((a, b, i))

a_max.sort(key=lambda x: x[1])
a_max.sort()
b_max.sort()
b_max.sort(key=lambda x: x[1])

flag = True
ans = 0
i, j = n, n
for _ in range(n):
    while used[i]:
        a_i, b_i, i = a_max.pop()
    while used[j]:
        a_j, b_j, j = b_max.pop()
    if abs(a_i - a_j) > abs(b_i - b_j):
        if flag:
            ans += a_i
        else:
            ans -= b_i
        used[i] = True
    else:
        if flag:
            ans += a_j
        else:
            ans -= b_j
        used[j] = True
    flag = not flag
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

n = int(input())
a_max, b_max = [], []
used = [False] * (n + 1)
used[n] = True
for i in range(n):
    a, b = map(int, input().split())
    a_max.append((a, b, i))
    b_max.append((a, b, i))

a_max.sort(key=lambda x: x[1])
a_max.sort()
b_max.sort()
b_max.sort(key=lambda x: x[1])
a_max, b_max = collections.deque(a_max), collections.deque(b_max)

flag = True
ans = 0
i, j = n, n
for _ in range(n):
    while used[i]:
        a_i, b_i, i = a_max.popleft()
    while used[j]:
        a_j, b_j, j = b_max.popleft()
    if abs(a_i - a_j) > abs(b_i - b_j):
        if flag:
            ans += a_i
        else:
            ans -= b_i
        used[i] = True
    else:
        if flag:
            ans += a_j
        else:
            ans -= b_j
        used[j] = True
    flag = not flag
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

n = int(input())
a_max, b_max = [], []
used = [False] * (n + 1)
used[n] = True
for i in range(n):
    a, b = map(int, input().split())
    a_max.append((a, b, i))
    b_max.append((a, b, i))

a_max.sort(key=lambda x: x[1])
a_max.sort()
b_max.sort()
b_max.sort(key=lambda x: x[1])
a_max, b_max = collections.deque(a_max), collections.deque(b_max)

flag = True
ans = 0
for _ in range(n):
    i, j = n, n
    while used[i]:
        a_i, b_i, i = a_max.popleft()
    while used[j]:
        a_j, b_j, j = b_max.popleft()
    if abs(a_i - a_j) > abs(b_i - b_j):
        if flag:
            ans += a_i
        else:
            ans -= b_i
        used[i] = True
        b_max.appendleft((a_j, b_j, j))
    else:
        if flag:
            ans += a_j
        else:
            ans -= b_j
        used[j] = True
        a_max.appendleft((a_i, b_i, i))
    flag = not flag
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

n = int(input())
a, b = [], []
used = [False] * (n + 1)
used[n] = True
for i in range(n):
    a_i, b_i = map(int, input().split())
    heapq.heappush(a, (-a_i, -b_i, i))
    heapq.heappush(b, (-b_i, -a_i, i))

flag = True
ans = 0
for _ in range(n):
    i, j = n, n
    while used[i]:
        a_i, b_i, i = heapq.heappop(a)
    while used[j]:
        b_j, a_j, j = heapq.heappop(b)
    if abs(a_i - a_j) > abs(b_i - b_j):
        if flag:
            ans -= a_i
        else:
            ans += b_i
        used[i] = True
        heapq.heappush(b, (b_j, a_j, j))
    else:
        if flag:
            ans -= a_j
        else:
            ans += b_j
        used[j] = True
        heapq.heappush(a, (a_i, b_i, i))
    flag = not flag
print(ans)

Submission status: Wrong Answer

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

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

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

    ans = pt_tak - pt_aok
    print(ans)

nikkei19q_c()

Submission status: Wrong Answer

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

    from operator import itemgetter
    prior_tak = iter(sorted(dish, key=itemgetter(2,1), reverse=True))
    prior_aok = iter(sorted(dish, key=itemgetter(1,2), reverse=True))
    exist = [True] * N
    pt_tak, pt_aok = 0, 0

    for i in range(N):
        if i%2 == 0:
            while True:
                idx, a, b = next(prior_tak)
                if exist[idx]: break
            pt_tak += a
            exist[idx] = False
        else:
            while True:
                idx, a, b = next(prior_aok)
                if exist[idx]: break
            pt_aok += b
            exist[idx] = False

    ans = pt_tak - pt_aok
    print(ans)

nikkei19q_c()

Submission status: Wrong Answer

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