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: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = []
B = []
for _ in range(N):
    s = input().split()
    A.append(int(s[0]))
    B.append(int(s[1]))

def list_add(in1, in2):
    wrk = np.array(in1) + np.array(in2)
    return wrk.tolist()

C = list_add(A, B)

ind = 0

a_happy = 0
b_happy = 0

for i in range(N):
    if i%2 == 0:
        ind = C.index(max(C))
        a_happy += A[ind]
        A.pop(ind)
        B.pop(ind)
        C.pop(ind)
    if i%2 == 1:
        ind = C.index(max(C))
        b_happy += B[ind]
        A.pop(ind)
        B.pop(ind)
        C.pop(ind)

print(a_happy - b_happy)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N=int(input())
A=np.array(input().split(" ")).astype(int)
for s in range(N-1):
    B=np.array(input().split(" ")).astype(int)
    A=np.r_[A,B]
A=A.reshape(N,-1)
SUM=np.sum(A,axis=1)
SUM=sorted(SUM,reverse=True)
SUB=np.sum(A[:,1])
ans=0
for s in range(0,N,2):
    ans+=SUM[s]
    
print(ans-SUB)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

Plate = []

Point = []

Taka  = 0

Aok   = 0

for n in range(N):
	a , b = list(map( int, input().split()))
	Plate.append([a , b ])
#print(Plate)

for p in Plate:
	Point.append(p[0] + p[1])
#print(Point)

for j in range(N//2+1):
	m = 0
	for k in range(N - 2*j):
		if Point[m] < Point[k]:
			m = k
	Taka += int(Plate[m][0])
	Plate.pop(k)
	Point.pop(k)
#	print("Takahashi's point = " + str(Taka))
#	print("Plate = " + str(Plate))
#	print("Point = " + str(Point))
	
	if Plate !=[]:
		m = 0
		for k in range(N-2*j-1):
			if Point[m] < Point[k]:
				m = k
		Aok += int(Plate[m][1])
		Plate.pop(k)
		Point.pop(k)
#	print("Aoki's point = " + str(Aok))
#	print("Plate = " + str(Plate))
#	print("Point = " + str(Point))
	if N == 2*j + 2:
		break


#print("Takahashi's point = " + str(Taka))
#print("Aoki's      point = " + str(Aok) )

print(Taka -Aok)

Submission status: Time Limit Exceeded

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

def max1(L, p):
  a = L[0][p]
  ix = 0
  for i in range(1, len(L)):
    if a > L[i][p]:
      a = L[i][p]
      ix = i
  return ix

for i in range(N):
  j = i % 2
  idx = max1(A, j)
  a[j] += A[idx][j]
  del A[idx]

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    A = []
    B = []
    C = []
    for _ in range(n):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)
        C.append(a + b)
    ans = 0
    isA = True
    while C:
        index = C.index(max(C))
        C.pop(index)
        if isA:
            ans += A[index]
        else:
            ans -= B[index]
        isA = not isA
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections


def r(n, ab):

    s = [(x[0] + x[1])/2 for x in ab]
    
    scr = 0
    sente = True
    for _ in range(n):
        i = s.index(max(s))
        if sente:
            scr += ab[i][0]
        else:
            scr -= ab[i][1]
        del ab[i]
        del s[i]
        sente = not sente

    return scr

def main():
    input = sys.stdin.readline
    n = int(input())
    ab = [tuple(map(int, input().split())) for _ in range(n)]
    print(r(n, ab))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def dif(n,ab):
    a=[int(i[0]) for i in ab]
    b=[int(i[1]) for i in ab]
    tp=0
    ap=0
    for i in range(n):
        tm=max(a)
        am=max(b)
        if tm>am:
            

            b[a.index(tm)]=0
            a[a.index(tm)]=0
            tp+=tm
        else:
            tp+=a[b.index(am)]
            a[b.index(am)]=0
            b[b.index(am)]=0
        am=max(b)
        a[b.index(am)]=0
        b[b.index(am)]=0
        ap+=am

    return tp-ap

n=int(input())
print(dif(n,[input().split() for i in range(n)]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
a = np.arange((n))
b = np.arange((n))

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

#######

diff = np.arange((n))
for i in range(n):
    diff[i] = a[i] - b[i]


a_score = 0
b_score = 0
for i in range(n):

    if (i % 2 == 0):
        array = (np.argsort(diff))
        a_score += a[array[i]]
    else:
        array = (np.argsort(diff))[::-1]
        b_score += b[array[i]]

print(a_score - b_score)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li_a = []
li_b = []
a_score = b_score = 0

counter = 0

for i in range(n):
    inputs = [int(i) for i in input().split()]
    li_a.append(inputs[0])
    li_b.append(inputs[1])

while True:
    # Turn A
    max_a_index = li_a.index(max(li_a))
    max_b_index = li_b.index(max(li_b))
    if li_b[max_a_index] == max(li_b):
        a_score += li_a.pop(max_a_index)
        li_b.pop(max_a_index)
    elif li_a[max_b_index] == max(li_a):
        a_score += li_a.pop(max_b_index)
        li_b.pop(max_b_index)
    else:
        li_c = [li_a[j] - li_b[j] for j in range(len(li_a))]
        a_score += li_a.pop(li_c.index(min(li_c)))
        li_b.pop(li_c.index(min(li_c)))

    # 終了判定
    counter += 1
    if counter == n:
        break

    # Turn B
    max_a_index = li_a.index(max(li_a))
    max_b_index = li_b.index(max(li_b))
    if li_a[max_b_index] == max(li_a):
        b_score += li_b.pop(max_b_index)
        li_a.pop(max_b_index)
    elif li_b[max_a_index] == max(li_b):
        b_score += li_b.pop(max_a_index)
        li_a.pop(max_a_index)
    else:
        li_c = [li_a[j] - li_b[j] for j in range(len(li_a))]
        b_score += li_b.pop(li_c.index(max(li_c)))
        li_a.pop(li_c.index(max(li_c)))
    # 終了判定
    counter += 1
    if counter == n:
        break
print(a_score - b_score)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = {x: 0 for x in range(N)}
B = {x: 0 for x in range(N)}
n_set = {x for x in range(N)}
total = 0
for i in range(N):
    a, b = list(map(int, input().split()))
    A[i] = a
    B[i] = b
A_list = sorted(A.items(), key=lambda x: x[1], reverse=True)
B_list = sorted(B.items(), key=lambda x: x[1], reverse=True)
def pick(P, Q, n_set):
    a = -1000000000
    t = 0
    max = 0
    for q in Q:
        i, value = q
        if i in n_set:
            max = value
            break
    sub = [x - max for i, x in P]
    for i, s in enumerate(sub):
        if s >= a and P[i][0] in n_set:
            a = s
            t = i
    return t
while n_set:
    x = pick(A_list, B_list, n_set)
    total += A_list[x][1]
    n_set.discard(A_list[x][0])
    if not n_set:
        break
    x = pick(B_list, A_list, n_set)
    total -= B_list[x][1]
    n_set.discard(B_list[x][0])
print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

def parse(inp):
    N=int(next(inp))
    ryouri=np.array(np.array([next(inp).split()],dtype=np.int64))
    for i in range(N-1):
        ryouri=np.append(ryouri,np.array([next(inp).split()],dtype=int),axis=0)
    ryouri=ryouri.transpose()
#    print(ryouri)
    
    return N,ryouri
  
def main(inp):
    N,ryouri=parse(inp)
    takahashi=0
    aoki=0
    
    delta=ryouri[0]-ryouri[1]
#    print(delta)
    
    for i in range(N):
        select = int(((-1)**(i+1)+1)/2)
#        print(select)
#         if(select==0):
        kouho=np.where(ryouri[select] == ryouri[select].max())[0]
#         elif(select==1):
#             kouho=np.where(ryouri[select] == ryouri[select].min())[0]

#        print("kouho",kouho)
        
        kouhodelta=np.empty((0,2),dtype=int)
        
        for j in range(np.size(kouho)):
#             print(kouho[j])
#             print(np.array([kouho[j],delta[kouho[j]]]))
            kouhodelta=np.append(kouhodelta,np.array([[kouho[j],delta[kouho[j]]]]),axis=0)
        kouhodelta=kouhodelta.transpose()
#        print("kouhodelta",kouhodelta)
            
        if(select==0):
            mineat=np.argmin(kouhodelta[1])
            eat=kouhodelta[0][mineat]
            takahashi+=ryouri[0][eat]
        if(select==1):
            maxeat=np.argmax(kouhodelta[1])
            eat=kouhodelta[0][maxeat]
            aoki+=ryouri[1][eat]
            
        ryouri=np.delete(ryouri,eat,1)
        delta=np.delete(delta,eat)
#        print("ryouri",ryouri)
#        print("delta",delta)
        
            
#    print(eat,takahashi,aoki)
    return takahashi-aoki
            
print(main(sys.stdin))

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

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)

da=dict()
db=dict()
def f(a):

    return str(a[0]).zfill(20)+str(a[1]).zfill(10)
for idx in range(0,len(a)):
    if f(a[idx]) not in da:
        da[f(a[idx])]=1
    else:
        da[f(a[idx])]+=1
for idx in range(0,len(b)):
    if f(b[idx]) not in db:
        db[f(b[idx])]=1
    else:
        db[f(b[idx])]+=1

def func(a,b):
    pa,pb=0,0
    tag=True
    ida,idb=0,0
    while ida<len(a) and idb<len(b):
        if da[f(a[ida])]==0:
            ida+=1
            continue
        if db[f(b[idb])]==0:
            idb+=1
            continue
        if a[ida]==b[idb] :
            tmp=a[ida]
        else:
            v1=a[0][0]-b[0][1]
            v2=b[0][0]-b[1][1]
            if v1>v2:
                tmp=a[ida]
            else:
                tmp=b[idb]
        if tag:
            pa+=tmp[0]
        else:
            pb+=tmp[1]
        ida+=1
        idb+=1
        da[f(tmp)]-=1
        db[f(tmp)]-=1
        tag=not tag
        a,b,ida,idb=b,a,idb,ida
        
    return pa-pb
    
print(func(a,b))

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

n = int(input())

points = []
a_point = 0
b_point = 0

for i in range(n):
    p_ = input().split()
    p = [int(s) for s in p_]
    points.extend([[p[0], p[1]]])

for i in range(n):
    if i % 2 == 0:
        points.sort(key=itemgetter(0))
        a_point += points[0][0]
        points.pop(0)
    else:
        points.sort(key=itemgetter(1))
        b_point += points[0][1]
        points.pop(0)

print(a_point - b_point)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
abd = sorted([[e[0], e[1], e[0]+e[1]] for e in ab], key=lambda x:x[2], reverse=True)

res = 0
for i in range(n):
    abi = abd.pop(0)
    if i % 2 == 0:
        res += abi[0]
    else:
        res -= abi[1]

print(res)

Submission status: Time Limit Exceeded

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

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

sum_A = 0
sum_B = 0

for i in range(N) :
    if i%2 == 0 :
        if max(A) >= max(B) :
            k = A.index(max(A))
            sum_A += A.pop(k)
            del B[k]
        else :
            k  = B.index(max(B))
            sum_A += A.pop(k)
            del B[k]
    else :
        if max(B) >= max(A) :
            k = B.index(max(B))
            sum_B += B.pop(k)
            del A[k]
        else :
            k = A.index(max(A))
            sum_B += B.pop(k)
            del A[k]

print(sum_A - sum_B)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
S = np.array([[int(i) for i in input().split()] for i in range(N)])
kouho = list(range(N))
score = [0,0]
s = 0
for i in range(N):

    max = [np.argmax(S[:,0]), np.argmax(S[:,1])]

    tmp = S.copy()
    tmp[max[1-s],:] = 0
    next = [np.argmax(tmp[:,0]), np.argmax(tmp[:,1])]

    maxmax = S[max[s],s] - S[max[1-s],1-s]
    maxnext = S[max[1-s],s] - tmp[next[1-s],1-s]

    if maxmax >= maxnext:
        score[s] += S[max[s],s]
        S[max[s],:] = 0
    else:
        score[s] += S[max[1-s],s]
        S[max[1-s],:] = 0

    s = 1-s

scorediff = score[0] - score[1]

print(scorediff)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
ps=[]
#for i in range(N):
#tmp=input()
ps = [list(map(int, input().split())) for _ in range(N)]

    

tg=[]
tp=[]

ag=[]
ap=[]
for p in ps:
  tg.append((p[1]+p[0]))
  tp.append(p[0])
  ag.append((p[0]+p[1]))
  ap.append(p[1])
    
import numpy as np
def is_even(num):
  if num % 2 == 0:
    return True
  else:
    return False
    
T=0
A=0

for i in range(N):
  if is_even(i):
    if tg[np.argmax(tg)]!=0:
      tmpIndex=np.argmax(tg)
      T+=tp.pop(tmpIndex)
      tg.pop(tmpIndex)
      ag.pop(tmpIndex)
      ap.pop(tmpIndex)
    else:
      tmpIndex=np.argmax(tp)
      T+=tp.pop(tmpIndex)
      tg.pop(tmpIndex)
      ag.pop(tmpIndex)
      ap.pop(tmpIndex)
  else:
    if ag[np.argmax(ag)]!=0:
      tmpIndex=np.argmax(ag)
      A+=ap.pop(tmpIndex)
      ag.pop(tmpIndex)
      tg.pop(tmpIndex)
      tp.pop(tmpIndex)
    else:
      tmpIndex=np.argmax(ap)
      A+=ap.pop(tmpIndex)
      ag.pop(tmpIndex)
      tg.pop(tmpIndex)
      tp.pop(tmpIndex)
print(T-A)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = [0] * n
b = [0] * n
for i in range(n):
    tmp = input().split()
    a[i] = int(tmp[0])
    b[i] = int(tmp[1])

a_point = 0
b_point = 0
cnt = 0
while cnt < n:
    #a
    maxIx = [i for i, x in enumerate(a) if x == max(a)]
    if len(maxIx) != 1:
        maxIx = [i for i, x in enumerate(b) if x == max(b)]
    
    a_point = a_point + a[maxIx[0]]
    del a[maxIx[0]]
    del b[maxIx[0]]

    cnt = cnt + 1
    if cnt == n:
        break
    
    #b
    maxIx = [i for i, x in enumerate(b) if x == max(b)]
    if len(maxIx) != 1:
        maxIx = [i for i, x in enumerate(a) if x == max(a)]
    
    b_point = b_point + b[maxIx[0]]
    del a[maxIx[0]]
    del b[maxIx[0]]

    cnt = cnt + 1

print(a_point - b_point)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())  #数値入力
A=[]
B=[]
for i in range(N):
  a,b=map(int, input().split())  #複数数値入力
  A.append(a)
  B.append(b)

A=np.array(A)
B=np.array(B)
# print(A)
# print(B)
A_point=0
B_point=0

count=0

for i in range( int(N/2 + 1) ):
  if (A.max() > B.max() ):
    a_index = int(A.argmax())
    A_point += A[a_index]
    A = np.delete(A, a_index)
    B = np.delete(B, a_index)
  elif (A.max() < B.max() ):
    b_index = int(B.argmax())
    A_point += A[b_index]
    A = np.delete(A, b_index)
    B = np.delete(B, b_index)
  elif (A.max() == B.max() ):
    a_index = int(A.argmax())
    A_point += A[a_index]
    A = np.delete(A, a_index)
    B = np.delete(B, a_index)
  # print("A_point")
  # print(A_point)
  count+=1
  if (N == count):
    break
  if (A.max() > B.max() ):
    a_index = int(A.argmax())
    B_point += B[a_index]
    A = np.delete(A, a_index)
    B = np.delete(B, a_index)
  elif (A.max() < B.max() ):
    b_index = int(B.argmax())
    B_point += B[b_index]
    A = np.delete(A, b_index)
    B = np.delete(B, b_index)
  elif (A.max() == B.max() ):
    b_index = int(B.argmax())
    B_point += B[b_index]
    A = np.delete(A, b_index)
    B = np.delete(B, b_index)
  # print("B_point")
  # print(B_point)
  count+=1
  if (N == count):
    break
# 最大 A,Bの小さい方

print(A_point-B_point)

Submission status: Time Limit Exceeded

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

res = 0
for i in range(n):
    a = max(ab, key=lambda x:x[0])
    b = max(ab, key=lambda x:x[1])
    if max(a) > max(b):
        ind = ab.index(a)
    else:
        ind = ab.index(b)
    abi = ab.pop(ind)
    if i % 2 == 0:
        res += abi[0]
    else:
        res -= abi[1]

print(res)

Submission status: Time Limit Exceeded

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

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]
            isRunned=True
        

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

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
def is_even(num):
  if num % 2 == 0:
    return True
  else:
    return False
  
N=int(input())
ps=[]
for i in range(N):
  tmp=input()
  ps.append([int(tmp.split()[0]),int(tmp.split()[1])])
    

tg=[]
tp=[]

ag=[]
ap=[]
for p in ps:
  tg.append((p[1]+p[0]))
  tp.append(p[0])
  ag.append((p[0]+p[1]))
  ap.append(p[1])
    
    
    
    
T=0
A=0

for i in range(N):
  if is_even(i):
    if tg[np.argmax(tg)]!=0:
      tmpIndex=np.argmax(tg)
      T+=tp.pop(tmpIndex)
      tg.pop(tmpIndex)
      ag.pop(tmpIndex)
      ap.pop(tmpIndex)
    else:
      tmpIndex=np.argmax(tp)
      T+=tp.pop(tmpIndex)
      tg.pop(tmpIndex)
      ag.pop(tmpIndex)
      ap.pop(tmpIndex)
  else:
    if ag[np.argmax(ag)]!=0:
      tmpIndex=np.argmax(ag)
      A+=ap.pop(tmpIndex)
      ag.pop(tmpIndex)
      tg.pop(tmpIndex)
      tp.pop(tmpIndex)
    else:
      tmpIndex=np.argmax(ap)
      A+=ap.pop(tmpIndex)
      ag.pop(tmpIndex)
      tg.pop(tmpIndex)
      tp.pop(tmpIndex)
print(T-A)

Submission status: Time Limit Exceeded

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

A1 = 0
A2 = 0
k = []

for i in range(N):
    L[i].append(i)
    k.append(i)
d = dict(zip(k, L))

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

for i in range(N):
    if len(d) == 0:
        break
    A1 = A1 + d.pop(k0.pop(0)[2])[0]
    if len(d) == 0:
        break
    for ii in range(len(k1)):
        if k1.pop(0)[2] in d:
            A2 = A2 + d.pop(k0.pop(0)[2])[1]
            break

print(A1-A2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = [list(map(int, input().split())) for _ in range(N)]
D.sort(reverse=True)

A=0
B=0
while D!=[]:
  A+=D[0][0]
  D.pop(0)
  if D==[]:
    break
  tmp=max(D, key=lambda x:x[1])
  B+=tmp[1]
  D.remove(tmp)
  
print(A-B)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = {}
for i in range(N):
    a, b = map(int, input().split())
    if not a + b in D:
        D[a + b] = []
    D[a + b].append([a, b])
for k in D:
    D[k] = sorted(D[k], reverse=True)
A = 0
B = 0
c = 0
for i in range(N):
    m = max(D)
    if not i % 2:
        A += D[m][c//2][0]
    else:
        B += D[m][-(c+1)//2][1]
    c += 1
    if c == len(D[m]):
        D.pop(m)
        c = 0
print(A - B)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
tl = []
al = []
dl = []

for i in range(n):
    a, b = (int(i) for i in input().split())
    tl.append(a)
    al.append(b)

ans = 0
for i in range(n//2+1):
    tm = max(tl)
    am = max(al)
    if tm >= am:
        idxl = [i for i, v in enumerate(tl) if v == tm]
        idx = al.index(max([al[i] for i in idxl]))
        ans += tl[idx]
        tl[idx] = 0
        al[idx] = 0
    else:
        idxl = [i for i, v in enumerate(al) if v == am]
        idx = tl.index(max([tl[i] for i in idxl]))
        ans += tl[idx]
        tl[idx] = 0
        al[idx] = 0
        
    tm = max(tl)
    am = max(al)
    if tm >= am:
        idxl = [i for i, v in enumerate(tl) if v == tm]
        idx = al.index(max([al[i] for i in idxl]))
        ans -= al[idx]
        tl[idx] = 0
        al[idx] = 0
    else:
        idxl = [i for i, v in enumerate(al) if v == am]
        idx = tl.index(max([tl[i] for i in idxl]))
        ans -= al[idx]
        tl[idx] = 0
        al[idx] = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np

N = int(input())
aList = []
bList = []
sa =[]
for i in range(N):
  a,b = map(int, stdin.readline().rstrip().split())
  aList.append(a)
  bList.append(b)

score = 0
for i in range (N):
  aIndex=np.argmax(aList)
  bIndex=np.argmax(bList)

  amax=aList[aIndex] 
  bmax=bList[bIndex] 

  if i % 2==0:    
    if amax > bmax:
      score += aList[aIndex]
      aList[aIndex] =0
      bList[aIndex] =0
    else:
      score += aList[bIndex]
      aList[bIndex] =0
      bList[bIndex] =0
  else:
    if amax > bmax:
      score -= bList[aIndex]
      aList[aIndex] =0
      bList[aIndex] =0
    else:
      score -= bList[bIndex]
      aList[bIndex] =0
      bList[bIndex] =0    

print(score)

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *
n = int(input())
a = []
aa = []
b = []
bb = []
for i in range(n):
    x, y = map(int, input().split())
    aa.append([x, y])
    bb.append([y, x])
a = deque(sorted(aa, reverse=True))
b = deque(sorted(bb, reverse=True))
a_ans = 0
b_ans = 0

while(a or b):
    a_av, a_bv = a[0]
    b_bv, b_av = b[0]
    if a_av == b_av and a_bv == b_bv:
        a.popleft()
        b.popleft()
        a_ans += a_av
        if b:
            b_bv, b_av = b[0]
            b_ans += b_bv
            a.remove([b_av, b_bv])
            b.popleft()
    elif a_av - a_bv >= b_bv - b_av:
        a_ans += a_av
        a.popleft()
        b.remove([a_bv, a_av])
        b_ans += b_bv
        b.popleft()
        a.remove([b_av, b_bv])
    else:
        a_ans += b_av
        b.popleft()
        a.remove([b_av, b_bv])
        if b:
            b_bv, b_av = b[0]
            b_ans += b_bv
            b.popleft()
            if a:
                a.remove([b_av, b_bv])
print(a_ans - b_ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
ApB = []

for i in range(N):
	temp1, temp2 = map(int, input().split())
	s = temp1 + temp2
	ApB.append([s, temp1, temp2, i])

ApB.sort()
ApB.reverse()
score = 0
for i in range(N):
	len_ApB = len(ApB)
	temp_ApB = ApB[0][0]
	if i % 2 == 0:
		score += ApB.pop(0)[1]
	else:
		score -= ApB.pop(0)[2]

print(score)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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 = np.array(A, dtype = np.int64)
    self.B = np.array(B, dtype = np.int64)
  
  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 - ally
    max_indices = np.argmax(diff)
    ally_nums = ally[max_indices]
    max_ally_num = np.max(ally_nums)
    a = np.where(ally == max_ally_num)[0][0]
    return a
 
 
  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]

    self.A = np.delete(self.A, dish_index)
    self.B = np.delete(self.B, dish_index)
 
solve = S()

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
ps=[]
for i in range(N):
  tmp=input()
  ps.append([int(tmp.split()[0]),int(tmp.split()[1])])
    

tg=[]
tp=[]

ag=[]
ap=[]
for p in ps:
  tg.append((p[1]+p[0]))
  tp.append(p[0])
  ag.append((p[0]+p[1]))
  ap.append(p[1])
    
    
    
import numpy as np
def is_even(num):
  if num % 2 == 0:
    return True
  else:
    return False
    
import operator
T=0
A=0

for i in range(N):
  if is_even(i):
    if tg[np.argmax(tg)]!=0:
      tmpIndex=np.argmax(tg)
      T+=tp.pop(tmpIndex)
      tg.pop(tmpIndex)
      ag.pop(tmpIndex)
      ap.pop(tmpIndex)
    else:
      tmpIndex=np.argmax(tp)
      T+=tp.pop(tmpIndex)
      tg.pop(tmpIndex)
      ag.pop(tmpIndex)
      ap.pop(tmpIndex)
  else:
    if ag[np.argmax(ag)]!=0:
      tmpIndex=np.argmax(ag)
      A+=ap.pop(tmpIndex)
      ag.pop(tmpIndex)
      tg.pop(tmpIndex)
      tp.pop(tmpIndex)
    else:
      tmpIndex=np.argmax(ap)
      A+=ap.pop(tmpIndex)
      ag.pop(tmpIndex)
      tg.pop(tmpIndex)
      tp.pop(tmpIndex)
print(T-A)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())  #数値入力
A=[]
B=[]
for i in range(N):
  a,b=map(int, input().split())  #複数数値入力
  A.append(a)
  B.append(b)

A=np.array(A)
B=np.array(B)
# print(A)
# print(B)
A_point=0
B_point=0

count=0

for i in range( int(N/2 + 1) ):
  if (max(A) > max(B) ):
    a_index = int(A.argmax())
    A_point += A[a_index]
    A = np.delete(A, a_index)
    B = np.delete(B, a_index)
  elif (max(A) < max(B) ):
    b_index = int(B.argmax())
    A_point += A[b_index]
    A = np.delete(A, b_index)
    B = np.delete(B, b_index)
  elif (max(A) == max(B) ):
    a_index = int(A.argmax())
    A_point += A[a_index]
    A = np.delete(A, a_index)
    B = np.delete(B, a_index)
  # print("A_point")
  # print(A_point)
  count+=1
  if (N == count):
    break
  if (max(A) > max(B) ):
    a_index = int(A.argmax())
    B_point += B[a_index]
    A = np.delete(A, a_index)
    B = np.delete(B, a_index)
  elif (max(A) < max(B) ):
    b_index = int(B.argmax())
    B_point += B[b_index]
    A = np.delete(A, b_index)
    B = np.delete(B, b_index)
  elif (max(A) == max(B) ):
    b_index = int(B.argmax())
    B_point += B[b_index]
    A = np.delete(A, b_index)
    B = np.delete(B, b_index)
  # print("B_point")
  # print(B_point)
  count+=1
  if (N == count):
    break
# 最大 A,Bの小さい方

print(A_point-B_point)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = [list(map(int, input().split())) for _ in range(N)]
D.sort(reverse=True)

A=0
B=0
for i in range(N):
  if i%2==0:
    A+=D[0][0]
    D.pop(0)
  else:
    tmp=max(D, key=lambda x:x[1])
    B+=tmp[1]
    D.remove(tmp)
  
print(A-B)

Submission status: Time Limit Exceeded

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)]
result = 0
for ind, item in enumerate(order):
    if ind%2==0:
        result += earned[item]
    else:
        result -= loss[item]
 
print(result[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AB = [[]]*N

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

AB.sort(reverse=True)

B = []
for ab in AB:
    B.append(ab[1])

point_t = 0
point_a = 0
flg = True
for _ in range(N):
    if flg:
        ab = AB.pop(0)
        b = B.pop(0)
        point_t += ab[0]
        flg = False
    else:
        idx = B.index(max(B))
        ab = AB.pop(idx)
        b = B.pop(idx)
        point_a += ab[1]
        flg = True
print(point_t - point_a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
itemsA = []
itemsB = []
for i in range(0, n):
	a, b = map(int, raw_input().split())
	itemsA.append(a)
	itemsB.append(b)
points = 0
for j in range(0,n):
	maxT = max(itemsA)
	maxTloc = itemsA.index(maxT)
	maxA = max(itemsB)
	maxAloc = itemsB.index(maxA)
	if(j%2 == 0):
		if(maxT >= maxA):
			points += maxT
			itemsA.pop(int(maxTloc))
			itemsB.pop(int(maxTloc))
		elif(maxT < maxA):
			points += itemsA[maxAloc]
			itemsA.pop(int(maxAloc))
			itemsB.pop(int(maxAloc))
	else:
		points -= maxA
		itemsA.pop(maxAloc)
		itemsB.pop(maxAloc)
print(points)