Problem:


Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.



Sample input:
3
10 10
20 20
30 30


Sample output:
20

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
earned = np.zeros((N,1),dtype=np.int64)
loss = np.zeros((N,1),dtype=np.int64)
scores = np.zeros((N,1),dtype=np.int64)
for i in range(N):
    a, b = map(np.int64, input().split())
    earned[i] = a
    loss[i] = b
    scores[i] = a+b
 
order = [i[0] for i in sorted(enumerate(scores), key=lambda x:x[1] , reverse=True)]
print(order)
result = 0
for ind, item in enumerate(order):
    if ind%2==0:
        result += earned[item]
    else:
        result -= loss[item]
 
print(result[0])

Submission status: Wrong Answer

Solution code:
# C 2nd
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def db(x): 
    global debug
    if debug: print(x)
debug = False
N = getInt()
dia,dib = zeros(N),zeros(N)
for i in range(N):
    a,b = getIntList()
    dia[i] = [abs(a-b),a,b,i]
    dib[i] = [abs(a-b),b,a,i]
#    dia[i] = [a,b,i]
#    dib[i] = [b,a,i]
db(dia)
dia.sort()
dia.reverse()
db(dia)
db(dib)
dib.sort()
dib.reverse()
db(dib)
p = 0
eaten = set([])
ia,ib = 0,0
for i in range(N):
    if i%2==0:
        while dia[ia][3] in eaten: ia += 1
        p += dia[ia][1]
        eaten.add(dia[ia][3])
        db(('a',dia[ia][3],dia[ia][1]))
    else:
        while dib[ib][3] in eaten: ib += 1
        if len(eaten)==N-2:
            il = ib+1
            while dib[il][3] in eaten: il += 1
            if -dib[ib][1]+dib[il][1]>-dib[il][1]+dib[ib][1]: 
                ib = il
                db(ib)
        p -= dib[ib][1]
        eaten.add(dib[ib][3])
        db(('b',dib[ib][3],dib[ib][1]))
print(p)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    a = []
    b = []
    for _ in range(N):
        ai, bi = map(int, input().split())
        a.append(ai)
        b.append(bi)
    ans = 0
    s = 1
    while True:
        if len(a) == 0:
            print(ans)
            exit()
        ma = a.pop()
        ans += ma * s
        b.pop()
        t = a
        a = b
        b = t
        s *= -1


main()

Submission status: Wrong Answer

Solution code:
import heapq


def solve(N, ABs):
    # print(N, ",", ABs)
    K = 10 ** 9
    taken = {i: False for i in range(N)}
    As = [(K - ab[0], K - ab[1], i) for i, ab in enumerate(ABs)]
    Bs = [(K - ab[1], K - ab[0], i) for i, ab in enumerate(ABs)]
    heapq.heapify(As)
    heapq.heapify(Bs)
    ans = 0
    taken_count = 0
    isTakahashi = True
    while True:
        if taken_count >= N:
            break

        am = As[0]
        while taken[am[2]]:
            heapq.heappop(As)
            am = As[0]

        bm = Bs[0]
        while taken[bm[2]]:
            heapq.heappop(Bs)
            bm = Bs[0]

        aTaka = K - am[0]
        bAoki = K - bm[0]

        bTaka = K - bm[1]
        aAoki = K - am[1]

        if isTakahashi:
            if aTaka > bAoki:
                ans += aTaka
                taken[am[2]] = True
                taken_count += 1
                heapq.heappop(As)
            else:
                ans += bTaka
                taken[bm[2]] = True
                taken_count += 1
                heapq.heappop(Bs)
        else:
            if aTaka > bAoki:
                ans -= aAoki
                taken[am[2]] = True
                taken_count += 1
                heapq.heappop(As)
            else:
                ans -= bAoki
                taken[bm[2]] = True
                taken_count += 1
                heapq.heappop(Bs)

        isTakahashi = not isTakahashi

    return ans

if __name__ == "__main__":
    N = int(input())
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, ABs))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq
import sys
N = int(input())
a,b = [], []
for i in range(N):
    ai,bi = map(int, input().split())
    heapq.heappush(a, (bi-ai,ai,i))
    heapq.heappush(b, (bi-ai,bi,i))
# print(a, b)
aoki = []
taka = []
used = set([-1])
for i in reversed(range(N)):
    if i%2==1: # aoki
        m = 1e10
        k = -1
        while k in used and b:
            v,vv,k = heapq.heappop(b)
        used.add(k)
        aoki.append(vv)
    else:
        m = 1e10
        k = -1
        while k in used and a:
            v,vv,k = heapq.heappop(a)
        used.add(k)
        taka.append(vv)
    #print(taka, aoki, used)
print(sum(taka)-sum(aoki))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
for i in range(N):
    a, b = map(int, input().split())
    L += [[a, b]]
L.sort(key=lambda x:(x[1], x[0]))
L.reverse()
A = 0
B = 0
i = 0
while 2*(i+1) <= N:
    A += L[2*i][0]
    B += L[2*i+1][1]
    i += 1
if N%2 == 1:
    A += L[-1][0]
print(A-B)

Submission status: Wrong Answer

Solution code:
# 2019/01/27
# NIKKEI Programming Contest 2019 - C
from operator import itemgetter

# Input
n = int(input())
ablist = list()

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

ablist.sort(key=itemgetter(1), reverse=True)

tsum = 0
asum = 0

# Sum
for idx in range(n):
    if idx % 2 == 0:
        # Turn of Takahashi
        tsum += ablist[idx][0]
    else:
        # Turn of Aoki
        asum += ablist[idx][1]

ans1 = tsum - asum

# Output
print(ans1)

Submission status: Wrong Answer

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


def main():
    n = int(input())
    diff_positive = dict()
    diff_negaitive = dict()
    pos_max = 0
    nega_max = 0
    a = [0 for _ in range(n)]
    b = [0 for _ in range(n)]

    for i in range(n):
        ai, bi = map(int, input().split())
        a[i] = ai
        b[i] = bi

        if ai - bi >= 0:
            diff_positive[i] = ai - bi
            pos_max = max(pos_max, ai - bi)
        else:
            diff_negaitive[i] = bi - ai
            nega_max = max(nega_max, abs(ai - bi))

    posi = sorted(diff_positive.items(), key=lambda x: x[1], reverse=True)
    nega = sorted(diff_negaitive.items(), key=lambda x: x[1], reverse=True)
    taka_point = 0
    aoki_point = 0

    if len(posi) % 2 == 0:
        taka = posi[::2] + nega[::2]
        aoki = posi[1::2] + nega[1::2]

        for t in taka:
            taka_point += a[t[0]]

        for a in aoki:
            aoki_point += b[a[0]]

        print(taka_point - aoki_point)
    else:
        taka1 = posi[::2] + nega[1::2]
        aoki1 = posi[1::2] + nega[::2]

        for t1 in taka1:
            taka_point += a[t1[0]]

        for a1 in aoki1:
            aoki_point += b[a1[0]]

        ans1 = taka_point - aoki_point

        taka2 = posi[1::2] + nega[::2]
        aoki2 = posi[::2] + nega[1::2]

        for t2 in taka2:
            taka_point += a[t2[0]]

        for a2 in aoki2:
            aoki_point += b[a2[0]]

        ans2 = taka_point - aoki_point
        print(max(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict

D = defaultdict(int)
N = int(input())
asum = 0
bsum = 0
p = 0

X = []
Y = []
for i in range(N):
	A, B = list(map(int, input().split()))
	X.append([A - B, -A, i])
	Y.append([A - B, -B, i])
X = sorted(X)
Y = sorted(Y)

turn = "a"
a0 = 0
b0 = -1
while p < N:
	if turn == "a":
		while True:
			if D[X[a0][2]] == 0:
				asum -= X[a0][1]
				D[X[a0][2]] += 1
				break
			else:
				a0 += 1

		turn = "b"
	else:
		while True:
			if D[Y[b0][2]] == 0:
				bsum -= Y[b0][1]
				D[Y[b0][2]] += 1
				break
			else:
				b0 -= 1
				
		turn = "a"
	p += 1

print(asum - bsum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from pprint import pprint
from operator import itemgetter
input = sys.stdin.readline

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

AB.sort(key=itemgetter(0))
AB.reverse()
for i in range(N):
    A[AB[i][2]][1] = i

AB.sort(key=itemgetter(1))
AB.reverse()
for i in range(N):
    B[AB[i][2]][1] = i

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

Submission status: Wrong Answer

Solution code:
N=input()

L=[ map(int,raw_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][0]
	else:
		ans-=L[i][1]
print ans

Submission status: Wrong Answer

Solution code:
n = int(input())
AB = [tuple(map(int,input().split())) + (i,) for i in range(n)]
AB_1 = sorted(AB,reverse=True,key=lambda x:(x[0],x[1]))
AB_2 = sorted(AB,reverse=True,key=lambda x:(x[1],x[0]))

used = set()
i = 0
j = 0
ta = 0
ao = 0
while True:
    while AB_1[i][2] in used:
        i += 1
    if AB_1[i][0] - AB_2[j][1] >= AB_2[j][0] - AB_1[i][1]:
        ta += AB_1[i][0]
        used.add(AB_1[i][2])
    else:
        ta += AB_2[j][0]
        used.add(AB_2[j][2])

    if len(used) >= n:
        break
    while AB_1[i][2] in used:
        i += 1

    while AB_2[j][2] in used:
        j += 1
    if AB_2[j][1] - AB_1[i][0] >= AB_1[i][1] - AB_2[j][0]:
        ao += AB_1[i][1]
        used.add(AB_1[i][2])
    else:
        ao += AB_2[j][1]
        used.add(AB_2[j][2])

    if len(used) >= n:
        break
    while AB_2[j][2] in used:
        j += 1
print(ta-ao)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict

D = defaultdict(int)
N = int(input())
asum = 0
bsum = 0
p = 0

X = []
Y = []
for i in range(N):
	A, B = list(map(int, input().split()))
	X.append([A - B, -A, i])
	Y.append([A - B, -B, i])
X = sorted(X)
Y = sorted(Y)

print(X)
print(Y)
turn = "a"
a0 = 0
b0 = -1
while p < N:
	if turn == "a":
		while True:
			if D[X[a0][2]] == 0:
				asum -= X[a0][1]
				D[X[a0][2]] += 1
				break
			else:
				a0 += 1

		turn = "b"
	else:
		while True:
			if D[Y[b0][2]] == 0:
				bsum -= Y[b0][1]
				D[Y[b0][2]] += 1
				break
			else:
				b0 -= 1
				
		turn = "a"
	p += 1

print(asum - bsum)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = []
for i in range(n):#h:高さ
    c.append([int(m) for m in input().split()]+[i])
"""d = []
for i in range(n):#h:高さ
    d.append([c[i][1], c[i][0], i])"""
tans = 0
aans = 0
e = []
a = []
b = []
for i in range(2 * n):
    if i < n:
        e.append([c[i][0], i])
        a.append([c[i][0], i])
    else:
        e.append([c[i - n][1], i - n])
        b.append([c[i - n][1], i - n])
#c.sort(reverse=True)
e.sort(reverse=True)

#d.sort(reverse=True)
a.sort(reverse=True)
b.sort(reverse=True)
numset = set()
i = 0
k = 0
wa = 0
wb = 0
while wa < n and wb < n:
    if k % 2 == 0:#t
        while True:
            if a[wa][1] not in numset and b[wb][1] not in numset:
                if c[a[wa][1]][0] - c[b[wb][1]][1] >= c[b[wb][1]][0] - c[a[wa][1]][1]:

                    tans += c[a[wa][1]][0]
                    numset.add(a[wa][1])
                    wa += 1
                    k += 1
                    break
                else:
                    tans += c[b[wb][1]][0]
                    numset.add(b[wb][1])
                    wb += 1
                    k += 1
                    break

            elif a[wa][1] in numset and b[wb][1] not in numset:
                wa += 1
                if wa == n:
                    break
            elif b[wb][1] in numset and a[wa][1] not in numset:
                wb += 1
                if wb == n:
                    break
            else:
                wa += 1
                wb += 1
                if wa == n or wb == n:
                    break
    else:
        while True:
            if a[wa][1] not in numset and b[wb][1] not in numset:
                if c[b[wb][1]][1] - c[a[wa][1]][0] >= c[a[wa][1]][1] - c[b[wb][1]][0]:

                    aans += c[b[wb][1]][1]
                    numset.add(b[wb][1])
                    wb += 1
                    k += 1
                    break
                else:
                    aans += c[a[wa][1]][1]
                    numset.add(a[wa][1])
                    wa += 1
                    k += 1
                    break

            elif a[wa][1] in numset and b[wb][1] not in numset:
                wa += 1
                if wa == n:
                    break
            elif b[wb][1] in numset and a[wa][1] not in numset:
                wb += 1
                if wb == n:
                    break
            else:
                wa += 1
                wb += 1
                if wa == n or wb == n:
                    break
"""while i < 2 * n:
    if k % 2 == 0:#t
        while True:
            if e[i][1] not in numset:
                tans += c[e[i][1]][0]
                numset.add(e[i][1])
                i += 1
                k += 1
                break
            else:
                i += 1
                if i == 2 * n:
                    break
    else:
        while True:
            if e[i][1] not in numset:
                aans += c[e[i][1]][1]
                numset.add(e[i][1])
                i += 1
                k += 1
                break
            else:
                i += 1
                if i == 2 * n:
                    break"""
print(tans -aans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [0  for i in range(n)]
b = [0  for i in range(n)]
x = [[0,0,0]  for i in range(n)]

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

s=sorted(x,reverse=True)


nu=n//2
mod=n%2

count=0
for i in range(nu):
    count+=x[i*2][1]

    count-=x[i*2+1][2]


if mod==1:
    count=x[2*nu][1]

    

    

print(count)

Submission status: Wrong Answer

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

AA = collections.deque(numpy.argsort(A)[::-1])
BB = collections.deque(numpy.argsort(B)[::-1])
ans = 0
aans,bans = 0,0
while True:
    try:
        a = AA.popleft()
        ans += A[a]
        if BB[0] == a:
            BB.popleft()
        b = BB.popleft()
        ans -= B[b]
        if AA[0] == b:
            AA.popleft()
    except:
        break
#if AA:
#    aans += A[AA.popleft()]
#if BB:
#    bans += B[BB.popleft()]

print(ans)
#print(aans - bans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [0  for i in range(n)]
b = [0  for i in range(n)]
x = [[0,0,0]  for i in range(n+2)]

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

s=sorted(x,reverse=True)


nu=n//2
mod=n%2

count=0
for i in range(nu):
    count+=x[i*2][1]

    count-=x[i*2+1][2]


if mod==1:
    count=x[2*nu][1]

    

    

print(count)

Submission status: Wrong Answer

Solution code:
N = input()
val= []
for _ in xrange(N):
    a, b  = map(int, raw_input().split())
    val.append((a + b, a, b))
val.sort()

res = 0
for i, (v, a, b) in enumerate(val):
    res += a if i % 2 == 0 else -b
print res

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
happiness_hash = {}
for i in range(n):
    a, b = tuple(map(int, input().split(' ')))

    happiness_hash[i] = (a, b)

sorted_happiness = (sorted(happiness_hash.items(), key = lambda x : (x[0], x[1]), reverse = True))

def select(who, opponent):
    sorted_happiness = sorted_happinesses[who]
    k, v = sorted_happiness[0]
    selected_dish = k
    max_happiness = v[who]
    max_opponent_happiness = v[opponent]
    if len(sorted_happiness) > 1 and sorted_happiness[0][0] == sorted_happiness[1][0]:
        for item in sorted_happiness:
            happiness = item[1][who]
            opponent_happiness = item[1][opponent]
            if max_opponent_happiness < opponent_happiness:
                max_opponent_happiness = opponent_happiness
                selected_dish = item[0]

    return max_happiness, selected_dish

total = 0
i = 0
for k, v in sorted_happiness:
    happiness = 0
    selected = None
    # aoki turn
    if i % 2 == 0:
        happiness, selected = v[0], k
        total += happiness
    else:
        happiness, selected = v[1], k
        total -= happiness
    i += 1

print(total)

Submission status: Wrong Answer

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

for i in range(n):
    ab[i].append(ab[i][0] - ab[i][1])
ab = sorted(ab, key=lambda x:(x[2]))
res = 0
for i in range(n):
    if i % 2 == 0:
        res += ab[i][0]
    else:
        res -= ab[i][1]

print(res)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter

taka = 0
ao = 0

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

for i, temp in enumerate(string_list):
  string_list[i].append(string_list[i][0] - string_list[i][1])

sorted_list = sorted(string_list, key=itemgetter(2))

for i in range(len(sorted_list)):
  if i % 2 == 0:
    temp = sorted_list.pop(0)
    taka += temp[0]
  else:
    temp = sorted_list.pop(0)
    ao += temp[1]

print(taka - ao)

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
S=[[0,0]for i in range(N)]
notused=[1 for i in range(N)]
for i in range(N):
    A,B=[int(x) for x in raw_input().split()]
    S[i]=[A,B,i]
SA=sorted( sorted(S,key=lambda x:x[1], reverse=True), key=lambda x:x[0], reverse=True)
SB=sorted( sorted(S,key=lambda x:x[0],reverse=True), key=lambda x:x[1],reverse=True)
ans=0
tturn=1
aind=0
bind=0
for i in range(N):
    if SA[aind][0]>SB[bind][1]:
        chosen=SA[aind][2]
        notused[chosen]=0
    elif SA[aind][0]==SB[bind][1]:
        if SA[aind][1]>=SB[bind][0]:
            chosen=SA[aind][2]
            notused[chosen]=0
        else:
            chosen=SB[bind][2]
            notused[chosen]=0
    else:
        chosen=SB[bind][2]
        notused[chosen]=0
    while( notused[ SA[aind][2] ]==0):
        aind+=1
        if aind==N:
            break
    while( notused[ SB[bind][2] ]==0):
        bind+=1
        if bind==N:
            break
    if tturn:
        ans+=S[chosen][0]
    else:
        ans-=S[chosen][1]
    tturn=not(tturn)
print ans

Submission status: Wrong Answer

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

for i in range(n):
    ab[i].append(ab[i][0] - ab[i][1])
ab = sorted(ab, key=lambda x:(x[2]))
print(ab)
res = 0
for i in range(n):
    if i % 2 == 0:
        res += ab[i][0]
    else:
        res -= ab[i][1]

print(res)

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
S=[[0,0]for i in range(N)]
notused=[1 for i in range(N)]
for i in range(N):
    A,B=[int(x) for x in raw_input().split()]
    S[i]=[A,B,i]
SA=sorted( sorted(S,key=lambda x:x[1], reverse=True), key=lambda x:x[0], reverse=True)
SB=sorted( sorted(S,key=lambda x:x[0],reverse=True), key=lambda x:x[1],reverse=True)
ans=0
tturn=1
aind=0
bind=0
for i in range(N):
    if SA[aind][0]>SB[bind][1]:
        chosen=SA[aind][2]
        notused[chosen]=0
    elif SA[aind][0]==SB[bind][1]:
        if SA[aind][1]>=SB[bind][0]:
            chosen=SA[aind][2]
            notused[chosen]=0
        else:
            chosen=SA[aind][2]
            notused[chosen]=0
    else:
        chosen=SB[bind][2]
        notused[chosen]=0
    while( notused[ SA[aind][2] ]==0):
        aind+=1
        if aind==N:
            break
    while( notused[ SB[bind][2] ]==0):
        bind+=1
        if bind==N:
            break
    if tturn:
        ans+=S[chosen][0]
    else:
        ans-=S[chosen][1]
    tturn=not(tturn)
print ans

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
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()
B.sort()
ans=0
inf=float("inf")
eaten=[False]*N
for i in range(N):
  if i%2==0:
    while eaten[A[-1][2]]:
      A.pop()
    while eaten[B[-1][2]]:
      B.pop()
    a1,b0,t=A.pop()
    b1,a2,s=B.pop()
    if t==s:
      ans+=a1
      eaten[t]=True
    else:
      b2=inf    
      while B and b2>10000000000:
        if not eaten[B[-1][2]]:
          b2,a0,g=B.pop()
        else:
          B.pop()
      if a1-b1>=a2-b2:
        ans+=a1
        eaten[t]=True
        if g!=t:
          B.append((b2,a0,g))
        B.append((b1,a2,s))
      else:
        ans+=a2
        eaten[s]=True
        A.append((a1,b0,t))
        B.append((b2,a0,g))
  else:
    while eaten[A[-1][2]]:
      A.pop()
    while eaten[B[-1][2]]:
      B.pop()
    a1,b2,t=A.pop()
    b1,a0,s=B.pop()
    if t==s:
      ans-=b1
      eaten[t]=True
    else:
      a2=inf    
      while A and a2>10000000000:
        if not eaten[A[-1][2]]:
          a2,b0,g=A.pop()
        else:
          A.pop()
      if b1-a1>=b2-a2:
        ans-=b1
        eaten[s]=True
        if g!=s:
          A.append((a2,b0,g))
        A.append((a1,b2,t))
      else:
        ans-=b2
        eaten[t]=True
        A.append((a2,b0,g))
        B.append((b1,a0,s))
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
val= []
for _ in xrange(N):
    ab  = map(int, raw_input().split())
    val.append(ab)
val.sort()

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

Submission status: Wrong Answer

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

if n%2==0:
    m = n//2
else:
    m =(n+1)//2

result = []
for i in range(1 << n):
    ax = [j for j in range(n) if ((i >> j) & 1) == 1]
    if len(ax)==m:
        result.append(sum([a[i] if i in ax else b[i] for i in range(n)]))
print(max(result))

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,a,i])
    b_.append([a+b,b,i])

sa_ = sorted(
  a_,
  key = lambda x: (x[0], x[1]),
  reverse=True
)
sb_ = sorted(
  b_,
  key = lambda x: (x[0], x[1]),
  reverse=True
)

i_={}
res=0

for i in range(n):
    if i % 2 :
        item = sb_.pop()
        while ( item[2] in i_ ) :
            item = sb_.pop()
        res -= item[1]
        i_[item[2]]=1
    else :
        item = sa_.pop()
        while ( item[2] in i_ ) :
            item = sa_.pop()
        res += item[1]
        i_[item[2]]=1

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(0, n):
  a.append(list(map(int, input().split())))
b = []
c = []
result = 0
for i in range(0, n):
  b.append(a[i][0] - a[i][1])
  c.append(a[i][1] - a[i][0])
if n % 2 == 0:
  for i in range(0, int(n/2)):
    
    result += max(b)
    maxindb = b.index(max(b))
    del b[maxindb]
    del c[maxindb]
    
    result -= max(c)
    maxindc = c.index(max(c))
    del b[maxindc]
    del c[maxindc]
    
if n % 2 != 0:
  for i in range(0, n//2):
    
    result += max(b)
    maxindb = b.index(max(b))
    del b[maxindb]
    del c[maxindb]
    
    result -= max(c)
    maxindc = c.index(max(c))
    del b[maxindc]
    del c[maxindc]

  result += max(b)
print(result)

Submission status: Wrong Answer

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

AA = collections.deque(numpy.argsort(A)[::-1])
BB = collections.deque(numpy.argsort(B)[::-1])
ans = 0
aans,bans = 0,0
while True:
    try:
        a = AA.popleft()
        ans += A[a]
        if BB[0] == a:
            BB.popleft()
        b = BB.popleft()
        ans -= B[b]
        if AA[0] == b:
            AA.popleft()

    except:
        break
if AA:
    aans += A[AA.popleft()]
if BB:
    bans += B[BB.popleft()]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = [0] * n
for i in range(n):
    a,b = map(int,input().split())
    ab[i] = [a,b,abs(a-b)]
ans = 0
for i in range(n):
    ab.sort(key=lambda ab:(ab[i%2],ab[2]))
    ans += ab[-1][i%2] * ((i % 2) * -2 +1)
    ab.pop()
print(ans+1)

Submission status: Wrong Answer

Solution code:
N = input()
val= []
for _ in range(N):
    a, b  = map(int, raw_input().split())
    val.append((a, b))
val.sort()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
arr = []
for n in range(N):
  A, B = list(map(int, input().split()))
  arr.append([ A - B , A, B])

arr = sorted(arr, reverse=False)
ta = sum([a[1] for a in arr[::2]])
ao = sum([a[2] for a in arr[1::2]])
print(ta - ao)

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,a,i])
    b_.append([a+b,b,i])

sa_ = sorted(
  a_,
  key = lambda x: (x[0], x[1]),
  reverse=True
)
sb_ = sorted(
  b_,
  key = lambda x: (x[0], x[1]),
  reverse=True
)

i_={}
asum=0
bsum=0

for i in range(n):
    if i % 2 :
        item = sb_.pop()
        while ( item[2] in i_ ) :
            item = sb_.pop()
        bsum += item[1]
        i_[item[2]]=1
    else :
        item = sa_.pop()
        while ( item[2] in i_ ) :
            item = sa_.pop()
        asum += item[1]
        i_[item[2]]=1

print(asum-bsum)

Submission status: Wrong Answer

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

d = [(i, a - b, a, b) for i, (a, b) in enumerate(zip(A, B))]
dAB = sorted(d, key=lambda x: (x[2], x[3]), reverse=True)
dBA = sorted(d, key=lambda x: (x[3], x[2]), reverse=True)
a_cand = [x[0] for x in dAB]
b_cand = [x[0] for x in dBA]
used = [False] * N
a_s = 0
b_s = 0
a_i = 0
b_i = 0
i = 0
while True:
    if a_i >= N and b_i >= N and i >= N:
        break
    if i & 1 == 0:
        while a_i < N and used[a_cand[a_i]]:
            a_i += 1
        if a_i >= N:
            i += 1
            continue
        a_s += A[a_cand[a_i]]
        used[a_cand[a_i]] = True
    else:
        while b_i < N and used[b_cand[b_i]]:
            b_i += 1
        if b_i >= N:
            i += 1
            continue
        b_s += B[b_cand[b_i]]
        used[b_cand[b_i]] = True
    i += 1
print(a_s - b_s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
B = []
C = []
for i in range(N):
    a,b = [int(c) for c in input().split()]
    A.append(a)
    B.append(b)
    c = a-b if a-b != 0 else -a /10000000
    C.append(c)

CSortIndex = [i[0] for i in sorted(enumerate(C), key=lambda x:x[1])]

score = 0
for i in range(len(CSortIndex)):
    select = CSortIndex[i]
    if (i % 2 == 0):
        score += A[select]
    else:
        score -= B[select]

print(score)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [0  for i in range(n)]
b = [0  for i in range(n)]
x = [[0,0,0]  for i in range(n+2)]

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

s=sorted(x,reverse=True)


nu=n//2
mod=n%2

counta=0
for i in range(nu):
    counta+=x[i*2][1]

    counta-=x[i*2+1][2]


if mod==1:
    counta+=x[2*nu][1]

    

    

print(counta)