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 bisect
import sys

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    # AB = iml(N)
    takahashi = {}
    aoki = {}
    for i in range(N):
        a = il()
        takahashi[i] = a[0]
        aoki[i] = a[1]
    T = [i[0] for i in sorted(takahashi.items(), key=lambda x: x[1])]
    A = [i[0] for i in sorted(aoki.items(), key=lambda x: x[1])]
    ind = [T.pop()]
    t = takahashi[ind[0]]
    a = 0
    for i in range(1, N):
        if i % 2 == 1:
            while A:
                j = A.pop()
                if j not in ind:
                    break
            a += aoki[j]
        else:
            while T:
                j = T.pop()
                if j not in ind:
                    break
            t += takahashi[j]
        ind.append(j)
    return t - a


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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AB = []
A = []
B = []
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: 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 set_list(es):
    es = es.tolist()
    return len(list(set(es))) == 1

ind = 0

A = np.array(A)
B = np.array(B)

a_happy = 0
b_happy = 0

for i in range(N):
    if i%2 == 0:
        if set_list(A):
            ind = np.argmax(B)
        else:
            ind = np.argmax(A)
        a_happy += A[ind]
        A = np.delete(A, ind)
        B = np.delete(B, ind)
    if i%2 == 1:
        if set_list(B):
            ind = np.argmax(A)
        else:
            ind = np.argmax(B)
        b_happy += B[ind]
        A = np.delete(A, ind)
        B = np.delete(B, ind)

print(a_happy - b_happy)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[]
b=[]
for i in range(n):
    a_i,b_i=list(map(int,input().split(' ')))
    a.append(a_i)
    b.append(b_i)
a_happiness=0
b_happiness=0
while len(a)!=0:
    a_happiness+=max(a)
    a_indexes=[i for i, x in enumerate(a) if x==max(a)]
    b_index=b.index(max([b[i] for i in a_indexes]))
    a.pop(b_index)
    b.pop(b_index) 
    if len(a)==0:
        break
    b_happiness+=max(b)
    b_indexes=[i for i, x in enumerate(b) if x==max(b)]
    a_index=a.index(max(a[i] for i in b_indexes))
    a.pop(a_index)
    b.pop(a_index)
    
print(a_happiness-b_happiness)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dishes = list()
for i in range(0, n):
    a, b = input().split()
    a = int(a)
    b = int(b)
    diff = b - a
    dishes.append([a, b, diff])

takahashi = 0
aoki = 0
diff_flag = True
for i in range(0, n):
    if i % 2 == 0:
        if diff_flag:
            dishes.sort(key=lambda x: x[2], reverse=True)
            j = dishes[0]
            if j[2] == 0:
                diff_flag = False
        if not diff_flag:
            dishes.sort(key=lambda x: x[0], reverse=True)

        j = dishes[0]
        dishes.pop(0)
        takahashi = takahashi + j[0]
    else:
        dishes.sort(key=lambda x: x[1], reverse=True)
        j = dishes[0]
        dishes.pop(0)
        aoki = aoki + j[1]

print(takahashi - aoki)

Submission status: Time Limit Exceeded

Solution code:
def f(a, b, ans, s):
    if len(a) == 0:
        print(ans)
        exit()
    if max(a) >= max(b):
        i = a.index(max(a))
        ans += a[i] * s
        a.pop(i)
        b.pop(i)
    else:
        i = b.index(max(b))
        ans += a[i] * s
        a.pop(i)
        b.pop(i)
    f(b, a, ans, (-1) * s)


def main():
    N = int(input())
    A = []
    B = []
    for _ in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)
    f(A, B, 0, 1)


main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
dish=[]
X_=[]
X=[]
Y=[]
Y_=[]
xp=0
yp=0
for i in range(N):
    A,B=map(int,input().split())
    dish.append([A,B])
    X.append(A)
    Y.append(B)
    X_.append(A-B)
    Y_.append(B-A)
#print(dish)

#print(X_,Y_)

#print(X)
#print(Y)
for i in range(N):
    if i %2==0:
        XM=min(X_)
        a=X_.index(XM)
        xp+=X[a]
        del X_[a]
        del X[a]
        del Y[a]
        del Y_[a]
        
    elif i %2==1:
        YM=max(Y_)
        a=Y_.index(YM)
        yp+=Y[a]
        del X_[a]
        del X[a]
        del Y[a]
        del Y_[a]
        
        
print(xp-yp)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import copy
N = int(input())
T = [list(map(int,input().split()))+[k] for k in range(N)]
A = copy.deepcopy(T)

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

U = [1 for k in range(N)]

ans = 0
l = 0
m = 0
for k in range(N):
    if k%2 == 0:
        while U[T[l][2]] == 0:
            l += 1
        ans += T[l][0]
        U[T[l][2]] = 0
    else:
        m = 0
        while U[A[m][2]] == 0:
            m += 1
        ans -= A[m][1]
        U[A[m][2]] = 0

print(ans)

Submission status: Time Limit Exceeded

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]


SLD=sorted(LD)
SLD.reverse()
if N%2==0:
    for i in range(0,N,2):
        PA+=LA[LD.index(SLD[i])]
        PB+=LB[LD.index(SLD[i+1])]
else:
    for i in range(0,N-1,2):
        PA+=LA[LD.index(SLD[i])]
        PB+=LB[LD.index(SLD[i+1])]

    PA+=LA[LD.index(SLD[N-1])]

print(PA-PB)

Submission status: Time Limit Exceeded

Solution code:
from statistics import mean, median,variance,stdev
import sys
import math
import fractions

def j(q):
    if q==1: print("YES")
    else:print("NO")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

n = ip()                              #入力整数1つ
#n,k= (int(i) for i in input().split())      #入力整数横2つ
#x,y,z = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列

#jの変数はしようできないので注意
a = []
b = []
for i in range(n):
    x,y = (int(i) for i in input().split())
    b.append(y)
    a.append(x)
sum = 0
c = [a,b]
ma = a.index(max(a))
mb = b.index(max(b))
for i in range(n):
    if a[ma] == 0: ma = a.index(max(a))
    if b[mb] == 0: mb = b.index(max(b))
    if b[ma] < b[mb] and a[mb] == a[ma] and i%2==0:
        ma = mb
    if a[ma] > a[mb] and b[mb] == b[ma] and i%2:
        mb = ma
    if i%2==0: de = a[ma]
    else : de = -b[mb]
    sum+=de
    a[ma] = 0
    b[ma] = 0
print(sum)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n = int(input())
r=0
l=[]
ta=0
ao=0
for i in range(0,n):
  a,b = map(int, input().split())
  v=a-b
  l.append([a,b,-v,(b-a)])

for i in range(0,n):
  if i%2==0:
    l=sorted(l, key=lambda x:(x[2],x[0]),reverse=True)
    p=l.pop(0)
    ta=ta+p[0]
  else:
    l=sorted(l, key=lambda x:(x[3],x[1]),reverse=True)
    p=l.pop(0)
    ao=ao+p[1]

print(ta-ao)

Submission status: Time Limit Exceeded

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

A = []
B = []
cntA = 0
cntB = 0

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

sw = 0
for i in range(N):
    if sw == 0:
        idx = 0
        for j in range(len(A)):
            if A[idx] < A[j]:
                idx = j
            elif A[idx] == A[j]:
                if B[idx] < B[j]:
                    idx = j
        sw = 1
        cntA += A[idx]
        del A[idx]
        del B[idx]
    else:
        idx = 0
        for j in range(len(A)):
            if B[idx] < B[j]:
                idx = j
            elif B[idx] == B[j]:
                if A[idx] > A[j]:
                    idx = j
        sw = 0
        cntB += B[idx]
        del A[idx]
        del B[idx]
print("{}".format(cntA-cntB))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
B = []
A_ = []
B_ = []
for i in range(N):
  A_num,B_num = map(int,input().split())
  A.append(A_num)
  B.append(B_num)
  if A_num-B_num == 0:
    A_.append(A_num)
    B_.append(B_num)
  else:
    A_.append(A_num-B_num)
    B_.append(B_num-A_num)

countA = 0
countB = 0
flg = 0
for i in range (N):
  if flg == 0:
    if min(A_) < 0:
      x = A_.index(min(A_))
    else:
      x = A_.index(max(A_))
    countA = countA + A[x]
  else:
    x = B_.index(max(B_))
    countB = countB + B[x]
  A.pop(x)
  B.pop(x)
  A_.pop(x)
  B_.pop(x)
  if flg == 0:
    flg = 1
  else:
    flg = 0

print(countA - countB)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=[]
for i in range(n):
  d.append(list(map(int,input().split())))
a=0
b=0
z=0
A = sorted(d,key=(lambda x : x[1]), reverse=True)
B = sorted(d,key=(lambda x : x[0]), reverse=True)
for j in range(n):
  z+=1
  if z %2==1:
    c=max(A,key=(lambda x : x[0]))
    a += c[0]
    A.remove(c)
    B.remove(c)
  else:
    c=max(B,key=(lambda x : x[1]))
    b += c[1]
    A.remove(c)
    B.remove(c)
print(a-b)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
    c,d = map(int,input().split())
    a.append(c)
    b.append(d)
t = 0
while(1):
    if len(a)==0:
        break
    maxa = -1
    ai = -1
    if b.count(max(b)) == len(b):
        ai = a.index(max(a))
    else:
        for k in [i for i, x in enumerate(b) if x == max(b)]:
            if maxa <= a[k]:
                maxa = a[k]
                ai = k
    t += a[ai]
    del a[ai]
    del b[ai]

    if len(b)==0:
        break
    maxb = -1
    bi = -1
    if a.count(max(a)) == len(a):
        bi = b.index(max(b))
    else:
        for k in [i for i, x in enumerate(a) if x == max(a)]:
            if maxb <= b[k]:
                maxb = b[k]
                bi = k
    t -= b[bi]
    del a[bi]
    del b[bi]
print(t)

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

scores = sorted(scores, reverse=True)
result = 0
for i in range(N):
    if i%2==0:
        result += earned[i]
    else:
        result -= loss[i]

print(result[0])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n =int(input())
a = []
takahashi = 0
aoki = 0
for i in range(n):
 tmp1,tmp2 = (int(i) for i in input().split())
 a.append([tmp1,tmp2])
a2 = sorted(a, key=lambda x: x[0])
a3 = sorted(a, key=lambda x: x[1])
d = deque(a2)
start = 0
for i in range(n):
 if start%2 == 0:
  d = sorted(d, key=lambda x: x[0])
  a,b =d.pop()
  takahashi += a
  #print(d)
 if start %2 == 1:
  d = sorted(d, key=lambda x: x[1])
  a,b = d.pop()
  aoki += b
  #print(d)
 start += 1
print(takahashi -aoki)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
s = []
for i in range(n):
  r = input().split()
  a.append(int(r[0]))
  b.append(int(r[1]))
  s.append(int(r[0]) + int(r[1]))
akoufuku = 0
bkoufuku = 0
for i in range(n):
  nmax = max(s)
  for x in range(n - i):
    if nmax == s[x]:
      idx = x
  if i % 2 == 0:
    akoufuku += a[idx]
  else:
    bkoufuku += b[idx]
  del a[idx]
  del b[idx]
  del s[idx]

print(akoufuku - bkoufuku)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
B = []
dsum = []
for _ in range(N):
    a_tmp,b_tmp = map(int, input().split())
    A.append(a_tmp)
    B.append(b_tmp)
    dsum.append(abs(a_tmp + b_tmp))

#d = [abs(A[i] - B[i]) for i in range(N)]
#dsum = [abs(A[i] + B[i]) for i in range(N)]
A_score = 0
B_score = 0

for i in range(N):
    ind_dsum = dsum.index(max(dsum))
    if i%2 == 0:
        A_score += A.pop(ind_dsum)
        B.pop(ind_dsum)
    else:
        B_score += B.pop(ind_dsum)
        A.pop(ind_dsum)
    dsum.pop(ind_dsum)

print(A_score - B_score)

Submission status: Time Limit Exceeded

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

for i in range(n):
	if i%2 == 0:
		ind = d.index(min(d))
		ans += a[ind]
	else:
		ind = d.index(min(d))
		ans -= b[ind]
	del a[ind]
	del b[ind]
	del d[ind]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = [None] * N
B = [None] * N
L = list(range(N))
F = [False] * N

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

P = 0
Q = 0

T = True

C    = zip(A, B, L)
C    = sorted(C, key=lambda x: [x[0], x[1]], reverse=True)
D    = zip(B, A, L)
D    = sorted(D, key=lambda x: [x[0], x[1]], reverse=True)

C_A, C_B, C_L = map(list, zip(*C))
D_B, D_A, D_L = map(list, zip(*D))

for _ in range(N):
    if T:
        for i in range(len(C_L)):
            if F[C_L[i]] == False:
                P        += C_A[i]
                F[C_L[i]] = True
                T         = False
                del C_A[i]
                del C_L[i]
                break
    else:
        for i in range(len(D_L)):
            if F[D_L[i]] == False:
                Q        += D_B[i]
                F[D_L[i]] = True
                T         = True
                del D_B[i]
                del D_L[i]
                break

print(P-Q)

Submission status: Time Limit Exceeded

Solution code:
def main():
    count = 0
    N = int(input())
    l = []
    for i in range(N):
       l.append(input().split())
    m = sorted(l,key=lambda x:int(x[0])-int(x[1]),reverse=True)
    for i in range(N):
       if not i&1:
         p = m.pop()
         count+=int(p[0])
       else:
        p = m[-1]
        m.remove(m[-1])
        count-=int(p[1])
    print(count)
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = [0 for i in range(N)]
B = [0 for i in range(N)]
C = [0 for i in range(N)]

for i in range(N):
  A[i], B[i] = [int(j) for j in input().split()]
  C[i] = A[i] + B[i]

diff = 0

while len(A) > 0:
  index = C.index(max(C))
  diff += A[index]
  A.pop(index)
  B.pop(index)
  C.pop(index)

  if len(A) == 0:
    break

  index = C.index(max(C))
  diff -= B[index]
  A.pop(index)
  B.pop(index)
  C.pop(index)

print(diff)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
z = []
for i in range(n):
    x, y = [int(x) for x in input().split()]
    z.append([x, y])
    z.sort(reverse=True)

a = []
b = []
for i in range(n // 2):
    a.append(z[2 * i][0])
    b.append(z[2 * i + 1][1])
if n % 2 == 1:
    a.append(z[len(z) - 1][0])

print(sum(a) - sum(b))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

aa = []
for i in range(n):
	k,l = map(int,input().split())
	aa.append([k,l])
aa.sort(key = lambda x:x[1])
aa.reverse()
co = 0
ma = 0
kkk = 0
for i in range(n):
	if i % 2 == 0:
		ma = max(aa,key = lambda x:x[0])[0]
		for j in range(n-i):
			if ma == aa[j][0]:
				co += aa.pop(j)[0]
				break
	else:
		co -= aa.pop(0)[1]

print(co)

Submission status: Time Limit Exceeded

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

d = [abs(A[i] - B[i]) for i in range(N)]
dsum = [abs(A[i] + B[i]) for i in range(N)]
A_score = 0
B_score = 0

for i in range(N):
    ind_dsum = dsum.index(max(dsum))
    if i%2 == 0:
        A_score += A.pop(ind_dsum)
        B.pop(ind_dsum)
    else:
        B_score += B.pop(ind_dsum)
        A.pop(ind_dsum)
    dsum.pop(ind_dsum)

print(A_score - B_score)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dishes = list()
for i in range(0, n):
    a, b = input().split()
    a = int(a)
    b = int(b)
    diff = b - a
    dishes.append([a, b, diff])

takahashi = 0
aoki = 0
for i in range(0, n):
    if i % 2 == 0:
        dishes.sort(key=lambda x: x[2], reverse=True)
        dishes.sort(key=lambda x: x[0], reverse=True)
        j = dishes[0]
        dishes.pop(0)
        takahashi = takahashi + j[0]
    else:
        dishes.sort(key=lambda x: x[1], reverse=True)
        j = dishes[0]
        dishes.pop(0)
        aoki = aoki + j[1]

print(takahashi - aoki)

Submission status: Time Limit Exceeded

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

AA = list(numpy.argsort(A)[::-1])
BB = list(numpy.argsort(B)[::-1])
Al = []
Bl = []
aans,bans = 0,0
for i in range(N):
    a = AA.pop(0)
    aans += A[a]
    Al.append(a)
    BB.remove(a)
    if not BB:
        break
    b = BB.pop(0)
    bans += B[b]
    Bl.append(b)
    AA.remove(b)
    if not AA:
        break

print(aans - bans)

Submission status: Time Limit Exceeded

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

def set_list(es):
    return len(list(set(es))) == 1

ind = 0

a_happy = 0
b_happy = 0

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

print(a_happy - b_happy)

Submission status: Time Limit Exceeded

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

for i in range(n):
    num = c.index(max(c))
    if i % 2 ==0:
        taka = taka + a[num]
    else:
        aoki = aoki + b[num]
    del a[num]
    del b[num]
    del c[num]
print(taka-aoki)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=[]
for i in range(n):
  d.append(list(map(int,input().split())))
a=0
b=0
z=0
for j in range(n):
  z+=1
  if z %2==1:
    D = sorted(d,key=(lambda x : x[1]), reverse=True)
    c=max(D,key=(lambda x : x[0]))
    a += c[0]
    d.remove(c)
  else:
    D = sorted(d,key=(lambda x : x[0]), reverse=True)
    e=max(D,key=(lambda x : x[1]))
    b += e[1]
    d.remove(e)
print(a-b)

Submission status: Time Limit Exceeded

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

a_total = b_total = 0
for i in range(n):
    if i % 2 == 0:
        num = max(a)
        idx = a.index(num)

        for l in range(len(a)):
            if a[l] == num and b[l] > b[idx]:
                idx = l
        a_total += a[idx]

    else:
        num = max(b)
        idx = b.index(num)
        for l in range(len(b)):
            if b[l] == num and a[l] > a[idx]:
                idx = l
        b_total += b[idx]

    a.pop(idx)
    b.pop(idx)

print(a_total - b_total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

aa = []
for i in range(n):
	k,l = map(int,input().split())
	aa.append([k,l])
aa.sort(key = lambda x:x[1])
aa.reverse()
co = 0
ma = 0
kkk = 0
for i in range(n):
	if i % 2 == 0:
		ma = max(aa,key = lambda x:x[0])[0]
		for j in range(len(aa)):
			if ma == aa[j][0]:
				co += aa.pop(j)[0]
				break
	else:
		co -= aa.pop(0)[1]

print(co)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
c = []
for _ in range(n):
  s, t = map(int, input().split())
  a.append(s)
  b.append(t)
  c.append(s + t)

taka = 0
aoki = 0
for i in range(n):
  p = c.index(max(c))
  x = a.pop(p)
  y = b.pop(p)
  z = c.pop(p)
  
  if i % 2 == 0:
    taka += x
  else:
    aoki += y

print(taka - aoki)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

count1 = 0
count2 = 0

def fa():
    if max(dif) > 0 or min(dif) < 0:
        if max(dif) > -min(dif):
            index = dif.index(max(dif))
        else:
            index = dif.index(min(dif))
    else:
        index = A.index(max(A))

    dif.pop(index)
    B.pop(index)
    return A.pop(index)

def fb():
    if max(dif) > 0 or min(dif) < 0:
        if max(dif) < -min(dif):
            index = dif.index(max(dif))
        else:
            index = dif.index(min(dif))
    else:
        index = A.index(max(A))

    dif.pop(index)
    A.pop(index)
    return B.pop(index)

while True:
    count1 += fa()
    if len(A) == 0:
        break
    count2 += fb()
    if len(A) == 0:
        break

print(count1 - count2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A_list = []
B_list = []
dif_list = []
for i in range(N):
    A, B = map(int, input().split())
    A_list.append(A)
    B_list.append(B)
    dif_list.append(B - A)

takahashi_score = 0
aoki_score = 0
for i in range(N):
    if len(dif_list) != 0:
        dish_index = dif_list.index(max(dif_list))
        score = A_list[dish_index]
        takahashi_score += score

        del A_list[dish_index]
        del B_list[dish_index]
        del dif_list[dish_index]

    if len(dif_list) != 0:
        dish_index = dif_list.index(max(dif_list))
        score = B_list[dish_index]
        aoki_score += score

        del A_list[dish_index]
        del B_list[dish_index]
        del dif_list[dish_index]

print(takahashi_score - aoki_score)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
taka = 0
aoki = 0
while (1):
    max_list = []
    max_list = [i for i, x in enumerate(a) if x == max(a)]
    max_val = 0
    max_index = 0
    for i in max_list:
        if(b[i] > max_val):
            max_index = i
    taka += a[max_index]
    b.pop(max_index)
    a.pop(max_index)
    if(len(a) == 0):
        print(taka - aoki)
        exit()
    max_list = []
    max_list = [i for i, x in enumerate(b) if x == max(b)]
    max_val = 0
    max_index = 0
    for i in max_list:
        if(b[i] > max_val):
            max_index = i
    aoki += b[max_index]
    a.pop(max_index)
    b.pop(max_index)
    if(len(a) == 0):
        print(taka - aoki)
        exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
    c,d = map(int,input().split())
    a.append(c)
    b.append(d)
t = 0
while(1):
    if len(a)==0:
        break
    
    maxa = -1
    ai = -1
    for k in [i for i, x in enumerate(b) if x == max(b)]:
        if maxa <= a[k]:
            maxa = a[k]
            ai = k
    t += maxa
    del a[ai]
    del b[ai]

    if len(b)==0:
        break
    maxb = -1
    bi = -1
    for k in [i for i, x in enumerate(a) if x == max(a)]:
        if maxb <= b[k]:
            maxb = b[k]
            bi = k
    t -= maxb
    del a[bi]
    del b[bi]
print(t)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a,b = [],[]
for _ in range(N):
    ai,bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
    
aoki = []
taka = []
rest = set([i for i in range(N)])
for i in reversed(range(N)):
    if i%2==1: # aoki
        m = 1e10
        for p in rest:
            if b[p]<m:
                m = b[p]
                idx = p
        aoki.append(m)
        rest.remove(idx)
    else:
        m = 1e10
        for p in rest:
            if a[p]<m:
                m = a[p]
                idx = p
        taka.append(m)
        rest.remove(idx)
    #print(rest)
    #print(aoki, taka)
print(sum(taka)-sum(aoki))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ab = [list(map(int,input().split())) + [0] for i in range(n)]
for i in ab:
    i[2] = abs(i[0] - i[1])
ab.sort(key=lambda ab:ab[2])
ba = sorted(ab,key=lambda ab:ab[1])
ans = 0
for i in range(n):
    ab.sort(key=lambda ab:ab[2])
    if i % 2 ==0:
        ans += ab[-1][0]
        ba.remove(ab[-1])
        ab.pop()
    else:
        ans -= ba[-1][1]
        ab.remove(ba[-1])
        ba.pop()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
count = 0
N = int(input())
l = []
for i in range(N):
    l.append([int(x) for x in input().split()])
m = sorted(l,key = lambda x:x[0]-x[1],reverse=True)
for i in range(N):
    if i&1==0:
        p = m.pop()
        count+=p[0]
    else:
        p = m[-1]
        m.remove(m[-1])
        count-=p[1]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    x, y = [int(x) for x in input().split()]
    a.append([x, y])
    a.sort(reverse=True)

b = 0
for i in range(n // 2):
    b += a[2 * i][0] - a[2 * i + 1][1] 
if n % 2 == 1:
    b += a[len(a) - 1][0]

print(b)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = [list(map(int,input().split()))+[k] for k in range(N)]

T = sorted(D, key = lambda x: (-x[0],-x[1]))
A = sorted(D, key = lambda x: (-x[1],-x[0]))
U = [1 for k in range(N)]

ans = 0
l = 0
m = 0
for k in range(N):
    if k%2 == 0:
        while U[T[l][2]] == 0:
            l += 1
        ans += T[l][0]
        U[T[l][2]] = 0
    else:
        m = 0
        while U[A[m][2]] == 0:
            m += 1
        ans -= A[m][1]
        U[A[m][2]] = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


N = int(input())

A = [None] * N
B = [None] * N

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

P = 0
Q = 0

F = True

for i in range(N):
    if F:
        C    = zip(A, B)
        C    = sorted(C, key=lambda x: [x[0], x[1]], reverse=True)
        A, B = map(list, zip(*C))
        P   += A[0]
        A.pop(0)
        B.pop(0)
        F = False
    else:
        D    = zip(B, A)
        D    = sorted(D, key=lambda x: [x[0], x[1]], reverse=True)
        B, A = map(list, zip(*D))
        Q   += B[0]
        A.pop(0)
        B.pop(0)
        F = True

print(P-Q)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
import numpy as np
n = int(input())

a = np.array([list(map(int,input().split())) for _ in range(n)])

def t(a):
    if len(a) == 1:
        return a[0][0]
    x = a[np.argmin(a[:,0]*2-a[:,1])]
    a = np.delete(a,[np.argmin(a[:,0]-a[:,1])],0)
    return ao(a) + x[0]

def ao(a):
    if len(a) == 1:
        return -a[0][1]
    x = a[np.argmin(a[:,0]*2-a[:,1])]
    a = np.delete(a,[np.argmin(a[:,0]-a[:,1])],0)
    return t(a) - x[1]

print(t(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Alist = []
Blist = []
sumlist = []

for i in range(N):
    a,b = map(int,input().split())
    Alist.append(a)
    Blist.append(b)
    sumlist.append(a+b)

counter = 0
takahasi = 0
aoki = 0
for i in range(N):
    if counter == 0:
        index = sumlist.index(max(sumlist))
        takahasi += Alist[index]
        sumlist[index] = 0
        counter = 1
        continue
    if counter == 1:
        index = sumlist.index(max(sumlist))
        aoki += Blist[index]
        sumlist[index] = 0
        counter = 0
        continue

print(takahasi-aoki)