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:
n = int(input())

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

for i in range(n):
    if not i%2:
        ans += dish[i]
        
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())

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

eaten = [0]*n
takahashi.sort()
aoki.sort()

takahashi = deque(takahashi)
aoki = deque(aoki)

t_s = 0
a_s = 0
for i in range(n):
    if i%2 == 0:
        while True:
            b, a, i = aoki.popleft()
            if eaten[i] == 0:
                t_s += a
                eaten[i] = 1
                break
    else:
        while True:
            a, b, i = takahashi.popleft()
            if eaten[i] == 0:
                a_s += b
                eaten[i] = 1
                break
            
print(t_s-a_s)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A, B, C = [], [], []
    for _ in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)
        C.append(a+b)
    C.sort()
    ans = 0
    for i in range(N//2+N%2):
        ans += C[2*i]
    ans -= sum(B)
    print(ans)

if __name__ == "__main__":
    main()

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(reverse=True,key=lambda x:x[1])

tak = [x[0] for x in ab[::2]]
aok = [x[1] for x in ab[1::2]]
s = sum(tak)-sum(aok)

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

tak1 = [x[0] for x in ab[::2]]
aok1 = [x[1] for x in ab[1::2]]

s1 = sum(tak1)-sum(aok1)
print (min(s,s1))

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(reverse=True,key=lambda x:x[1])

tak = [x[0] for x in ab[::2]]
aok = [x[1] for x in ab[1::2]]
print (sum(tak)-sum(aok))

Submission status: Wrong Answer

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

print(sum(map(lambda x: x[0], AB[::2])) - sum(map(lambda x: x[1], AB[1::2])))

Submission status: Wrong Answer

Solution code:

def resolve():
    N = int(input())

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

    P.sort()
    taka = 0
    aoki = 0
    for i, (diff, idx) in enumerate(P):
        if i % 2 == 0:
            taka += A[idx]
        else:
            aoki += B[idx]

    print(taka - aoki)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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 = []
    used = set()
    for i in range(n):
        a, b = input_int_list()
        dishes.append((a, b, i))
    t_que = sorted(dishes, key=lambda x: (x[0]))
    a_que = sorted(dishes, key=lambda x: (x[1]))
    t_sum = 0
    a_sum = 0
    while t_que:
        # Tのターン
        while t_que:
            a, b, i = t_que.pop()
            if i not in used:
                t_sum += a
                used.add(i)
                break
        # Aのターン
        while a_que:
            a, b, i = a_que.pop()
            if i not in used:
                a_sum += b
                used.add(i)
                break
    print(t_sum - a_sum)

    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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 = []
    used = set()
    for i in range(n):
        a, b = input_int_list()
        dishes.append((a, b, i))
    t_que = sorted(dishes, key=lambda x: (x[0], x[1]))
    a_que = sorted(dishes, key=lambda x: (x[1], x[0]))
    t_sum = 0
    a_sum = 0
    while t_que:
        # Tのターン
        while t_que:
            a, b, i = t_que.pop()
            if i not in used:
                t_sum += a
                used.add(i)
                break
        # Aのターン
        while a_que:
            a, b, i = a_que.pop()
            if i not in used:
                a_sum += b
                used.add(i)
                break
    print(t_sum - a_sum)

    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Solution code:
# coding: utf-8
n = int(input())
l1=[]
l2=[]
Flg=[True]*n
F=[True]*n
l=[]
for i in range(n):
    a, b = map(int,input().split())
    l.append([a,b])
    l1.append([i, a, a-b])
    l2.append([i, b, b-a])
l1.sort(key=lambda x:x[2], reverse=True)
l1.sort(key=lambda x:x[1], reverse=True)
l2.sort(key=lambda x:x[2], reverse=True)
l2.sort(key=lambda x:x[1], reverse=True)
#print(l1)
#print(l2)
i1=0
i2=0
A=[]
cnt=0
flg=True#高橋君の番フラグ
while True:
    if flg:
        while True:
            if Flg[i1]:
                A.append(i1)
                flg=False
                Flg[i1]=False
                cnt+=1
                break
            else:
                i1+=1
    else:
        while True:
            if Flg[i2]:
                Flg[i2]=False
                cnt+=1
                flg=True
                break
            else:
                i2+=1
    if cnt==n:
        break
ans=0
#print(A)
A.sort()
for a in A:
    F[a]=False
for i in range(n):
    if F[i]:
        ans-=l[i][1]
    else:
        ans+=l[i][0]
print(ans)

Submission status: Wrong Answer

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

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for i in range(N)]
D = []
for a, b in AB:
    D.append((b - a, 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: Wrong Answer

Solution code:
# coding: utf-8
n = int(input())
l1=[]
l2=[]
Flg=[True]*n
F=[True]*n
l=[]
for i in range(n):
    a, b = map(int,input().split())
    l.append([a,b])
    l1.append([i, a-b])
    l2.append([i, b-a])
l1.sort(key=lambda x:x[1], reverse=True)
l2.sort(key=lambda x:x[1], reverse=True)
i1=0
i2=0
A=[]
cnt=0
flg=True#高橋君の番フラグ
while True:
    if flg:
        while True:
            if Flg[i1]:
                A.append(i1)
                flg=False
                Flg[i1]=False
                cnt+=1
                break
            else:
                i1+=1
    else:
        while True:
            if Flg[i2]:
                Flg[i2]=False
                cnt+=1
                flg=True
                break
            else:
                i2+=1
    if cnt==n:
        break
ans=0
#print(A)
A.sort()
for a in A:
    F[a]=False
for i in range(n):
    if F[i]:
        ans-=l[i][1]
    else:
        ans+=l[i][0]
print(ans)

Submission status: Wrong Answer

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

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

def main():
    n = int(input())
    
    a = [0]*n
    b = [0]*n
    for i in range(n):
        x, y = map(int, input().split())
        a[i] = (y-x, x)
        b[i] = (x-y, y)
    
    a.sort()
    b.sort()
        
    ans = 0
    for i in range(n):
        if i%2:
            ans -= b[i][1]
        else:
            ans += a[i][1]
    
    print(ans)
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a = [0]*n
    b = [0]*n
    for i in range(n):
        x, y = map(int, input().split())
        a[i] = (x, i)
        b[i] = (y, i)
    
    a.sort(reverse=True)
    b.sort(reverse=True)
    already = [False]*n
    ind_a, ind_b = 0, 0
    ans = 0
    for i in range(n):
        if i%2:
            while already[b[ind_b][1]]:
                ind_b += 1
            ans -= b[ind_b][0]
            already[b[ind_b][1]] = True
        else:
            while already[a[ind_a][1]]:
                ind_a += 1
            ans += a[ind_a][0]
            already[a[ind_a][1]] = True
    
    print(ans)
            
    
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a = [0]*n
    b = [0]*n
    for i in range(n):
        x, y = map(int, input().split())
        a[i] = (x, i)
        b[i] = (y, i)
    
    a.sort(reverse=True)
    b.sort(reverse=True)
    already = [False]*n
    ind_a, ind_b = 0, 0
    ans = 0
    for i in range(n):
        if i%2:
            while already[b[ind_b][1]]:
                ind_b += 1
            ans -= b[ind_b][0]
            already[b[ind_b][1]] = True
        else:
            while already[a[ind_a][1]]:
                ind_a += 1
            ans += a[ind_a][0]
            already[b[ind_a][1]] = True
    
    print(ans)
            
    
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a = [0]*n
    b = [0]*n
    for i in range(n):
        x, y = map(int, input().split())
        a[i] = (x, i)
        b[i] = (y, i)
    
    a.sort()
    b.sort()
    already = [False]*n
    ind_a, ind_b = 0, 0
    ans = 0
    for i in range(n):
        if i%2:
            while already[b[ind_b][1]]:
                ind_b += 1
            ans -= b[ind_b][0]
            already[b[ind_b][1]] = True
        else:
            while already[a[ind_a][1]]:
                ind_a += 1
            ans += a[ind_a][0]
            already[b[ind_a][1]] = True
    
    print(ans)
            
    
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import collections

from operator import itemgetter

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    AB = [[int(x) for x in input().split()] for _ in range(N)]

    takahashi = []
    aoki = []

    for i, (a, b) in enumerate(AB):
        takahashi.append((a, b, i))
        aoki.append((a, b, i))

    takahashi.sort(key=itemgetter(1), reverse=True)
    takahashi.sort(key=itemgetter(0), reverse=True)
    aoki.sort(key=itemgetter(0), reverse=True)
    aoki.sort(key=itemgetter(1), reverse=True)

    v = set()

    i = 0
    ti = 0
    ai = 0
    tp = 0
    ap = 0
    while i != N:
        while takahashi[ti][2] in v:
            ti += 1
        while aoki[ai][2] in v:
            ai += 1

        # takahashi turn
        if i % 2 == 0:
            if takahashi[ti][2] == aoki[ai][2]:
                tp += takahashi[ti][0]
                v.add(takahashi[ti][2])
            else:
                if takahashi[ti][0] == aoki[ai][1]:
                    if takahashi[ti][1] >= aoki[ai][0]:
                        tp += takahashi[ti][0]
                        v.add(takahashi[ti][2])
                    else:
                        tp += aoki[ai][0]
                        v.add(aoki[ai][2])
                elif takahashi[ti][0] > aoki[ai][1]:
                    tp += takahashi[ti][0]
                    v.add(takahashi[ti][2])
                else:
                    tp += aoki[ai][0]
                    v.add(aoki[ai][2])
        # aoki turn
        else:
            if takahashi[ti][2] == aoki[ai][2]:
                ap += takahashi[ti][1]
                v.add(takahashi[ti][2])
            else:
                if takahashi[ti][0] == aoki[ai][1]:
                    if takahashi[ti][1] >= aoki[ai][0]:
                        ap += takahashi[ti][1]
                        v.add(takahashi[ti][2])
                    else:
                        ap += aoki[ai][1]
                        v.add(aoki[ai][2])
                elif takahashi[ti][0] > aoki[ai][1]:
                    ap += takahashi[ti][1]
                    v.add(takahashi[ti][2])
                else:
                    ap += aoki[ai][1]
                    v.add(aoki[ai][2])

        i += 1

    print(tp - ap)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import collections

from operator import itemgetter

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    AB = [[int(x) for x in input().split()] for _ in range(N)]

    takahashi = []

    for i, (a, b) in enumerate(AB):
        if a > b:
            takahashi.append((b - a, a, b))
        elif b < a:
            takahashi.append((a - b, a, b))
        else:
            takahashi.append((0, a, b))

    takahashi.sort(key=itemgetter(1), reverse=True)
    takahashi.sort(key=itemgetter(0))

    takahashipt = 0
    aokipt = 0
    for i in range(N):
        if i % 2 == 0:
            takahashipt += takahashi[i][1]
        else:
            aokipt += takahashi[i][2]

    print(takahashipt - aokipt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
import heapq
n=int(input())

x=[]
#heapq.heapify(x)

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

#print(x)
heapq.heapify(x)
#print(x)

ascore=0
bscore=0
for i in range(n):
    temp=heapq.heappop(x)
    if i%2:
        bscore+=temp[2]
    else:
        ascore+=temp[1]

print(ascore-bscore)

Submission status: Wrong Answer

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

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((a-b, -b, i))
    di[i] = False
li.sort()
l.sort()
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: Wrong Answer

Solution code:
from heapq import *
n=int(input())
a,b=[],[]
c=[0]*n
for i in range(n):
  p,q=map(int,input().split())
  heappush(a,(-p,-q,i))
  heappush(b,(-q,-p,i))
ans=0
while a:
  if a:p,_,i=heappop(a)
  while c[i] and a:p,_,i=heappop(a)
  if c[i]==0:
    c[i]=1
    ans-=p
  if b:p,_,i=heappop(b)
  while c[i] and b:p,_,i=heappop(b)
  if c[i]==0:
    c[i]=1
    ans+=p
print(ans)

Submission status: Wrong Answer

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

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)

for i in range(1,n,2):
    ans -= cou[i]
print(ans)

Submission status: Wrong Answer

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

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

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

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

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()
    aa, bb = [], []        
    for i in range(n):
        a, b = mi()
        aa.append(a)
        bb.append(b)
    ia = sorted(range(n), key=lambda x: aa[x])
    ib = sorted(range(n), key=lambda x: bb[x])
    visted = [False]*n
    cura, curb = 0, 0
    ans = 0
    for i in range(n):
        if i%2 == 0:
            while visted[ib[curb]]:
                curb += 1
            ans += aa[ib[curb]]
            visted[ib[curb]] = True
        else:
            while visted[ia[cura]]:
                cura += 1
            ans -= bb[ia[cura]]
            visted[ia[cura]] = True

    
    print(ans)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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])
ABC = sorted(ABC,key = lambda x:[x[2],-x[0]])
q = collections.deque(ABC)
taka = 0
aoki = 0
while q:
    taka += q.popleft()[0]
    if len(q)==0:
        break
    aoki += q.popleft()[1]
print(taka-aoki)

Submission status: Wrong Answer

Solution code:


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


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


def solve():
    N = read_int()
    eaten = [False]*N
    AB = []
    for _ in range(N):
        AB.append(read_ints())
    players = [
        sorted(range(N), key=lambda i: (AB[i][0], AB[i][1])),
        sorted(range(N), key=lambda i: (AB[i][1], AB[i][0])),
    ]
    p = 0
    scores = [0, 0]
    for _ in range(N):
        while eaten[players[p][-1]]:
            players[p].pop()
        i = players[p].pop()
        scores[p] += AB[i][p]
        eaten[i] = True
        p ^= 1
    return scores[0]-scores[1]


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

Submission status: Wrong Answer

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 = []
b = []
c = [0]*n
for i in range(n):
    A, B = MAP()
    a.append([A, B, i])
    b.append([B, A, i])

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

while a or b:
    while a and c[a[0][2]] == 1:
        a.popleft()
    if a:
        c[a[0][2]] = 1
        tmp_a += a[0][0]
        a.popleft()

    while b and c[b[0][2]] == 1:
        b.popleft()
    if b:
        c[b[0][2]] = 1
        tmp_b += b[0][0]
        b.popleft()

ans = tmp_a - tmp_b
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = sorted([list(map(int, input().split())) for _ in range(n)])[::-1]
ans = 0
# 高橋くん->最大にしたい　青木さん->最小にしたい

if n%2==0: x=n//2
else: x = n//2+1
ans = [0,0]
for i in range(n):
    a,b = ab[i][0], ab[i][1]
    if i%2==0: ans[0] += a
    else: ans[1] += b
print(ans[0]-ans[1])

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, a, b))
AB.sort()
T = 0
A = 0
for i in range(n):
    _, a, b = AB.pop()
    if i%2 == 0:
        T += a
    else:
        A += b
print(T-A)

Submission status: Wrong Answer

Solution code:
n =int(input())
AB = []
for i in range(n):
    a, b = map(int, input().split())
    AB.append((b-a, a, b))
AB.sort()
T = 0
A = 0
for i in range(n):
    _, a, b = AB.pop()
    if i%2 == 0:
        T += a
    else:
        A += b
print(T-A)

Submission status: Wrong Answer

Solution code:
def solve(n, a, b):
    v = sorted(sorted(zip(a, b), reverse=True), key=lambda x:x[0]-x[1])
    ans = 0
    for i in range(n):
        if i % 2 == 0:
            ans += v[i][0]
        else:
            ans -= v[i][1]
    return ans

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

Solution code:
def solve(n, a, b):
    v = sorted(zip(a, b), key=lambda x:x[0]-x[1])
    ans = 0
    for i in range(n):
        if i % 2 == 0:
            ans += v[i][0]
        else:
            ans -= v[i][1]
    return ans

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

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

Solution code:
# https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_c

# 相手との差を最大化するので、必ずしも自分が最大になるようにしても正解にはならない。
# 場合によっては、自分が高くなくても、相手がすごく高くなるものを先に食べる必要がある。
# そういう意味では、差が大きいものから処理していく。
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[2], reverse=True)
x = y = 0
for i in range(n):
    if i % 2 == 0:
        x += dish[i][0]
    else:
        y += dish[i][1]
ans = x - y
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_c

# 相手との差を最大化するので、必ずしも自分が最大になるようにしても正解にはならない。
# 場合によっては、自分が高くなくても、相手がすごく高くなるものを先に食べる必要がある。
# そういう意味では、差が大きいものから処理していく。
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[2])
dish.sort(key=lambda x: x[0])
x = y = 0
for i in range(n):
    if i % 2 == 0:
        x += dish[i][0]
    else:
        y += dish[i][1]
ans = x - y
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_c

# 相手との差を最大化するので、必ずしも自分が最大になるようにしても正解にはならない。
# 場合によっては、自分が高くなくても、相手がすごく高くなるものを先に食べる必要がある。
# そういう意味では、差が大きいものから処理していく。
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[2])

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

Submission status: Wrong Answer

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

Solution code:
import numpy as np

N = int(input())
A, B = np.array([input().split() for i in range(N)], dtype=np.int64).T

score = (A+B)[::-1][::2].sum()

print(score-sum(B))

Submission status: Wrong Answer

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

for i in range(N):
    cooking.append(list(map(int, input().split())))

cook_T = sorted(cooking, key=lambda x:x[0], reverse=True)
cook_A = sorted(cooking, key=lambda x:x[1])
    
ans = 0
for i in range(N):
    if(i%2 == 0):
        ans += cook_T[i][0]
    else:
        ans -= cook_T[i][1]
    
        
ans1 = 0
for i in range(N):
    if(i%2 == 0):
        ans1 += cook_A[i][0]
    else:
        ans1 -= cook_A[i][1]
print(max(ans, ans1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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((Ain+Bin,i))
    ao.append((Bin+Ain,i))
taka.sort()
ao.sort()
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)

Submission status: Wrong Answer

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


ab=sorted(abi,key=lambda x:(x[0],x[1]),reverse=True)
ba=sorted(abi,key=lambda x:(x[1],x[0]),reverse=True)

checked=set([])

ans_a=0
ans_b=0

i=0
j=0

#print(ab)
#print(ba)

while(True):

    while(i<n and ab[i][2] in checked):
        i+=1

    if(i>=n):
        break

    ans_a+=ab[i][0]
    checked.add(ab[i][2])
#    print(ans_a,ans_b,checked)

    while(j<n and ba[j][2] in checked):
        j+=1

    if(j>=n):
        break

    ans_b+=ba[j][1]
    checked.add(ba[j][2])
#    print(ans_a,ans_b,checked)

print(ans_a-ans_b)

Submission status: Wrong Answer

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


ab=sorted(abi,key=lambda x:(x[0],x[1]),reverse=True)
ba=sorted(abi,key=lambda x:(x[1],x[0]),reverse=True)

checked=set([])

ans_a=0
ans_b=0

i=0
j=0

#print(ab)
#print(ba)

while(i+j<n):

    while(i<n and ab[i][2] in checked):
        i+=1

    if(i>=n):
        break

    ans_a+=ab[i][0]
    checked.add(ab[i][2])
#    print(ans_a,ans_b,checked)

    while(j<n and ba[j][2] in checked):
        j+=1

    if(j>=n):
        break

    ans_b+=ba[j][1]
    checked.add(ba[j][2])
#    print(ans_a,ans_b,checked)

print(ans_a-ans_b)

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])
b=[i[1]for i in ab]
s=[i[0]+i[1]for i in ab]
print(sum(s[::2])-sum(b))

Submission status: Wrong Answer

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

C=[]
D=0
for _ in range(N):
    a,b=map(int,input().split())
    C.append((a,b))
    D+=b
    
C.sort(key=lambda x:x[0]+x[1])
C=deque(C)
print(C)

i=0
A=0
while C:
    (a,b)=C.pop()
    if i==0:
        A+=a+b
    i^=1
print(A-D)

Submission status: Wrong Answer

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

C=[]
D=0
for _ in range(N):
    a,b=map(int,input().split())
    C.append((a,b))
    D+=b
    
C.sort(key=lambda x:x[0]+x[1])
C=deque(C)

i=0
A=0
while C:
    (a,b)=C.popleft()
    if i%2==0:
        A+=a+b
    i^=1
print(A-D)

Submission status: Wrong Answer

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

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

i=0
A=0
B=0
while C:
    a,b=C.pop()
    if i%2:
        B+=b
    else:
        A+=a
    i+=1
print(A-B)

Submission status: Wrong Answer

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

N = int(readline())
A = [0] * N
B = [0] * N
C = [0] * N
for i in range(N):
  A[i],B[i] = map(int,readline().split())
  C[i] = A[i] + B[i]
  
C = sorted(C, reverse = True)

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