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:
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 C:
        if i%2 == 0: output += A[i]
        else: output -= B[i]
    print(output)

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 == 0:
        ans += A[index[i]]
    else:
        ans -= B[index[i]]

print(ans)

Submission status: Wrong Answer

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,sortedC = index_sort(C)
ans = 0
for i in range(N):
    if i%2 == 0:
        ans += A[index[i]]
    else:
        ans -= B[index[i]]

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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)
    print(sum([a[1] for i,a in enumerate(AB) if i%2==0])-sum([b[2] for i,b in enumerate(AB) if i%2==1]))


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    AB=[tuple(map(int,input().split())) for i in range(n)]
    AB.sort(reverse=True)
    ans=0
    for i ,ab in enumerate(AB):
        if i%2==0:
            ans+=ab[0]
        else:
            ans-=ab[1]
    print(ans)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

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])
l.sort(key = lambda x:x[0]-x[1])
ans=0
for i in range(n):
  if i%2==0:
    ans+=l[i//2][0]
  else:
    ans-=l[n-1-(i//2)][1]
print(ans)

Submission status: Wrong Answer

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

print(taka-ao)

Submission status: Wrong Answer

Solution code:
n = int(input())
point = []
for _ in range(n):
    a, b = map(int, input().split())
    point.append((a - b, a, b))
point.sort()
point = list(reversed(point))

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

Submission status: Wrong Answer

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=0
ao=0
for i in range(n):
    if i%2==0:
        tk+=ab[i][0]
    else:
        ao+=ab[i][1]
print(tk-ao)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
n=int(input())
a,b=[],[]
for i in range(n):
    a_,b_=map(int,input().split())
    a.append((a_,b_,i))
    b.append((b_,a_,i))
a.sort(key=itemgetter(1))
a.sort(key=itemgetter(0),reverse=True)
b.sort(key=itemgetter(1))
b.sort(key=itemgetter(0),reverse=True)
eated=set()
tak=0
ao=0
i,j=0,0
#print(a)
#print(b)
while 1:
    while a[i][2] in eated:
        i+=1
        if i==n:
            break
    while b[j][2] in eated:
        j+=1
        if j==n:
            break
    if i==n or j==n:
        break
    if a[i][0]-b[j][0]>b[j][1]-a[i][1]:
        tak+=a[i][0]
        eated.add(a[i][2])
    elif a[i][0]-b[j][0]==b[j][1]-a[i][1]:
        if a[i][0]>=b[j][1]:
            tak+=a[i][0]
            eated.add(a[i][2])
        else:
            tak+=b[j][1]
            eated.add(b[j][2])
    else:
        tak+=b[j][1]
        eated.add(b[j][2])
    while a[i][2] in eated:
        i+=1
        if i==n:
            break
    while b[j][2] in eated:
        j+=1
        if j==n:
            break
    if i==n or j==n:
        break
    if b[j][0]-a[i][0]>a[i][1]-b[j][1]:
        ao+=b[j][0]
        eated.add(b[j][2])
    elif b[j][0]-a[i][0]==a[i][1]-b[j][1]:
        if b[j][0]>=a[i][1]:
            ao+=b[j][0]
            eated.add(b[j][2])
        else:
            ao+=a[i][1]
            eated.add(a[i][2])
    else:
        ao+=a[i][1]
        eated.add(a[i][2])
print(tak-ao)
#print(tak)
#print(ao)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Solution code:
from collections import deque
from collections import OrderedDict

def main():
    N, *AB = map(int, open(0).read().split())
    C = OrderedDict()
    for i in range(N):
        a = AB[i * 2 + 0]
        b = AB[i * 2 + 1]
        d = a - b
        if d not in C:
            C[d] = []
        C[d].append((a, b))
    for c in C:
        C[c] = deque(sorted(C[c], reverse=True))
    # print(C)
    f, s = 0, 0
    hand = True
    for c in C:
        v = C[c]
        while len(v) != 0:
            if hand:
                h = v.popleft()
                f += h[0]
                # print('f,{},{}'.format(f, h))
                hand = False
            else:
                h = v.popleft()
                s += h[1]
                # print('s,{},{}'.format(s, h))
                hand = True
    print(f - s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
ABd=[]
for i in range(N):
    a,b=map(int,input().split())
    ABd.append([a,b,a-b]);
ABd.sort(key=lambda x:x[2])
res=0
for i in reversed(range(N)):
    if((N-1-i)%2==0):
        res+=ABd[i][0]
    else:
        res-=ABd[i][1]
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
dishes = [list(map(int, input().split())) + [0] for _ in range(N)]
dishes.sort()
cnt = 0
dT = [dishes[i][0] * N + i for i in range(N)]
dA = [dishes[i][1] * N + i for i in range(N)]
dT.sort(reverse=True)
dA.sort(reverse=True)
T, A = 0, 0
t, a = 0, 0
for i in range(N):
  if i % 2 == 0:
    while dishes[dT[t]%N][2] == 1:
      t += 1
    T += dishes[dT[t]%N][0]
    dishes[dT[t]%N][2] = 1
  else:
    while dishes[dA[a]%N][2] == 1:
      a += 1
    A += dishes[dA[a]%N][1]
    dishes[dA[a]%N][2] = 1
print(T - A)

Submission status: Wrong Answer

Solution code:
N = int(input())
dishes = [list(map(int, input().split())) + [0] for _ in range(N)]
dishes.sort()
cnt = 0
dT = [dishes[i][0] * N + i for i in range(N)]
dA = [dishes[i][1] * N + i for i in range(N)]
dT.sort(reverse=True)
dA.sort(reverse=True)
print(dishes)
T, A = 0, 0
t, a = 0, 0
for i in range(N):
  if i % 2 == 0:
    while dishes[dT[t]%N][2] == 1:
      t += 1
    T += dishes[dT[t]%N][0]
    dishes[dT[t]%N][2] = 1
  else:
    while dishes[dA[a]%N][2] == 1:
      a += 1
    A += dishes[dA[a]%N][1]
    dishes[dA[a]%N][2] = 1
print(T - A)

Submission status: Wrong Answer

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])
ans=0
for i in range(n):
  if i%2==0:
    ans+=l[i//2][0]
  else:
    ans-=l[n-1-(i//2)][1]
print(ans)

Submission status: Wrong Answer

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

t_point=0
a_point=0

ab_t=copy.deepcopy(ab)
ab_t.sort(key=lambda x:(x[0],x[1]),reverse=True)

ab_a=copy.deepcopy(ab)
ab_a.sort(key=lambda x:(x[1],x[0]),reverse=True)

checked_i=set()

ti=0
ai=0
while(len(checked_i)<n):
#    print(t_point,a_point,ti,ai,checked_i)
    while(ab_t[ti][2] in checked_i):
        ti+=1
    t_point+=ab_t[ti][0]
    checked_i.add(ab_t[ti][2])

    if(len(checked_i)>=n):
        break

#    print(t_point,a_point,ti,ai,checked_i)
    while(ab_a[ai][2] in checked_i):
        ai+=1
    a_point+=ab_a[ai][1]
    checked_i.add(ab_a[ai][2])

#print(t_point,a_point,ti,ai,checked_i)
print(t_point-a_point)

Submission status: Wrong Answer

Solution code:
n = int(input())

D = []

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

D.sort(reverse=True)

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

print(ans)

Submission status: Wrong Answer

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

a_b=[]
for a,b in ab:
    a_b.append((a-b,a,b))
a_b=sorted(a_b)
#print(a_b)
chokudai=0
aoki=0
for i in range(n):
    if i%2==0:
        chokudai+=a_b[i//2][1]
    else:
        aoki+=a_b[-(i+1)//2][2]
print(chokudai-aoki)

Submission status: Wrong Answer

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

a_b=[]
for a,b in ab:
    a_b.append((a-b,a,b))
a_b=sorted(a_b)
#print(a_b)
chokudai=0
aoki=0
for i in range(n):
    if i%2==0:
        chokudai+=a_b[i][1]
    else:
        aoki+=a_b[i][2]
print(chokudai-aoki)

Submission status: Wrong Answer

Solution code:
n = int(input())

AB = [[0, 0, 0] for i in range(n)]
T = []
A = []
for i in range(n):
    a, b = map(int, input().split())
    AB[i][0] = a
    AB[i][1] = b
    AB[i][2] = i

AB.sort()
for i in range(n):
    b = AB[i][1]
    id = AB[i][2]
    T.append((b, id))
T.sort()

AB.sort(key=lambda x: x[1])
for i in range(n):
    a = AB[i][0]
    id = AB[i][2]
    A.append((a, id))
A.sort()

#print(T)
#print(A)

used = set()
ans = 0
for i in range(n):
    if i%2 == 0:
        while T:
            b, idx = T.pop()
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans += AB[idx][0]
    else:
        while A:
            a, idx = A.pop()
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans -= AB[idx][1]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

AB = [[0, 0, 0] for i in range(n)]
T = []
A = []
for i in range(n):
    a, b = map(int, input().split())
    AB[i][0] = a
    AB[i][1] = b
    AB[i][2] = i

AB.sort(reverse=True)
for i in range(n):
    b = AB[i][1]
    id = AB[i][2]
    T.append((b, id))
T.sort()

AB.sort(key=lambda x: x[1], reverse=True)
for i in range(n):
    a = AB[i][0]
    id = AB[i][2]
    A.append((a, id))
A.sort()

#print(T)
#print(A)

used = set()
ans = 0
for i in range(n):
    if i%2 == 0:
        while T:
            b, idx = T.pop()
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans += AB[idx][0]
    else:
        while A:
            a, idx = A.pop()
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans -= AB[idx][1]
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    a,b = map(int,input().split())
    if i%2 == 0:res.append(a+b)
    ans-=b

res.sort(reverse = True)
for i in range((n+1)//2):
    ans+=res[i]
print(ans)

Submission status: Wrong Answer

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

l = sorted([[a + b, a, b] for a, b in ab])
res = 0
for idx, i in enumerate(l):
    if idx % 2 == 0:
        res += i[1]
    else:
        res -= i[2]
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())

AB = [[0, 0, 0] for i in range(n)]
T = []
A = []
for i in range(n):
    a, b = map(int, input().split())
    AB[i][0] = a
    AB[i][1] = b
    AB[i][2] = i

AB.sort(reverse=True)
for i in range(n):
    b = AB[i][1]
    id = AB[i][2]
    T.append((b, id))

AB.sort(key=lambda x: x[1], reverse=True)
for i in range(n):
    a = AB[i][0]
    id = AB[i][2]
    A.append((a, id))

#print(T)
#print(A)

T.reverse()
A.reverse()

used = set()
ans = 0
for i in range(n):
    if i%2 == 0:
        while T:
            b, idx = T.pop()
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans += AB[idx][0]
    else:
        while A:
            a, idx = A.pop()
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans -= AB[idx][1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    T, A = [None] * N, [None] * N
    used = [False] * N
    for i in range(N):
        a, b = map(int, input().split())
        T[i] = (b - a, a, b, i)
        A[i] = (a - b, b, a, i)
    T.sort(reverse = True)
    A.sort(reverse = True)
    hapiness = 0
    ti, ai = 0, 0
    for i in range(N):
        if i % 2 == 0:
            while used[T[ti][3]]: ti += 1
            hapiness += T[ti][1]
            used[T[ti][3]] = True
            ti += 1           
        else:
            while used[A[ai][3]]: ai += 1
            hapiness -= A[ai][1]
            used[A[ai][3]] = True
            ai += 1
    print(hapiness)
   
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    C = [(ab[0], i) for i, ab in enumerate(AB)]
    D = [(ab[1], i) for i, ab in enumerate(AB)]
    seen = [False]*N
    C.sort()
    D.sort()
    ans = 0
    for i in range(N):
        if i % 2 == 0:
            # Takahashi
            cur = C.pop()[1]
            while C and seen[cur]:
                cur = C.pop()[1]
            seen[cur] = True
            ans += AB[cur][0]
        else:
            cur = D.pop()[1]
            while D and seen[cur]:
                cur = D.pop()[1]
            seen[cur] = True
            ans -= AB[cur][1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

AB = [[0, 0, 0] for i in range(n)]
T = []
A = []
for i in range(n):
    a, b = map(int, input().split())
    AB[i][0] = a
    AB[i][1] = b
    AB[i][2] = i

AB.sort(reverse=True)
for i in range(n):
    b = AB[i][1]
    id = AB[i][2]
    T.append(((-1)*b, id))

AB.sort(key=lambda x: x[1], reverse=True)
for i in range(n):
    a = AB[i][0]
    id = AB[i][2]
    A.append(((-1)*a, id))

import heapq
heapq.heapify(T)
heapq.heapify(A)
used = set()
ans = 0
for i in range(n):
    if i%2 == 0:
        while T:
            b, idx = heapq.heappop(T)
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans += AB[idx][0]
    else:
        while A:
            a, idx = heapq.heappop(T)
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans -= AB[idx][1]
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    C = [(ab[0], i) for i, ab in enumerate(AB)]
    D = [(ab[1], i) for i, ab in enumerate(AB)]
    seen = [False]*N
    C.sort()
    D.sort()
    ans = 0
    for i in range(N):
        if i % 2 == 0:
            # Takahashi
            cur = D.pop()[1]
            while D and seen[cur]:
                cur = D.pop()[1]
            seen[cur] = True
            ans += AB[cur][0]
        else:
            cur = C.pop()[1]
            while C and seen[cur]:
                cur = C.pop()[1]
            seen[cur] = True
            ans -= AB[cur][1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    dish = [None] * N
    for i in range(N):
        a, b = map(int, input().split())
        dish[i] = (b - a, a, b)
    dish.sort()
    hapiness = 0
    for i in range(N):
        if i % 2 == 0: hapiness += dish[N - i - 1][1]
        else: hapiness -= dish[N - i - 1][2]
    print(hapiness)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
AB = []
for i in range(n):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort(key=lambda x: (max(x), min(x)), reverse=True)
A, B = [], []
for i in range(n):
    a, b = AB[i]
    if i % 2 == 0:
        A.append(a)
    else:
        B.append(b)
print(sum(A)-sum(B))

Submission status: Wrong Answer

Solution code:
n = int(input())
AB = []
for i in range(n):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort(key=lambda x: (max(x), min(x)), reverse=True)
print(AB)
A, B = [], []
for i in range(n):
    a, b = AB[i]
    if i % 2 == 0:
        A.append(a)
    else:
        B.append(b)
print(sum(A)-sum(B))

Submission status: Wrong Answer

Solution code:
n = int(input())

AB = [[0, 0] for i in range(n)]
T = []
A = []
for i in range(n):
    a, b = map(int, input().split())
    AB[i][0] = a
    AB[i][1] = b
    T.append(((-1)*b, i))
    A.append(((-1)*a, i))

import heapq
heapq.heapify(T)
heapq.heapify(A)
used = set()
ans = 0
for i in range(n):
    if i%2 == 0:
        while T:
            b, idx = heapq.heappop(T)
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans += AB[idx][0]
    else:
        while A:
            a, idx = heapq.heappop(T)
            if idx in used:
                continue
            else:
                break
        used.add(idx)
        ans -= AB[idx][1]
print(ans)

Submission status: Wrong Answer

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

A = [(a, b, i) for i, (a, b) in enumerate(AB)]
B = [(a, b, i) for i, (a, b) in enumerate(AB)]

A.sort(key=lambda a: a[0] - a[1])
B.sort(key=lambda a: a[1] - a[0])

ans = 0
V = set()
for i in range(N):
    if i % 2 == 0:
        while A and A[-1][1] in V:
            A.pop()
        a, _, k = A.pop()
        ans += a
        V.add(k)
    else:
        while B and B[-1][1] in V:
            B.pop()
        _, b, k = B.pop()
        ans -= b
        V.add(k)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x:x[1]+x[0])

ans = 0
for i,(a,b) in enumerate(AB):
    if i%2:
        ans -= b
    else:
        ans += a
print(ans)

Submission status: Wrong Answer

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

A = [(a, i) for i, (a, _) in enumerate(AB)]
B = [(b, i) for i, (_, b) in enumerate(AB)]

A.sort()
B.sort()

ans = 0
V = set()
for i in range(N):
    if i % 2 == 0:
        while A and A[-1][1] in V:
            A.pop()
        a, k = A.pop()
        ans += a
        V.add(k)
    else:
        while B and B[-1][1] in V:
            B.pop()
        b, k = B.pop()
        ans -= b
        V.add(k)
print(ans)

Submission status: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x:x[1]-x[0])

ans = 0
for i,(a,b) in enumerate(AB):
    if i%2:
        ans -= b
    else:
        ans += a
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# どうせ何を喰っても自分のポイントは入るので、
# 高橋くんは青木くんの得られるポイントの最大のやつを,青木くんは高橋くんのvice versa
# 同じポイントならその中でも自分が得られるものが多いほうがいい
n = int(input())
arr = []
used = [False for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    arr.append([i, a, b])

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

c = 0
taki, aoki = (0, 0)
while c < n:
    if c % 2 == 0:
        # # 高橋くんのターン
        while aoki < n and used[aok[aoki][0]]:
            aoki += 1
        used[aok[aoki][0]] = True
        takp += aok[aoki][
            1]  # 青木くんの得られるポイントで降順ソートしたインデックスの料理を食べた時に得られる高橋くんのポイント
        aoki + 1
    else:
        # 青木くんのターン
        while taki < n and used[tak[taki][0]]:
            taki += 1
        used[tak[taki][0]] = True
        aokp += tak[taki][2]  # 略
        taki += 1
    c += 1
print(takp - aokp)
if not all(used):
    print(0 / 0)

Submission status: Wrong Answer

Solution code:
# どうせ何を喰っても自分のポイントは入るので、
# 高橋くんは青木くんの得られるポイントの最大のやつを,青木くんは高橋くんのvice versa
# 同じポイントならその中でも自分が得られるものが多いほうがいい
n = int(input())
arr = []
used = [False for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    arr.append([i, a, b])

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

c = 0
taki, aoki = (0, 0)
while c < n:
    if c % 2 == 0:
        # # 高橋くんのターン
        while aoki < n and used[aok[aoki][0]]:
            aoki += 1
        used[aok[aoki][0]] = True
        takp += aok[aoki][
            1]  # 青木くんの得られるポイントで降順ソートしたインデックスの料理を食べた時に得られる高橋くんのポイント
        aoki + 1
    else:
        # 青木くんのターン
        while taki < n and used[tak[taki][0]]:
            taki += 1
        used[tak[taki][0]] = True
        aokp += tak[taki][2]  # 略
        taki += 1
    c += 1
print(takp - aokp)

Submission status: Wrong Answer

Solution code:
N = int(input())
ABlist = sorted([a+b for a, b in [map(int, input().split()) for _ in range(N)]])[::-1]
ans = 0
for i in range(N):
    if i % 2 == 0:
        ans += ABlist[i]
    else:
        ans -= ABlist[i]
print(ans // 2)

Submission status: Wrong Answer

Solution code:
N = int(input())
A_finish = [False] * N
B_finish = [False] * N
As, Bs = [], []
for i in range(N):
    a,b = map(int, input().split())
    As.append((a,i))
    Bs.append((b,i))

cnt = 0
As.sort(reverse=True)
Bs.sort(reverse=True)

# print(As,Bs)
p_a = 0
p_b = 0

while cnt < N:
    # 高橋くんのターン
    while True:
        a,i = As.pop()
        if not B_finish[i]:
            p_a += a
            A_finish[i] = True
            cnt += 1
            break
    # 青木
    while True and cnt < N:
        b,i = Bs.pop()
        if not A_finish[i]:
            p_b += b
            B_finish[i] = True
            cnt += 1
            break
    # print(cnt,p_a,p_b)

print(p_a-p_b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[];b=[]
ref=[1]*n
for i in range(n):
    tmpa,tmpb=map(int,input().split())
    a.append([tmpa,tmpb-tmpa,i])
    b.append([tmpb,tmpa-tmpb,i])
a.sort(reverse=True)
b.sort(reverse=True)
ans=0
tmpa=0;tmpb=0

for i in range(n):
    if i%2==0:
        while ref[a[tmpa][2]]!=1:
            tmpa+=1
        ans+=a[tmpa][0]
        ref[a[tmpa][2]]="a"
    else:
        while ref[b[tmpb][2]]!=1:
            tmpb+=1
        ans-=b[tmpb][0]
        ref[b[tmpb][2]]="b"
print(ans)

Submission status: Wrong Answer

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