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())
P = []

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

P.sort()

ans = 0
turn = 1
for i in range(N):
    ans -= P[i][turn]
    turn = 3 - turn

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = [list(map(int,input().split())) for i in range(n)]
for i in range(n):lis[i].append(lis[i][1]-lis[i][0])

takahashi = 0
aoki = 0
lis.sort(key = lambda x:x[0],reverse=True)
lis.sort(key = lambda x:x[2],reverse=True)

for i in range(n):
    if i % 2 == 0:
        takahashi += lis[i][0]
    else:
        aoki += lis[i][1]

print(takahashi-aoki)

Submission status: Wrong Answer

Solution code:
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort(key=lambda x:(max(x),min(x)))

a = b = 0
for i,(x,y) in enumerate(reversed(src)):
    if i%2:
        b += y
    else:
        a += x
print(a-b)

Submission status: Wrong Answer

Solution code:
n=int(input())
f=[0]*n
d=[list(map(int,input().split()))+[i]for i in range(n)]
t=sorted(d,key=lambda x:(x[0]-x[1],-x[0]))[::-1]
a=sorted(d,key=lambda x:(x[0]-x[1],-x[1]))[::-1]
sumt=suma=0
while t:
    x=t.pop()
    while t and f[x[2]]:
        x=t.pop()
    if not f[x[2]]:
        sumt+=x[0]
    f[x[2]]=1
    if a:
        y=a.pop()
        while a and f[y[2]]:
            y=a.pop()
        if not f[y[2]]:
            suma+=y[1]
        f[y[2]]=1
print(sumt-suma)

Submission status: Wrong Answer

Solution code:
N = int( input())
SA = [(0,0,0)]*N
SB = [(0,0,0)]*N
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
    a, b = map( int, input().split())
    A[i] = a
    B[i] = b
    SA[i] = (-a+b,-a,i)
    SB[i] = (b-a,-b,i)
SA.sort()
SB.sort()
ansa = 0
ansb = 0
a = 0
b = 0
for i in range(N):
    if i%2 == 0:
        while 1:
            if C[a] == 0:
                _,_,j = SA[i]
                ansa += A[j]
                C[a] = 1
                a += 1
                break
            else:
                a += 1
    else:
        while 1:
            if C[b] == 0:
                _,_,j = SB[i]
                ansb += B[j]
                C[b] = 1
                b += 1
                break
            else:
                b += 1
print( ansa - ansb)

Submission status: Wrong Answer

Solution code:
N = int(input())
C = [list(map(int, input().split())) for _ in range(N)]
C = sorted(map(lambda x: (abs(x[0] - x[1]), x), C), reverse=True)
print(sum(c[1][0] for c in C[::2]) - sum(c[1][1] for c in C[1::2]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
b = []
a_b = []
point_a = 0
point_b = 0
#a_bは高橋くん視点の幸福度の差
for i in range(n):
    tmp1 , tmp2 = map(int,input().split())
    a.append(tmp1)
    b.append(tmp2)
    a_b.append((tmp1-tmp2,i))
a_b.sort()
#print(a_b)
flag = True
while a_b:
    if flag:
        point_a += a[a_b.pop(0)[1]]
    else:
        point_b += b[a_b.pop(0)[1]]
    flag = not(flag)
print(point_a - point_b)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
def inpl(): return list(map(int, input().split()))
tkao = [0, 0]
t = 0

N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
    A[i], B[i] = inpl()
C = [A, B]

S = sorted([((B[i] - A[i]), i) for i in range(N)], key=lambda x: x[0])

for i in range(N):
    tkao[i%2] += C[i%2][S.pop()[1]]

print(tkao[0] - tkao[1])

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.append(b)
    
A.sort(reverse=True)
B.sort(reverse=True)

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

Submission status: Wrong Answer

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

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

P.sort()

ans = 0
turn = 1
for i in range(N):
    ans += P[i][turn]
    turn = 3 - turn

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
b = []
real = []
for i in range(n):
    c,d = map(int,input().split())
    a.append((c,d,i))
    b.append((d,c,i))
    real.append((c,d))


a.sort(reverse = True)
b.sort(reverse = True)
count = 0
check = []
for i in range(n):
    check.append(0)

i = 0
j = 0
sum1 = 0
sum2 = 0
while(count < n):
    if count & 1:
        indi = a[j][2]
        while(check[indi] != 0):
            j+=1
            indi = a[j][2]
        count += 1
        check[indi] = 1
        sum2 += real[indi][1]
    else:
        indi = a[i][2]
        while(check[indi] != 0):
            i+=1
            indi = a[i][2]
        check[indi] = 1
        sum1 += real[indi][0]
        count += 1
print(sum1 - sum2)

Submission status: Wrong Answer

Solution code:
import collections

def solve():
    N = int(input())
    A = []
    B = []
    for i in range(N):
        (a, b) = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)

    pq = sorted([(a,b) for a, b in zip(A, B)], key=lambda x: (max(x), sum(x)), reverse=True)
    pq = collections.deque(pq)

    # print(pq)
    score = [0, 0]
    idx = 0
    while pq:
        x = pq.popleft()
        score[idx] += x[idx]
        idx = (idx + 1) % 2
    # print(score)

    return score[0] - score[1]


def main():
    ans = solve()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
C = [list(map(int, input().split())) for _ in range(N)]
C = sorted(map(lambda x: (x[0] - x[1], x), C), reverse=True)
print(sum(c[1][0] for c in C[::2]) - sum(c[1][1] for c in C[1::2]))

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:(x[0] - x[1],-x[0]))

t = 0
a = 0
for i in range(n):
    if i % 2 == 0:
        t += AB.pop(0)[0]
    else:
        a += AB.pop(0)[1]
print(t-a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A.sort(reverse=1)

B=[]
for i in range(n):
    B.append([i,A[i][0]-A[i][1]])

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

#print(B)

S=[0,0]
for i in range(n):
    S[i%2]+=A[B[i][0]][i%2]

print(S[0]-S[1])

Submission status: Wrong Answer

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

li2 = sorted(li, key=lambda x: (x[0] - x[1]), reverse=True)
v = 0
for a, b in li2[::2]:
    v += a
for a, b in li2[1::2]:
    v -= b

print(v)

Submission status: Wrong Answer

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

a = b = 0
for i,(x,y) in enumerate(reversed(src)):
    if i%2:
        b += y
    else:
        a += x
print(a-b)

Submission status: Wrong Answer

Solution code:
def nikkei2019_qual_c():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n)]
    ab.sort(key=lambda x: (x[1] - x[0], x[0]), reverse=True)
    takahashi = ab[::2]
    aoki = ab[1::2]
    t_score = 0
    a_score = 0
    for v in takahashi:
        t_score += v[0]
    for v in aoki:
        a_score += v[1]
    print(t_score - a_score)


nikkei2019_qual_c()

Submission status: Wrong Answer

Solution code:
N = int(input())
Takahashi = [None] * N
Aoki = [None] * N
Used = [False] * N
for i in range(N):
    a, b = map(int, input().split())
    Takahashi[i] = (a, b, i)
    Aoki[i] = (b, a, i)
Takahashi.sort(reverse = True)
Aoki.sort(reverse = True)
TotalPoint = 0

usedDish = 0
tnum, anum = 0, 0
while usedDish < N:
    if usedDish % 2 == 0:
        while Used[Takahashi[tnum][2]]:
            tnum += 1
        TotalPoint += Takahashi[tnum][0]
        Used[Takahashi[tnum][2]] = True
        usedDish += 1
    else:
        while Used[Aoki[anum][2]]:
            anum += 1
        TotalPoint -= Aoki[anum][0] 
        Used[Aoki[anum][2]] = True
        usedDish += 1
print(TotalPoint)

Submission status: Wrong Answer

Solution code:
n = input()
flag = [0 for i in xrange(n)]
a = []
b = []
for i in xrange(n):
	x,y = map(int, raw_input().split())
	a.append((x,y,i))
	b.append((y,x,i))
a.sort(reverse = True)
b.sort(reverse = True)
ptr1 = 0
ptr2 = 0
turn = 0
ans1 = 0
ans2 = 0
while ptr1 < n and ptr2 < n:
	if turn == 0:
		while ptr1 < n and flag[a[ptr1][2]] == 1:
			ptr1 += 1
		if ptr1 < n:
			ans1 += a[ptr1][0]
			flag[a[ptr1][2]] = 1
			ptr1 += 1
		turn = 1
	else:
		while ptr2 < n and flag[b[ptr2][2]] == 1:
			ptr2 += 1
		if ptr2 < n:
			ans2 += b[ptr2][0]
			flag[b[ptr2][2]] = 1
			ptr2 += 1
		turn = 0
print ans1-ans2

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:x[0] - x[1])
print(AB)

t = 0
a = 0
for i in range(n):
    if i % 2 == 0:
        t += AB.pop(0)[0]
    else:
        a += AB.pop(0)[1]
print(t-a)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

aa.sort(key = lambda x:x[1])
co = 0
for i in range(n):
	if i % 2 == 0:
		co += aa[i][0]

	else:
		co -= aa[i][1]
print(co)

Submission status: Wrong Answer

Solution code:
def solve(n, xs, ys):
    first_score = 0
    second_score = 0
    zs = sorted(zip(xs, ys))
    for i in range(n):
        if i % 2 == 0:
            first_score += zs[i][0]
        else:
            second_score += zs[i][1]
    res = first_score - second_score
    return res


def main():
    n = int(input())
    xs = [0] * n
    ys = [0] * n
    for i in range(n):
        xs[i], ys[i] = (int(z) for z in input().split())
    res = solve(n, xs, ys)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


a.sort(reverse = True)
b.sort(reverse = True)
count = 0
check = []
for i in range(n):
    check.append(0)

i = 0
j = 0
sum1 = 0
sum2 = 0
while(count < n):
    if count & 1:
        indi = a[j][2]
        while(check[indi] != 0):
            j+=1
            indi = a[j][2]
        count += 1
        check[indi] = 1
        sum2 += b[indi][0]
    else:
        indi = a[i][2]
        while(check[indi] != 0):
            i+=1
            indi = a[i][2]
        check[indi] = 1
        sum1 += a[indi][0]
        count += 1
print(sum1 - sum2)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

dishes = []
n = getN()
for i in range(n):
    dishes.append(getMN())

dishes.sort(key=lambda x: (x[0] - x[1]))
#print(dishes)
ans = 0
for i, d in enumerate(dishes):
    if i % 2 == 0:
        ans += d[0]
    else:
        ans -= d[1]

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

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

taka = 0
aoki = 0
for i, c in enumerate(Cs):
    if i % 2 == 0:
        taka += Cs[i][1]
    else:
        aoki += Cs[i][2]
print(taka - aoki)    

# for i in range(N // 2):
#     aoki += Cs[i][2]
# for i in range(N // 2, N):
#     taka += Cs[i][1]
# print(taka - aoki)

Submission status: Wrong Answer

Solution code:
from sys import stderr, setrecursionlimit
from collections import deque
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

n = getInt()
dishes = getIntsLines(n)
dishes_dif = map(lambda x:x[0] - x[1], dishes)

# 青木さんは先頭から，高橋くんは末尾から持っていく
dishes_tuple = deque(sorted(zip(dishes, dishes_dif), key=lambda x:x[1]))

takahashi = 0
aoki = 0

while len(dishes_tuple) > 1:
    dish = dishes_tuple.pop()
    takahashi += dish[0][0]
    dish = dishes_tuple.pop()
    aoki += dish[0][1]

if len(dishes_tuple) == 1:
    dish = dishes_tuple.pop()
    takahashi += dish[0][0]

print(takahashi - aoki)

Submission status: Wrong Answer

Solution code:
N = input()
A = [0]*N
B = [0]*N
for i in range(N):
  A[i],B[i] = map(int, raw_input().split())

T = []
for i in range(N):
  T += [(-A[i]-B[i], i)]
T.sort()

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

Submission status: Wrong Answer

Solution code:
n=int(input())
p=[]
for i in range(n):
    a,b=map(int, input().split())
    p.append([a,b,b-a])
p.sort(key=lambda x:-x[2])
taka=0
aoki=0
for i in range(n):
    if i%2==0:taka+=p[i][0]
    else: aoki+=p[i][1]
print(taka-aoki)

Submission status: Wrong Answer

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


a.sort()
b.sort()
count = 0
check = []
for i in range(n):
    check.append(0)

i = 0
j = 0
sum1 = 0
sum2 = 0
while(count < n):
    if count & 1:
        indi = a[j][2]
        while(check[indi] != 0):
            j+=1
            indi = a[j][2]
        count += 1
        check[indi] = 1
        sum2 += b[indi][0]
    else:
        indi = a[i][2]
        while(check[indi] != 0):
            i+=1
            indi = a[i][2]
        check[indi] = 1
        sum1 += a[indi][0]
        count += 1
print(sum1 - sum2)

Submission status: Wrong Answer

Solution code:
N = int(input())

L = list(list(map(int,input().split())) for i in range(N))
#print(L)
turn = "taka"
takapoint = 0
aokipoint = 0

L = sorted(L,key=lambda x: x[1],reverse=True)
L = sorted(L,key=lambda x: x[0],reverse=True)


while len(L) :
    if turn == "taka" :
        takapoint += L[0][0]
        L.pop(0)
        turn = "aoki"
    else :
        aokipoint += L[0][1]
        L.pop(0)
        turn = "taka"
ans = takapoint - aokipoint

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[0 for i in range(N)]
B=[0 for i in range(N)]
for i in range(N):
    A[i],B[i]=map(int,input().split())
X=sorted([(A[i],B[i],i) for i in range(N)])
Y=sorted([(B[i],A[i],i) for i in range(N)])
used=[0 for i in range(N)]
tkhs=0
aoki=0
while(len(X)>0 and len(Y)>0):
    while(True):
        if len(X)>0 and used[X[-1][2]]==1:
            X.pop()
        else:
            break
    if len(X)==0:
        break
    tkhs+=X[-1][0]
    used[X[-1][2]]=1
    X.pop()
    while(True):
        if len(Y)>0 and used[Y[-1][2]]==1:
            Y.pop()
        else:
            break
    if len(Y)==0:
        break
    aoki+=Y[-1][0]
    used[Y[-1][2]]=1
    Y.pop()
print(tkhs-aoki)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N = int(input())
    A = [li_input() for _ in range(N)]

    P1 = [x[:] for x in A]
    # P2 = [x[:] for x in A]

    P1.sort(key=lambda p:p[0]-p[1], reverse=True)

    # print(P1)

    p1_ans = 0
    p2_ans = 0

    for i in range(len(P1)):
        if i % 2 == 0:
            p1_ans += P1[i][0]
        else:
            p2_ans += P1[i][1]
        
    print(p1_ans - p2_ans)



main()

Submission status: Wrong Answer

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

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

SUM=[[sum(AB[i])]+[i] for i in range(N)]

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

T=[]
A=[]

for s,i in SUM:
    if i%2==0:
        T.append(i)
    else:
        A.append(i)

ANS=0
for t in T:
    ANS+=AB[t][0]
for a in A:
    ANS-=AB[a][1]

print(ANS)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A, B = [], []
Z = []

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

    Z.append(-a - b)

indexes = np.argsort(Z)
ans = 0
for i, cnt in enumerate(indexes):
    if cnt % 2 == 0:
        ans += A[i]
    else:
        ans -= B[i]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = [list(map(int,input().split())) for i in range(n)]
for i in range(n):lis[i].append(lis[i][1]-lis[i][0])

takahashi = 0
aoki = 0

lis.sort(key = lambda x:x[2],reverse=True)

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

Submission status: Wrong Answer

Solution code:
def read():
    return int(input())


def readlist():
    return list(map(int, input().split()))


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


N = read()
A = []
B = []
C = []
for _ in range(N):
    a, b = readmap()
    A.append(a)
    B.append(b)
    C.append(a+b)

A.sort(key=dict(zip(A, C)).get)
B.sort(key=dict(zip(B, C)).get)

A.reverse()
B.reverse()

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

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
p=[]
for i in range(n):
    a,b=map(int, input().split())
    p.append([a,b,b-a])
p.sort(key=lambda x:x[2])
taka=0
aoki=0
for i in range(n):
    if i%2==0:taka+=p[i][0]
    else: aoki+=p[i][1]
print(taka-aoki)

Submission status: Wrong Answer

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



count = 0
check = []
for i in range(n):
    check.append(0)

i = 0
j = 0
sum1 = 0
sum2 = 0
while(count < n):
    if count & 1:
        indi = a[j][2]
        while(check[indi] != 0):
            j+=1
            indi = a[j][2]
        count += 1
        check[indi] = 1
        sum2 += b[indi][0]
    else:
        indi = a[i][2]
        while(check[indi] != 0):
            i+=1
            indi = a[i][2]
        check[indi] = 1
        sum1 += a[indi][0]
        count += 1
print(sum1 - sum2)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N = int(input())
    A = [li_input() for _ in range(N)]

    P1 = [x[:] for x in A]
    # P2 = [x[:] for x in A]

    P1.sort(key=lambda p:p[0]-p[1])

    p1_ans = 0
    p2_ans = 0

    for i in range(len(P1)):
        if i % 2 == 0:
            p1_ans += P1[i][0]
        else:
            p2_ans += P1[i][1]
        
    print(p1_ans - p2_ans)



main()

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[]
for i in range(n):
    A.append(list(map(int,input().split())))
B=[]
for i in range(n):
    B.append([i,A[i][0]-A[i][1]])

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

#print(B)

S=[0,0]
for i in range(n):
    S[i%2]+=A[B[i][0]][i%2]

print(S[0]-S[1])

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
ab = [tuple(map(int, line.split())) for line in sys.stdin.readlines()]
abd = [(a - b, -a, b) for a, b in ab]
abd.sort()
print(-sum(x[1] for x in abd[::2]) - sum(x[2] for x in abd[1::2]))

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[]
for i in range(n):
    A.append(list(map(int,input().split())))
B=[]
for i in range(n):
    B.append([i,A[i][0]-A[i][1]])

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

print(B)

S=[0,0]
for i in range(n):
    S[i%2]+=A[B[i][0]][i%2]

print(S[0]-S[1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

rec = sorted(rec, key=lambda x: x[0] - x[1])
num = 0
for i in range(N):
    if i % 2 == 0:
        num += rec[i][0]
    else:
        num -= rec[i][1]

print(num)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
ab = [tuple(map(int, line.split())) for line in sys.stdin.readlines()]
abd = [(a - b, a, b) for a, b in ab]
abd.sort()
print(sum(x[1] for x in abd[::2]) - sum(x[2] for x in abd[1::2]))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n = int(input())
l1=[]
l2=[]
Flg=[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 i in range(n):
    if i in A:
        ans+=l[i][0]
    else:
        ans-=l[i][1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A, B = [], []
for i in range(N):
  a, b = map(int, input().split())
  A.append(a)
  B.append(b)
  
x, y = 0, 0
for i in range(N//2):
  if A.index(max(A)) == B.index(max(B)):
    s = A.index(max(A))
    x += A[s]
    A.pop(s)
    B.pop(s)
    t = B.index(max(B))
    y += B[t]
    A.pop(t)
    B.pop(t)
  else:
    s = max(A)
    t = max(B)
    u = A[B.index(t)]
    w = sorted(B)[-2]
    if s-t >= u-w:
      x += s
      y += t
    else:
      x += u
      y += w
      
if N % 2 == 1:
  x += A[0]
print(x-y)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A, B = [], []
for i in range(N):
  a, b = map(int, input().split())
  A.append(a)
  B.append(b)
  
x, y = 0, 0
for i in range(N//2):
  if A.index(max(A)) == B.index(max(B)):
    s = A.index(max(A))
    x += A[s]
    A.pop(s)
    B.pop(s)
    t = B.index(max(B))
    y += B[t]
    A.pop(t)
    B.pop(t)
  else:
    s = max(A)
    t = max(B)
    u = A[B.index(t)]
    w = sorted(B)[-2]
    if s-t >= u-w:
      x += s
      y += t
    else:
      x += u
      y += w
      
if N % 2 == 1:
  x += A[0]
print(x-y)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

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

x.sort()

ascore=0
bscore=0

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

print(ascore-bscore)

Submission status: Time Limit Exceeded

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

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