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:
n=int(input())
s=[]
t=[]
for i in range(n):
    a,b=map(int,input().split())
    s.append([a,b,i])
    t.append([b,a,i])
s.sort()
t.sort()
r=0
q=0
p=[]
for i in range(n):
    if i%2!=1:
        while s[-1][2] in p:
            s.pop(-1)
        else:
            r+=s[-1][0]
            p.append(s[-1][2])
    else:
        while t[-1][2] in p:
            t.pop(-1)
        else:
            q+=t[-1][0]
            p.append(t[-1][2])
print(r-q)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 27 20:47:32 2019

@author: Owner
"""


import sys
import numpy as np

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""

N = int(input())

A = [0]*N
B = [0]*N
dA = [0]*N
dB = [0]*N
totAB = [0]*N
for n in range(N):
    A[n], B[n] = map(int, input().split())
    totAB[n] = A[n] + B[n]
    
n = 0
result = 0
while len(A) != 0:
    iT = totAB.index(max(totAB))
    if n % 2 == 0:
        result += A.pop(iT)
        del B[iT]
        del totAB[iT]
    else:
        result -= B.pop(iT)
        del A[iT]
        del totAB[iT]
    n +=1    
    
print(result)

Submission status: Time Limit Exceeded

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

Solution code:
n = int(input())

ryouri = [list(map(int, input().split())) for _ in range(n)]
score_t = 0
score_a = 0

for i in range(n):
    if i % 2 == 0:
        ryouri = sorted(ryouri, key=lambda x: ((x[0] - x[1]), x[0]*(-1)))
        score_t += ryouri[0][0]
        ryouri.pop(0)
    else:
        ryouri = sorted(ryouri, key=lambda x: (x[1]*(-1), x[1] - x[0]))
        score_a += ryouri[0][1]
        ryouri.pop(0)
        
print(score_t - score_a)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def main():
    N = int(input())
    A = np.array([])
    B = np.array([])
    for i in range(0, N):
        a, b = map(int, input().split())
        A = np.append(A, [a])
        B = np.append(B, [b])
    
    takahashi = 0
    aoki = 0
    # 足し算して一番大きいところからとる
    sum_AB = A + B
    l = sum_AB.argsort()[::-1]
    for idx, i in enumerate(l):
        if idx % 2 == 0:
            takahashi += A[i]
        else:
            aoki += B[i]

    print(int(takahashi - aoki))


main()

Submission status: Time Limit Exceeded

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

a = 0
for i in range(1,n+1):
    if i % 2 != 0:
        ab = sorted(ab, key=lambda x : (x[1],x[0]), reverse = True)
        a += ab[0][0]
    else:
        ab = sorted(ab, key=lambda x : (x[0],x[1]), reverse = True)
        a -= ab[0][1]
    del ab[0]
print(a)

Submission status: Time Limit Exceeded

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))]
    m = max(diff)
    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: Time Limit Exceeded

Solution code:
import numpy as np

def choose(A,B):
	Diff=[]
	for i,a in enumerate(A):
		tempB = B[:i]+B[i+1:]
		Diff.append(a - max(tempB))
	return np.argmax(Diff)

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

scoreA=0
scoreB=0

while 1:
	if len(A)==1:
		scoreA += A.pop(0)
		B.pop(0)
		break

	idx = choose(A,B)
	scoreA += A.pop(idx)
	B.pop(idx)

	if len(B)==1:
		scoreB += B.pop(0)
		A.pop(0)
		break

	idx = choose(B,A)
	scoreB += B.pop(idx)
	A.pop(idx)

print(scoreA-scoreB)

exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
tkh = list()
a_b = list()
for i in range(n):
    a,b = input().split()
    a = int(a)
    b= int(b)
    a_b.append([a,b])
    tkh.append([a-b,a,b,i])
aok = tkh.copy()

tkh.sort(key = lambda x : x[2])
tkh.sort(key = lambda x : x[1])
tkh.sort(key = lambda x : -x[0])

aok.sort(key = lambda x : x[1])
aok.sort(key = lambda x : x[2])
aok.sort(key = lambda x : -x[0])
takahashi, aoki, =0,0
count =0
while a_b != [0]*n:
    if count%2 == 0:
        tmp = tkh.pop()[3]
        if a_b[tmp] != 0:
            takahashi += a_b[tmp][0]
            a_b[tmp] = 0
            count +=1

    else:
        tmp = aok.pop()[3]
        if a_b[tmp] != 0:
            aoki += a_b[tmp][1]
            a_b[tmp] = 0
            count += 1

print(takahashi-aoki)

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]

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
        
    
for i in range(N):
    if i%2==0:
        PA+=LA[i]
    else:
        PB+=LB[i]

print(PA-PB)

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])
A = 0
B = 0
for i in range(N):
    index = 0
    m = 0
    for j in range(len(D[max(D)])):
        if m < D[max(D)][j][i % 2]:
            index = j
            m = D[max(D)][j][i % 2]
    if not i % 2:
        A += m
    else:
        B += m
    D[max(D)].pop(index)
    if len(D[max(D)]) == 0:
        D.pop(max(D))
print(A - B)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
AB = [list(map(int, input().split())) for i in range(N)]

ab = np.array([x[0]-x[1] for x in AB])
sorted_ab = np.array(sorted(ab))
AB = np.array(AB)

Takahashi = 0
Aoki = 0
for i in range(N):
    if i % 2 == 0:
        t = np.where(ab == sorted_ab[0])
        T = np.array([AB[i][0] for i in list(t)[0]])
        T1 = np.where(T == max(T))
        t = list(t)[0][[list(T1)[0][0]][0]]
        Takahashi += AB[t][0]
        ab = np.delete(ab, t)
        sorted_ab = np.delete(sorted_ab, 0)
        AB = np.delete(AB, [t], axis=0)
    else:
        a = np.where(ab == sorted_ab[0])
        A = np.array([AB[i][1] for i in list(a)[0]])
        A1 = np.where(A == max(A))
        a = list(a)[0][[list(A1)[0][0]][0]]
        Aoki += AB[a][1]
        ab = np.delete(ab, a)
        sorted_ab = np.delete(sorted_ab, 0)
        AB = np.delete(AB, [a], axis=0)


print(Takahashi-Aoki)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

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

ab = sorted(ab, key=itemgetter(0,1), reverse=True)
total = 0
for i in range(n):
    if i % 2 == 0:
        total += ab[0][0]
        ab.pop(0)
    else:
        ab = sorted(ab, key=itemgetter(1), reverse=True)
        idx = 0
        for l in range(len(ab)):
            if ab[l][1] < ab[idx][1]:
                break
            elif ab[l][0] < ab[idx][0]:
                idx = l
        total -= ab[idx][1]
        ab.pop(idx)
        
print(total)

Submission status: Time Limit Exceeded

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

sa = 0
sb = 0
for i in range(n):
    if i % 2 == 0:
        ab.sort(reverse=True)
        sa += ab[0][0]
        ab.pop(0)
    else:
        ab = sorted(ab, key=lambda x: x[1], reverse=True)
        sb += ab[0][1]
        ab.pop(0)
        
print(sa-sb)

Submission status: Time Limit Exceeded

Solution code:
A = []
B = []
C = []

N = int(input())
for i in range(N):
#    try:
        ai,bi = map(int,input().split())
        A.append(ai)
        B.append(bi)
        C.append(bi + ai)
#    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: Time Limit Exceeded

Solution code:
data = input().split()

n = int(data[0])
a=[]
valueList=[]
for i in range(n):
    line = input().split()
    tmp=[]
    tmp.append(int(line[0]))
    tmp.append(int(line[1]))
    a.append(tmp)
    valueList.append(int(line[1]) + int(line[0]))

res=0

for i in range(n):
    ind = valueList.index(max(valueList))
    if(i%2 == 0):
        res += a[ind][0]
    else:
        res -= a[ind][1]
    a.pop(ind)
    valueList.pop(ind)

print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
ab = []
for _ in range(n):
    r = list(map(int, input().split()))
    ab.append(r)

ab = np.array(ab)
takahashi = np.array([])
aoki = np.array([])

while True:
    if len(ab) == 0:
        break
    # 高橋
    ab = ab[np.lexsort((ab[:,0], ab[:,1]))]
    takahashi = np.append(takahashi,ab[len(ab)-1,0])
    ab = np.delete(ab,len(ab)-1,0)

    # 青木
    if len(ab) == 0:
        break
    ab = ab[np.lexsort((ab[:,1], ab[:,0]))]
    aoki = np.append(aoki,ab[len(ab)-1,1])
    ab = np.delete(ab,len(ab)-1,0)
    

print(int(takahashi.sum()) - int(aoki.sum()))

Submission status: Time Limit Exceeded

Solution code:


import sys
import numpy as np

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""

N = int(input())

A = [0]*N
B = [0]*N
dA = [0]*N
dB = [0]*N
totAB = [0]*N
for n in range(N):
    A[n], B[n] = map(int, input().split())
    totAB[n] = A[n] + B[n]
    
n = 0
result = 0
while len(A) != 0:
    iT = getNearestValueIndex(totAB, max(totAB))
    if n % 2 == 0:
        result += A.pop(iT)
        del B[iT]
        del totAB[iT]
    else:
        result -= B.pop(iT)
        del A[iT]
        del totAB[iT]
    n +=1    
    
print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
FOOD = [[int(i) for i in input().split()] for j in range(N)]

ans = 0
FOOD.sort()
while(FOOD.__len__() > 1):
    l = FOOD.pop()
    s = max(enumerate(FOOD),key=lambda x: x[1][1])
    FOOD.pop(s[0])
    ans += l[0] - s[1][1]

if FOOD.__len__() == 0:
    print(ans)
else:
    print(ans + FOOD[0][0])

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)
        MAX = 0
        for k in range(L):
            MAX = max(MAX,B[indexes[k]])
            if 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)
        MAX = 0
        for k in range(L):
            MAX = max(MAX,A[indexes[k]])
            if 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:
n = int(input().strip())
array1 = []
array2 = []
for i in range(n):
  inputstr = input().strip()
  array1.append(int(inputstr.split(" ")[0]))
  array2.append(int(inputstr.split(" ")[1]))

s1 = 0
s2 = 0
j = 0
for i in range(n):
  ai = array1.index(max(array1))
  bi = array2.index(max(array2))
  if (array1[ai]-array1[bi]) > (array2[ai]-array2[bi]):
    s1 = s1 + array1[ai]
    array1.pop(ai)
    array2.pop(ai)
  else:
    s1 = s1 + array1[bi]
    array1.pop(bi)
    array2.pop(bi)
  j = j+1
  if j >= n:
    break
  ai = array1.index(max(array1))
  bi = array2.index(max(array2))
  if (array1[ai]-array1[bi]) > (array2[ai]-array2[bi]):
    s2 = s2 + array2[ai]
    array1.pop(bi)
    array2.pop(bi)
  else:
    s2 = s2 + array2[bi]
    array1.pop(ai)
    array2.pop(ai)
  j = j+1
  if j >= n:
    break

print (str(s1-s2))

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:

A = []
B = []
C = []

N = int(input())
for i in range(N):
    try:
        ai,bi = map(int,input().split())
        A.append(ai)
        B.append(bi)
        C.append(bi + ai)
    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: Time Limit Exceeded

Solution code:
n = int(raw_input())

dishes_a = list()
dishes_b = list()
dishes_diff = list()
for i in range(0, n):
    a, b = raw_input().split()
    a = int(a)
    b = int(b)
    diff = b - a
    dishes_a.append(a)
    dishes_b.append(b)
    dishes_diff.append(diff)

takahashi = 0
aoki = 0
index = 0
diff_flag = True

if max(dishes_diff) == 0:
    diff_flag = False

for i in range(0, n):
    if i % 2 == 0:
        if diff_flag and max(dishes_diff) != 0:
            index = dishes_diff.index(max(dishes_diff))
        else:
            diff_flag = False
            index = dishes_a.index(max(dishes_a))

        takahashi = takahashi + dishes_a[index]
        dishes_a.pop(index)
        dishes_b.pop(index)
        dishes_diff.pop(index)
    else:
        index = dishes_b.index(max(dishes_b))
        aoki = aoki + dishes_b[index]
        dishes_a.pop(index)
        dishes_b.pop(index)
        dishes_diff.pop(index)

print(takahashi - aoki)

Submission status: Time Limit Exceeded

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

a, b = 0, 0
for i in range(1,n+1):
    if i % 2 != 0:
        ab = sorted(ab, key=lambda x : (x[1],x[0]), reverse = True)
        a += ab[0][0]
    else:
        ab = sorted(ab, key=lambda x : (x[0],x[1]), reverse = True)
        b += ab[0][1]
    del ab[0]
print(a-b)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
takahashi = []
aoki = []
menus  = []

for i in range(N):
    menu = input().split()
    menus.append([int(menu[0]), int(menu[1]), i])

menus = np.array(menus)
#print(menus)

t_score = 0
a_score = 0

for i in range(N):
    if i % 2 == 0: #高橋くん
        idx = np.argmax(menus, axis=0)
        #print(idx)
        #print(menus[idx[0],0], menus[idx[1],1])
        if menus[idx[0],0] >= menus[idx[1],1]:
            t_score += menus[idx[0],0]
            menus = np.delete(menus, idx[0], 0)
        else:
            t_score += menus[idx[1],0]
            menus = np.delete(menus, idx[1], 0)
    else: #青木さん
        idx = np.argmax(menus, axis=0)
        #print(idx)
        #print(menus[idx[0],0], menus[idx[1],0])
        if menus[idx[1],1] >= menus[idx[0],0]:
            a_score += menus[idx[1],1]
            menus = np.delete(menus, idx[1], 0)
        else:
            a_score += menus[idx[0],1]
            menus = np.delete(menus, idx[1], 0)
    #print(t_score,a_score)
    #print("-----")
print(t_score-a_score)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 27 20:47:32 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""

N = int(input())

A = [0]*N
B = [0]*N
dA = [0]*N
dB = [0]*N
totAB = [0]*N
for n in range(N):
    A[n], B[n] = map(int, input().split())
    totAB[n] = A[n] + B[n]
    
n = 0
result = 0
while len(A) != 0:
    iT = getNearestValueIndex(totAB, max(totAB))
    #print(iT)
    if n % 2 == 0:
        result += A.pop(iT)
        B.pop(iT)
        totAB.pop(iT)
    else:
        result -= B.pop(iT)
        A.pop(iT)
        totAB.pop(iT)
    n +=1    
    
print(result)

Submission status: Time Limit Exceeded

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

LIST = []
cntA = 0
cntB = 0

N = int(input())
for i in range(N):
    X, Y = map(int, input().split())
    LIST.append([X, Y])

sw = 0
for i in range(N):
    if sw == 0:
        idx = 0
        LIST.sort(key=lambda x: x[0], reverse=True)
        for j in range(len(LIST)):
            if LIST[0][0] > LIST[j][0]:
                break
            elif LIST[idx][1] < LIST[j][1]:
                idx = j
        sw = 1
        cntA += LIST[idx][0]
        del LIST[idx]
    else:
        idx = 0
        LIST.sort(key=lambda x: x[1], reverse=True)
        for j in range(len(LIST)):
            if LIST[0][1] > LIST[j][1]:
                break
            elif LIST[idx][0] > LIST[j][0]:
                idx = j
        sw = 0
        cntB += LIST[idx][1]
        del LIST[idx]
print("{}".format(cntA-cntB))

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))
    li_c = [li_a[j] - li_b[j] for j in range(len(li_a))]
    if max(li_a) == li_a[max_b_index] or max(li_b) == li_b[max_a_index]:
        a_score += max(li_a)
        del li_a[max_a_index]
        del li_b[max_b_index]
    else:
        a_score += li_a[li_c.index(min(li_c))]
        del li_a[li_c.index(min(li_c))]
        del li_b[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))
    li_c = [li_a[j] - li_b[j] for j in range(len(li_a))]
    if max(li_a) == li_a[max_b_index] or max(li_b) == li_b[max_a_index]:
        b_score += max(li_b)
        del li_a[max_a_index]
        del li_b[max_b_index]
    else:
        b_score += li_b[li_c.index(max(li_c))]
        del li_a[li_c.index(max(li_c))]
        del li_b[li_c.index(max(li_c))]
    # 終了判定
    counter += 1
    if counter == n:
        break
  
print(a_score - b_score)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [None]*N
B = [None]*N
A_ = [None]*N
B_ = [None]*N
for i in range(N):
  A_num,B_num = map(int,input().split())
  A[i] = A_num
  B[i] = B_num
  if A_num-B_num == 0:
    A_[i] = A_num
    B_[i] = B_num
  else:
    A_[i] = A_num-B_num
    B_[i] = B_num-A_num
count = 0
for i in range (N):
  if i%2 == 0:
    if min(A_) >= 0:
      x = A_.index(max(A_))
    else:
      x = A_.index(min(A_))
    count = count + A[x]
  else:
    x = B_.index(max(B_))
    count = count - B[x]
  A[x] = 0
  B[x] = 0
  A_[x] = 0
  B_[x] = 0

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
ab = []
for _ in range(n):
    r = list(map(int, input().split()))
    ab.append(r)

ab = np.array(ab)
takahashi = []
aoki = []

while True:
    if len(ab) == 0:
        break
    # 高橋
    ab = ab[np.lexsort((ab[:,0], ab[:,1]))]
    takahashi.append(ab[len(ab)-1,0])
    ab = np.delete(ab,len(ab)-1,0)

    # 青木
    if len(ab) == 0:
        break
    ab = ab[np.lexsort((ab[:,1], ab[:,0]))]
    aoki.append(ab[len(ab)-1,1])
    ab = np.delete(ab,len(ab)-1,0)
    

print(sum(takahashi) - sum(aoki))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n =int(input())
takahashi = 0
aoki = 0
a = [[int(i) for i in input().split()] for i in range(n)] 
a2 = sorted(a, key=lambda x: x[0])
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:
data = input().split()

n = int(data[0])
a=[]
valueList=[]
for i in range(n):
    line = input().split()
    tmp=[]
    tmp.append(int(line[0]))
    tmp.append(int(line[1]))
    a.append(tmp)
    valueList.append(int(line[1]) + int(line[0]))


taka=0
ao=0

for i in range(n):
    ind = valueList.index(max(valueList))
    if(i%2 == 0):
        taka += a[ind][0]
    else:
        ao += a[ind][1]
    a.pop(ind)
    valueList.pop(ind)

print(taka-ao)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def main():
    N = int(input())
    A = np.array([])
    B = np.array([])
    for i in range(0, N):
        a, b = map(int, input().split())
        A = np.append(A, [a])
        B = np.append(B, [b])
    
    # 自分に一番良い手と相手にとって一番良い手を考える
    # 自分にとって一番良い手が複数ある場合、その中で相手が一番良い手を選ぶ
    # 自分にとって一番良い手が１つの場合、相手にとって一番良い手との比較をして、差が大きい方を選ぶ
    # 自分にとって良いてと相手にとっていい手が同じ場合(相手にとっていい手が複数ある場合も)、それを選ぶ
    takahashi = 0
    aoki = 0
    # 足し算して一番大きいところからとる
    sum_AB = A + B
    #  print(A, B)
    for i in range(1, N + 1):
        max_idx = np.argmax(sum_AB)
        #  print(sum_AB, max_idx)
        if i % 2 == 1:
            takahashi += A[max_idx]
        else:
            aoki += B[max_idx]
        sum_AB[max_idx] = -1

    print(int(takahashi - aoki))


def sub(N, A, B):
    takahashi = 0
    aoki = 0
    # 足し算して一番大きいところからとる
    sum_AB = A + B
    #  print(A, B)
    for i in range(1, N + 1):
        max_idx = np.argmax(sum_AB)
        #  print(sum_AB, max_idx)
        if i % 2 == 1:
            takahashi += A[max_idx]
        else:
            aoki += B[max_idx]
        sum_AB[max_idx] = -1

    print(int(takahashi - aoki))



main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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

for i in range(N) :
#while len(L) :
    if not i % 2 :
        L = sorted(L,key=lambda x: x[0],reverse=True)
        takapoint += L[0][0]
        L.pop(0)
        #turn = "aoki"
    else :
        L = sorted(L,key=lambda x: x[1],reverse=True)
        aokipoint += L[0][1]
        L.pop(0)
        #turn = "taka"
ans = takapoint - aokipoint

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

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

ab = sorted(ab, key=itemgetter(0,1), reverse=True)

total = 0
for i in range(n):
    if i % 2 == 0:
        total += ab[0][0]
        ab.pop(0)
    else:
        idx = 0

        for l in range(len(ab)):
            if ab[l][0] < ab[idx][0] and ab[l][1] >= ab[idx][1]:
                idx = l
        total -= ab[idx][1]
        ab.pop(idx)

print(total)

Submission status: Time Limit Exceeded

Solution code:
# Different Strokes
n = int(input())
a, b = list(), list()
for _ in range(n):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)
a_sort = sorted(a)
b_sort = sorted(b)
ans = 0
while True:
    const = b_sort.pop()
    indexes = list()
    for i in range(len(b)):
        if b[i] == const:
            indexes.append(i)
    m = 0
    ind = 0
    for i in indexes:
        if a[i] >= m:
            ind = i
            m = a[i]
    ans += m
    a_sort.remove(m)
    del a[ind]
    del b[ind]
    if not a:
        exit(print(ans))

    const = a_sort.pop()
    indexes = list()
    for i in range(len(a)):
        if a[i] == const:
            indexes.append(i)
    m = 0
    ind = 0
    for i in indexes:
        if b[i] >= m:
            ind = i
            m = b[i]
    ans -= m
    b_sort.remove(m)
    del a[ind]
    del b[ind]
    if not a:
        exit(print(ans))

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):
    return np.unique(es).shape[0] == 1

ind = 0

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

a_happy = 0
b_happy = 0
A_flag = 0
B_flag = 0

if set_list(A):
    A_flag = 1

if set_list(B):
    B_flag = 1

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

print(a_happy - b_happy)