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: Runtime Error

Solution code:
import functools

@functools.lru_cache()

def solution(N, A, B):
    A_score=[]
    B_score=[]
    for n in range(0, N):
        if n > 0:
            tmp = A
            A = B
            B = tmp
        max_index_list = [i for i, x in enumerate(A) if x == max(A)]
        j = max_index_list[0]
        if len(max_index_list) > 1:
            maxim = 0
            for i in max_index_list:
                if B[i] > maxim:
                    j = i
                    maxim = B[i]
        if n%2==0:
            A_score.append(A[j])
        else:
            B_score.append(A[j])
        A.pop(j)
        B.pop(j)
    return(sum(A_score)-sum(B_score))

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

solution(N, A, B)

Submission status: Runtime Error

Solution code:
n = int(input())
a = []
for i in range(0, n):
  a.append(list(map(int, input())))
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, 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: Runtime Error

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


def main():
    """Function."""
    n = int(input())
    vals = [list(map(int, input().split())) for _ in range(n)]

    sorted_vals = sorted(vals, key=lambda x: (x[0], x[1]))

    dict_1 = {}
    for i in range(len(sorted_vals)):
        dict_1[str(i)] = sorted_vals[i][1]

    takahashi = 0
    for _ in range(n):
        if sorted_vals:
            length = len(sorted_vals) - 1
            takahashi += sorted_vals[length][0]
            del sorted_vals[length]
            del dict_1[str(length)]
        else:
            break

        if dict_1:
            val = max(dict_1.values())
            takahashi -= val
            keys = [int(kv[0]) for kv in dict_1.items() if kv[1] == val]
            keys = sorted(keys)
            del dict_1[str(keys[-1])]
            del sorted_vals[keys[-1]]
        else:
            break

    print(takahashi)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

PA=0
PB=0

LD=[0]*N
for i in range(N):
    LD[i]=LA[i]+LB[i]

isRunned=True
while(isRunned):
    isRunned=False
    for i in range(N-1):
        if LD[i]<LD[i+1]:
            LD[i],LD[i+1]=LD[i+1],LD[i]
            LA[i],LA[i+1]=LA[i+1],LA[i]
            LB[i],LB[i+1]=LB[i+1],LB[i]
            isRunned=True
        

print(sum(LA[::2])-sum(PB[1::2]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000)

N = int(input())
A = {}
B = {}
ABsa = {}
BAsa = {}
noneqqs = []
cnt = 0
aeqq_cnt = 0
beqq_cnt = 0

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	ABsa[i] = a-b
	BAsa[i] = b-a
	if(a==b):
		cnt += 1
	elif(a!=b):
		noneqqs.append(i)
	if(A[0]==a):
		aeqq_cnt += 1
	if(B[0]==b):
		beqq_cnt += 1

takahashi = 0
aoki = 0
if(cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]

		elif(j%2 == 1):
			maxkey2 = max(B,key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(aeqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(B,key=B.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(B, key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(beqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(A, key=A.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

else:
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(BAsa,key=BAsa.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(ABsa, key=ABsa.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

Submission status: Runtime Error

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
        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:
          B.pop()
      if b1-a1>=b2-a2:
        ans-=b1
        eaten[s]=True
        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: Runtime Error

Solution code:
"""
1. 高橋くんの幸せ順に料理をリスト化する
2. 青木さんの幸せ順に料理をリスト化する
3. 高橋君のリストで最も幸せな料理を選ぶ
4. 同じ幸せ度の料理が複数あれば、最も青木さんの幸せそうな料理を選ぶ
5. 選んだ料理をお互いのリストから除去する
6. 青木さんに選択肢を渡して3に戻る

"""

import pandas as pd

N = int(input())
l = pd.DataFrame([list(map(int, input().split(" "))) for i in range(N)])

who = 0
t = 0
a = 0
for _ in range(N):

    # 3
    choice = l[l[who] == max(l[who])]

    # 4
    if choice.shape[who] > 1:
        choice = choice[choice[1-who] == max(choice[1-who])].head(1)
    # 5
    l = l.drop(choice.index)

    if who == 0:
        t += max(choice[who])
    else:
        a += max(choice[who])
    
    who = 1 - who

print(t-a)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [0]*N
B = [0]*N
C = [0]*N

for i in range(N):
#    try:
        A[i],B[i] = map(int,input().split())
        C.[i] = A[i]+B[i]
#    except parserError:
#        print(-2999999997)
#        exit

Takahashi = 0
Aoki = 0

for i in range(N):
    if i % 2 == 0 :
        n = C.index(max(C))
        Takahashi += A[n]
        del A[n]
        del B[n]
        del C[n]
    else :
        n = C.index(max(C))
        Aoki += B[n]
        del A[n]
        del B[n]
        del C[n]

print(Takahashi - Aoki)

Submission status: Runtime Error

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
  AB = [int(n) for n in input().split()]
  A.append(AB[0])
  B.append(AB[1])

class S(object):
 
  def __init__(self):
    self.takahashi = 0
    self.aoki = 0
    self.A = A
    self.B = B
  
  def select_dish(self, is_takahashi):
    if is_takahashi:
      ally = self.A
      enemy = self.B
    else:
      ally = self.A
      enemy = self.B
 
    if len(ally) == 1:
      return 0
    
    diff = [enemy[k] - ally[k] for k in range(len(ally))]
    max_indices = [i for i, x in enumerate(diff) if x == m]
    ally_nums = [ally[p] for p in max_indices]
    max_ally_num = max(ally_nums)
 
    return ally.index(max_ally_num)
 
 
  def pick_dish(self, is_takahashi):
    if is_takahashi:
      dish_index = self.select_dish(is_takahashi)
      self.takahashi += self.A[dish_index]
    else:
      dish_index = self.select_dish(is_takahashi)
      self.aoki += self.B[dish_index]
 
    del self.A[dish_index]
    del self.B[dish_index]
 
solve = S()

for i in range(len(A)):
  solve.pick_dish(i % 2 == 0)
 
print(solve.takahashi - solve.aoki)

Submission status: Runtime Error

Solution code:
import numpy as np


def _main():
    N = int(input())
    AB = np.empty((2, N), dtype=np.int64)
    for i in range(N):
        AB[:, i] = list(map(int, input().split()))
    AB_sorted = AB.argsort(axis=-1)
    sorted_tops = [N - 1, N - 1]

    scores = [0, 0]  # 高橋くん, 青木さん
    food_exists = np.ones((N,), dtype=bool)
    turn = 0

    def _search(turn, n, alpha, beta):
        if n >= N:
            return 0

        for eat in range(N):
            if not food_exists[eat]:
                continue

            food_exists[eat] = False
            score = -_search(turn ^ 1, n + 1, -beta, -alpha) + AB[turn, eat]
            food_exists[eat] = True

            if alpha < score:
                alpha = score

        return score

    print(_search(0, 0, -np.inf, np.inf))


if __name__ == '__main__':
    _main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
b = sorted([[a[i][0] + a[i][1], a[i][0]] for i in range(n)])
taka = 0
aoki = 0

for i in range(n):
  if i % 2 == 0:
    taka += b[n - k - i - 1][1]
  else:
    aoki += b[n - k - i - 1][0] - b[n - k - i - 1][1]

print(taka - aoki)

Submission status: Runtime Error

Solution code:
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 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
        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 a2>10000000000:
        if not eaten[A[-1][2]]:
          a2,b0,g=A.pop()
        else:
          B.pop()
      if b1-a1>=b2-a2:
        ans-=b1
        eaten[t]=True
        A.append((a2,b0,g))
        A.append((a1,b2,t))
      else:
        ans+=a2
        eaten[s]=True
        A.append((a2,b0,g))
        B.append((b1,a0,s))
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000000000000000)

N = int(input())
A = {}
B = {}
ABsa = {}
BAsa = {}
noneqqs = []
cnt = 0
aeqq_cnt = 0
beqq_cnt = 0

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	ABsa[i] = a-b
	BAsa[i] = b-a
	if(a==b):
		cnt += 1
	elif(a!=b):
		noneqqs.append(i)
	if(A[0]==a):
		aeqq_cnt += 1
	if(B[0]==b):
		beqq_cnt += 1

takahashi = 0
aoki = 0
if(cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]

		elif(j%2 == 1):
			maxkey2 = max(B,key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(aeqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(B,key=B.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(B, key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(beqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(A, key=A.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

else:
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(BAsa,key=BAsa.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(ABsa, key=ABsa.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

Submission status: Runtime Error

Solution code:
a=input()
N=int(a)
T=0
A=0
a_l=list()
b_l=list()
for i in range(N):
    b=input()
    c=b.split()
    a_l.append(int(c[0]))
    b_l.append(int(c[1]))
count=0
k=len(b_l)
if k==1:
    T+=max(a_l)
while k!=1:
    T+=max(a_l)
    g=max(a_l)
    e=a_l.index(g)
    k=len(b_l)
    del a_l[e]
    del b_l[e]
    if k==1:
        break
    else:
        h=max(b_l)
        A+=h
        f=b_l.index(h)
        del a_l[f]
        del b_l[f]
print(T-A)

Submission status: Runtime Error

Solution code:
a=input()
N=int(a)
T=0
A=0
a_l=list()
b_l=list()
for i in range(N):
    b=input()
    c=b.split()
    a_l.append(int(c[0]))
    b_l.append(int(c[1]))
count=0
while True:
    T+=max(a_l)
    g=max(a_l)
    e=a_l.index(g)
    k=len(b_l)
    del a_l[e]
    del b_l[e]
    if k==1:
        break
    else:
        h=max(b_l)
        A+=h
        f=b_l.index(h)
        del a_l[f]
        del b_l[f]
print(T-A)

Submission status: Runtime Error

Solution code:
import functools
from operator import add

sara = int(input())

takahashi = []
aoki = []

for i in range(sara):
    tmp1, tmp2 = (int(i) for i in input().split(" "))
    takahashi.append(tmp1)
    aoki.append(tmp2)

takahashi_fix = []
aoki_fix = []

for i in range(sara):
    takahashi_fix.append(takahashi[i] - aoki[i])
    aoki_fix.append(aoki[i] - takahashi[i])

results_taka = []
results_ao = []

for i in range(sara):
    if len(results_taka) + len(results_ao) == sara:
        break

    tmp = max(takahashi_fix)
    tmp_index = takahashi_fix.index(tmp)

    results_taka.append(takahashi.pop(tmp_index))
    del aoki_fix[tmp_index]

    if len(results_taka) + len(results_ao) == sara:
        break

    tmp = max(aoki_fix)
    tmp_index = aoki_fix.index(tmp)
    results_ao.append(aoki.pop(tmp_index))
    del takahashi_fix[tmp_index]

print(functools.reduce(add, results_taka) - functools.reduce(add, results_ao))

Submission status: Runtime Error

Solution code:
a=input()
N=int(a)
Takahashi=0
Aoki=0
Takahashi_list=list()
Aoki_list=list()
for i in range(N):
    b=input()
    c=b.split()
    Takahashi_list.append(int(c[0]))
    Aoki_list.append(int(c[1]))
count=0
while True:
    Takahashi+=max(Takahashi_list)
    g=max(Takahashi_list)
    e=Takahashi_list.index(g)
    k=len(Aoki_list)
    del Takahashi_list[e]
    del Aoki_list[e]
    if k==1:
        break
    else:
        h=max(Aoki_list)
        Aoki+=h
        f=Aoki_list.index(h)
        del Takahashi_list[f]
        del Aoki_list[f]
        count+=1
print(Takahashi-Aoki)

Submission status: Runtime Error

Solution code:
n = int(input())
a1, a2 = map(int,input().split())
b1, b2 = map(int,input().split())
c1, c2 = map(int,input().split())
import math
a = [a1, b1, c1]
b = [a2, b2, c2]

def combinationListRecursive(data, r):
  if r == 0 or r > len(data):
    return []

  result = []
  _combinationListRecursive(data, r, 0, [], result)
  return result


def _combinationListRecursive(data, r, start, progress, result):
  if r == 0:
    result.append(progress)
    return

  for i in range(start, len(data)):
    _combinationListRecursive(data, r - 1, i + 1, progress + [data[i]], result)

result = combinationListRecursive(range(n), math.ceil(n/2))
best = 0
for i in result:
    atmp = 0
    btmp = 0
    for k in range(n):
        if k in i:
            atmp += a[k]
        else:
            btmp += b[k]
    if best < atmp - btmp:
        best = atmp - btmp
print(best)

Submission status: Runtime Error

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
    while True:
        if taken_count >= N:
            break
        if not len(As):
            break
        am = heapq.heappop(As)
        while taken[am[2]]:
            am = heapq.heappop(As)

        if not len(Bs):
            ans += (K - am[0])
            break

        bm = heapq.heappop(Bs)
        while taken[bm[2]]:
            bm = heapq.heappop(Bs)
        if am[2] == bm[2]:
            ans += (K - am[0])
            taken[am[2]] = True
            taken_count += 1
            if taken_count < N:
                bm = heapq.heappop(Bs)
                while taken[bm[2]]:
                    bm = heapq.heappop(Bs)
                ans -= (K - bm[0])
                taken[bm[2]] = True
                taken_count += 1
        elif am[0] < bm[0]:
            ans += (K - am[0])
            ans -= (K - bm[0])
            taken[am[2]] = True
            taken[bm[2]] = True
            taken_count += 2
        elif am[0] > bm[0]:
            ans += (K - bm[0])
            ans -= (K - am[0])
            taken[am[2]] = True
            taken[bm[2]] = True
            taken_count += 2

    return ans

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

Submission status: Runtime Error

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
    while True:
        if taken_count >= N:
            break
        if not len(As):
            break
        am = heapq.heappop(As)
        while taken[am[2]]:
            am = heapq.heappop(As)

        if not len(Bs):
            ans += (K - am[0])
            break

        bm = heapq.heappop(Bs)
        while taken[bm[2]]:
            bm = heapq.heappop(Bs)
        if am[2] == bm[2]:
            ans += (K - am[0])
            taken[am[2]] = True
            taken_count += 1
            if taken_count < N:
                bm = heapq.heappop(Bs)
                while taken[bm[2]]:
                    bm = heapq.heappop(Bs)
                ans -= (K - bm[0])
                taken[bm[2]] = True
                taken_count += 1
        elif am[0] > bm[0]:
            ans += (K - am[0])
            ans -= (K - bm[0])
            taken[am[2]] = True
            taken[bm[2]] = True
            taken_count += 2
        elif am[0] < bm[0]:
            ans += (K - bm[0])
            ans -= (K - am[0])
            taken[am[2]] = True
            taken[bm[2]] = True
            taken_count += 2

    return ans

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
b=[]
c=[]
for i in range(n):
    s=input().split()
    A,B=int(s[0]),int(s[1])
    a.append(A)
    b.append(B)
    c.append(A+B)
print(n,a,b,c,d)
c.sort(reverse=True)
s1=0
s2=0
for i in range(n):
    s1+=a[i]
    if i%2:
        s2+=c[i]
print(s1-s2)

Submission status: Runtime Error

Solution code:
n = int(input())
res_tak = [0 for i in range(n)]
res_she = [0 for i in range(n)]
for j in range(n):
    a, b = (int(i) for i in input().split())
    res_tak[j] = a
    res_she[j] = b
tak = 0
she = 0
for i in range(n):
    if i & 1 == 1:
        she += max(res_she)
        j=0
        while res_she[j]==max(res_she):
            j+=1
        res_she[j] = 0
        res_tak[j] = 0
    else:
        tak += max(res_tak)
        j=0
        while res_tak[j]==max(res_tak):
            j+=1
        res_she[j] = 0
        res_tak[j] = 0


print(abs(she-tak))

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
Nlist=[]
Takalist=[]
Aokilist=[]
for n in range(N):
    Nlist.append(n)
    a,b = map(int,input().split())
    Takalist.append(a)
    Aokilist.append(b)
Takalist = np.araay(Takalist)
Aokilist = np.array(Aokilist)
Taka = 0
Aoki = 0

flg = 0
for n in range(N):
    if flg == 0:
        big = max(Takalist)
        Taka += big
        num = Takalist.index(big)
        Aokilist[num]=0
        Takalist[num]=0
        flg=1

    elif flg == 1:
        big = max(Aokilist)
        Aoki += big
        num = Aokilist.index(big)
        Aokilist[num]=0
        Takalist[num]=0
        flg=0

print(Taka-Aoki)

Submission status: Runtime Error

Solution code:
N = int(input())
AB = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
    AB.append([a,b])
    
acount = 0
bcount = 0
for i in range(N):
    if max(A) >= max(B):
        AA = max(A)
        indexes = [j for j, x in enumerate(A) if x == AA]
        L = len(indexes)
        ab = []
        MAX = 0
        for k in range(L):
            if MAX <= B[indexes[k]]:
                MAX = B[indexes[k]]
                Index = indexes[k]
        
        if i%2 == 0:
            acount+=A.pop(Index)
            trash = B.pop(Index)
        else:
            bcount+=B.pop(Index)
            trash = A.pop(Index)
    else:
        BB = max(B)
        indexes = [j for j, x in enumerate(B) if x == BB]
        L = len(indexes)
        ab = []
        MAX = 0
        for k in range(L):
            if MAX <= A[indexes[k]]:
                MAX = A[indexes[k]]
                Index = indexes[k]
        
        if i%2 == 0:
            acount+=A.pop(Index)
            trash = B.pop(Index)
        else:
            bcount+=B.pop(Index)
            trash = A.pop(Index)
print(acount-bcount)

Submission status: Runtime Error

Solution code:
import math
import copy
from dask.dataframe.tests.test_rolling import idx


def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input())
a=nCast(inputArr1(n))

b=copy.deepcopy(a)


def akey(a):
    return a[0],a[1]
def bkey(a):
    return a[1],a[0]
list.sort(a,key=akey,reverse=True)
list.sort(b,key=bkey,reverse=True)


def func(a,b):
    pa,pb=0,0
    tag=True
    while len(a)!=0:
        if a[0]==b[0]:
            tmp=a[0]
        else:
            v1=a[0][0]-b[0][1]
            v2=b[0][0]-b[1][1]
            if v1>v2:
                tmp=a[0]
            else:
                tmp=b[0]
        if tag:
            pa+=tmp[0]
        else:
            pb+=tmp[1]
        tag=not tag
        a,b=b,a
        a.remove(tmp)
        b.remove(tmp)
    return pa-pb
    
print(func(a,b))

Submission status: Runtime Error

Solution code:
import heapq


def solve(N, ABs):
    # print(N, ",", ABs)
    taken = {i: False for i in range(N)}
    As = [(ab[0], ab[1], i) for i, ab in enumerate(ABs)]
    Bs = [(ab[1], ab[0], i) for i, ab in enumerate(ABs)]
    heapq.heapify(As)
    heapq.heapify(Bs)
    ans = 0
    taken_count = 0
    while True:
        if taken_count >= N:
            break
        if not len(As):
            break
        am = As.pop(len(As) - 1)
        while taken[am[2]]:
            am = As.pop(len(As) - 1)

        if not len(Bs):
            ans += am[2]
            break

        bm = Bs.pop(len(Bs) - 1)
        while taken[bm[2]]:
            bm = Bs.pop(len(Bs) - 1)
        if am[2] == bm[2]:
            ans += am[0]
            taken[am[2]] = True
            taken_count += 1
            if len(Bs):
                bm = Bs.pop(len(Bs) - 1)
                while taken[bm[2]]:
                    bm = Bs.pop(len(Bs) - 1)
                ans -= bm[0]
                taken[bm[2]] = True
                taken_count += 1
        elif am[0] > bm[0]:
            ans += am[0]
            ans -= bm[0]
            taken[am[2]] = True
            taken[bm[2]] = True
            taken_count += 2
        elif am[0] < bm[0]:
            ans += bm[0]
            ans -= am[0]
            taken[am[2]] = True
            taken[bm[2]] = True
            taken_count += 2

    return ans

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

Submission status: Runtime Error

Solution code:
N = int(input())
AB = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
    AB.append([a,b])
    
acount = 0
bcount = 0
for i in range(N):
    if max(A) >= max(B):
        indexes = [j for j, x in enumerate(A) if x == max(A)]
        L = len(indexes)
        ab = []
        MAX = 0
        for k in range(L):
            if MAX <= B[indexes[k]]:
                MAX = B[indexes[k]]
                Index = indexes[k]
        
        if i%2 == 0:
            acount+=A.pop(Index)
            trash = B.pop(Index)
        else:
            bcount+=B.pop(Index)
            trash = A.pop(Index)
    else:
        indexes = [j for j, x in enumerate(B) if x == max(B)]
        L = len(indexes)
        ab = []
        MAX = 0
        for k in range(L):
            if MAX <= A[indexes[k]]:
                MAX = A[indexes[k]]
                Index = indexes[k]
        
        if i%2 == 0:
            acount+=A.pop(Index)
            trash = B.pop(Index)
        else:
            bcount+=B.pop(Index)
            trash = A.pop(Index)
print(acount-bcount)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
b=[]
c=[]
d=[]
for i in range(n):
    A,B=int(input().split()[0]),int(input().split()[1])
    a.append(A)
    b.append(B)
    c.append(A+B)
    d.append(i)
d.sort(key=lambda x:c[x],reverse='True')
s1=0
s2=0
for i in range(n):
    s1+=a[i]
    if i%2==0:
        s2+=c[d[i]]
print(s1-s2)

Submission status: Runtime Error

Solution code:
N=int(input())

Takahashi=[]
Aoki=[]

for i in range(N):
  A,B=map(int,input().split())
  if A>B:
    symbol='p'
  else:
    symbol='m'
  Takahashi.append((abs(A-B),B,i,A))
  Aoki.append((abs(B-A),A,i,B))

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

#print(Takahashi)

turn=0
Asum=0
Bsum=0
while Takahashi:
  if turn%2==0:
    Asum+=Takahashi[0][3]
    index=Takahashi[0][2]
    Takahashi.pop(0)
    Aoki=[x for x in Aoki if x in Takahashi] 
  else:
    Bsum+=Aoki[0][3]
    index=Aoki[0][2]
    Aoki.pop(0)
    Takahashi=[x for x in Takahashi if x in Aoki]
    
    
  turn+=1
  #print(Takahashi)
  #print(Aoki)
print(Asum-Bsum)

Submission status: Runtime Error

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

li = sorted(box, key = lambda x: sum(x), reverse = True)

taka = []
aoki = []

for i in range(len(li)):
    taka.append(li[0][0])
    del li[0]
    if li == []:
        print(sum(taka) - sum(aoki))
        quit()
    aoki.append(li[0][1])
    del li[0]

print(sum(taka) - sum(aoki))