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 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)

def remove(a, data,tag):
    if tag:
        tag=0
    else:
        tag=1
    n = len(a)
    first = 0
    last = n - 1

    while first <= last:
        mid = int(first + ((last - first) / 2))

        if a[mid][tag] < data[tag] or (a[mid][tag] == data[tag] and a[mid][1-tag] < data[1-tag]):
            last = mid - 1
        elif a[mid][tag] > data[tag] or (a[mid][tag] == data[tag] and a[mid][1-tag] > data[1-tag]):
            first = mid + 1
        else:
            del a[mid]
            return

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

    return pa-pb
    
print(func(a,b))

Submission status: Time Limit Exceeded

Solution code:
N1 = int(input())
A1 = [0] * N1
B1 = [0] * N1
for i in range(N1):
    A1[i], B1[i] = map(int, input().split())

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

print(solution(N1, A1, B1))

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)

def remove(a, data,tag):
    if tag:
        tag=0
    else:
        tag=1
    n = len(a)
    first = 0
    last = n - 1

    while first <= last:
        mid = int(first + ((last - first) / 2))

        if a[mid][tag] < data[tag] or (a[mid][tag] == data[tag] and a[mid][1-tag] < data[1-tag]):
            last = mid - 1
        elif a[mid][tag] > data[tag] or (a[mid][tag] == data[tag] and a[mid][1-tag] > data[1-tag]):
            first = mid + 1
        else:
            del a[mid]
            return

def func(a,b):
    pa,pb=0,0
    tag=True
    while len(a)!=0:

        if a[0]==b[0]:
            tmp=a[0]
        else:
            v1=a[0][0]-b[0][1]
            v2=b[0][0]-b[1][1]
            if v1>v2:
                tmp=a[0]
            else:
                tmp=b[0]
        if tag:
            pa+=tmp[0]
        else:
            pb+=tmp[1]
        remove(a,tmp,tag)
        remove(b,tmp,not tag)
        tag=not tag
        a,b=b,a

    return pa-pb
    
print(func(a,b))

Submission status: Time Limit Exceeded

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


main()

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

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(a_tmp + b_tmp)
dsum = [A[i]/sum(A) + B[i]/sum(B) for i in range(N)]
A_score = 0
B_score = 0
dsum.sort(reverse=True)
A.sort(key=dict(zip(A, dsum)).get)
B.sort(key=dict(zip(B, dsum)).get)

for i in range(N):
    if i%2 == 0:
        A_score += A[i]
    else:
        B_score += B[i]
print(A_score - B_score)

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 = [0] * n
for i in range(n):
    a,b = map(int,input().split())
    ab[i] = [a,b,abs(a-b)]
ans = 0
for i in range(n):
    ab.sort(key=lambda ab:(ab[i%2],ab[2]))
    ans += ab[-1][i%2] * ((i % 2) * -2 +1)
    ab.pop()
print(ans)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = [list(map(int, input().split())) for _ in range(N)]
A=0
B=0
for i in range(N):
  if i%2==0:
    D.sort(reverse=True)
    A+=D[0][0]
    D.pop(0)
  else:
    D.sort(reverse=True, key=lambda x:x[1])
    B+=D[0][1]
    D.pop(0)
  
print(A-B)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
foodlist = []
for i in range(N):
    foodlist.append(map(int, raw_input().split()))
#print foodlist
evalution_list = [0] * N
for i in range(N):
        evalution_list[i] = foodlist[i][0] + foodlist[i][1]
#print evalution_list

which_turn=0
points=[0,0]
for i in range(N):
    #print("turn is {0}".format(which_turn))
    choise = evalution_list.index(max(evalution_list))
    #print choise
    foodpoint=foodlist[choise][which_turn]
    #print foodpoint
    evalution_list[choise]=None
    points[which_turn] += foodpoint
    #print("point is {0}".format(points[which_turn]))
    
    which_turn += 1
    if which_turn > 1:
        which_turn=0
#print points
#print("point diff is {0}".format(points[0]-points[1]))
print("{0}".format(points[0]-points[1]))

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(input())

aDishScore = []
bDishScore = []

for i in range(N):
    a, b = map(int, input().split())
    aDishScore += [a]
    bDishScore += [b]

aTotal = 0
bTotal = 0

aTurn = True

def happinessScore(playerDish, opponentDish):
    return playerDish - opponentDish

def getMaxIndex(playerDish, opponentDish):
    index = -1
    maxScore = -sys.maxsize + 1
    opponentMaxScore = -sys.maxsize + 1
    for i in enumerate(playerDish):
        if (i[1] > maxScore):
            index = i[0]
            maxScore = i[1]
            opponentMaxScore = opponentDish[i[0]]
        elif (i[1] == maxScore):
            if (opponentMaxScore < opponentDish[i[0]]):
                index = i[0]
                maxScore = i[1]
                opponentMaxScore = opponentDish[i[0]]
    return index

def indexToTake(playerDish, opponentDish):
    playerMaxIndex = getMaxIndex(playerDish, opponentDish)
    opponentMaxIndex = getMaxIndex(opponentDish, playerDish)
    
    playerDishCopy = playerDish.copy()
    opponentDishCopy = opponentDish.copy()
    del opponentDishCopy[opponentMaxIndex]
    del playerDishCopy[opponentMaxIndex]
    opponentSecondMaxIndex = getMaxIndex(opponentDishCopy, playerDishCopy)
    
    if (happinessScore(playerDish[playerMaxIndex], opponentDish[opponentMaxIndex]) >= happinessScore(playerDish[opponentMaxIndex], opponentDish[opponentSecondMaxIndex])):
        return playerMaxIndex
    else:
        return opponentMaxIndex


for i in range(N):
    if (aTurn):
        index = indexToTake(aDishScore, bDishScore)
        aTotal += aDishScore[index]
        del aDishScore[index]
        del bDishScore[index]
    else:
        index = indexToTake(bDishScore, aDishScore)
        bTotal += bDishScore[index]
        del aDishScore[index]
        del bDishScore[index]
    aTurn = not aTurn

print(aTotal - bTotal)

Submission status: Time Limit Exceeded

Solution code:
from collections import OrderedDict
N = int(input())
AB = [tuple(int(i) for i in input().split()) for _ in range(N)]

temp = sorted(AB, key=lambda x: x[0])
B_list = OrderedDict((i, v) for i, v in enumerate(sorted(temp, key=lambda x: x[1])))
temp = sorted(AB, key=lambda x: x[1])
A_list = OrderedDict((i, v) for i, v in enumerate(sorted(temp, key=lambda x: x[0])))


def f(a, b):
    if a[0] == b[1]:
        return a[0] >= b[0]
    return a[0] > b[1]

turn = True
t_sum = 0
a_sum = 0
for _ in range(N):
    A_max = list(A_list.values())[0]
    B_max = list(A_list.values())[0]

    if (A_max, B_max):
        k, v = A_list.popitem()
        del B_list[k]
    else:
        k, v = B_list.popitem()
        del A_list[k]

    if turn:
        t_sum += v[0]
    else:
        a_sum += v[1]

    turn = not turn

print(t_sum - a_sum)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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, dtype='float32')
#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(int(t_score-a_score))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
takahashi=[]
aoki=[]
gohan=[]
kouhuku_t=0
kouhuku_a=0
for i in range(n):
    a,b=map(int,input().split())
    takahashi.append(a)
    aoki.append(b)
    gohan.append(a+b)
for i in range(n):
    if i%2==0:
        count=np.argmax(gohan)
        kouhuku_t+=takahashi.pop(count)
        del aoki[count]
    else:
        count=np.argmax(aoki)
        kouhuku_a+=aoki.pop(count)
        del takahashi[count]
    del gohan[count]
print(kouhuku_t-kouhuku_a)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
L = []
for i in range(N):
  L.append(raw_input())
A = []
B = []
for i in range(N):
    # print(L[i])
    L[i] = L[i].split()
    A.append([int(L[i][0]), int(L[i][1]), i])
    B.append([int(L[i][1]), int(L[i][0]), i])
    L[i].append(i)
A.sort()
B.sort()
used = []
time = 0
resultA = 0
resultB = 0
i = N-1
while i >= 0:
    if time % 2 == 0:
        if A[i][2] not in used:
            resultA += A[i][0]
            used.append(A[i][2])
            time += 1
    else:
        if B[i][2] not in used:
            resultB += B[i][0]
            used.append(B[i][2])
            time += 1
    i -= 1

result = resultA - resultB
# print(used)
print(result)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter, attrgetter
#input
n = int(input())
H = [list(map(int, input().split())) for _ in range(n)]
C = sorted(H, key = itemgetter(0), reverse = True)
A = sorted(H, key = itemgetter(1), reverse = True)

ans_c = 0
ans_a = 0
turn = 0

while C != [] and A != []:
    if turn % 2 == 0: #chokudai 
        if C[0][0] - A[0][0] >= A[0][1] - C[0][1]:
            chokudai = C.pop(0)
            ans_c += chokudai[0]
            A = sorted(C, key = itemgetter(1), reverse = True)
        else:
            chokudai = A.pop(0)
            ans_c += chokudai[0]
            C = sorted(A, key = itemgetter(0), reverse = True)
    else: #aoki
        if A[0][1] - C[0][1] >= C[0][0] - A[0][0]:
            aoki = A.pop(0)
            ans_a += aoki[1]
            C = sorted(A, key = itemgetter(0), reverse = True)
        else:
            aoki = C.pop(0)
            ans_a += aoki[1]
            A = sorted(C, key = itemgetter(1), reverse = True)
    turn += 1

print(ans_c - ans_a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AB = [[], []]
ab = [0, 0]

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

for i in range(N):
    m = max(AB[i%2])
    indexs = [j for j, x in enumerate(AB[i%2]) if x == max(AB[i%2])]
    B = []
    for j in range(len(indexs)):
        B.append(AB[(i+1)%2][indexs[j]])
    index = AB[(i+1)%2].index(max(AB[(i+1)%2]))
    ab[i%2] += m
    del AB[0][index]
    del AB[1][index]

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

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter, attrgetter
#input
n = int(input())
H = [list(map(int, input().split())) for _ in range(n)]
C = sorted(H, key = itemgetter(0), reverse = True)
A = sorted(H, key = itemgetter(1), reverse = True)

ans_c = 0
ans_a = 0
turn = 0

while C != [] and A != []:
    if turn % 2 == 0: #chokudai 
        if C[0][0] - A[0][0] >= A[0][1] - C[0][1]:
            chokudai = C.pop(0)
            ans_c += chokudai[0]
            A = sorted(C, key = itemgetter(1), reverse = True)
        else:
            chokudai = A.pop(0)
            ans_c += chokudai[0]
            C = sorted(A, key = itemgetter(0), reverse = True)
    else: #aoki
        if A[0][1] - C[0][1] >= C[0][0] - A[0][0]:
            aoki = A.pop(0)
            ans_a += aoki[1]
            C = sorted(A, key = itemgetter(1), reverse = True)
        else:
            aoki = C.pop(0)
            ans_a += aoki[1]
            A = sorted(C, key = itemgetter(1), reverse = True)
    turn += 1

print(ans_c - ans_a)

Submission status: Time Limit Exceeded

Solution code:
base = int(input())

data = [[0 for i in range(2)] for j in range(base)]

for num in range(base):
    _num = input()
    _num = _num.split()
    data[num][0] = int(_num[0])
    data[num][1] = int(_num[1])


takahashi = 0
aoki = 0

data.sort()
data.reverse()
_num = 0
for num in range(base):
    if(_num == 0):
        takahashi += data[0][0]
        _num = 1
        data.pop(0)
        data.sort()
        data.reverse()

    elif(_num == 1):
        aoki += data[0][1]
        _num = 0
        data.pop(0)
        data.sort()
        data.reverse()

print(takahashi-aoki)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dish_list = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    dish_list.append([ai, bi, ai+bi])




a, b = 0, 0
def dfs(dish_list, turn):
    if dish_list==[]:
        return
    # print("=================")
    # print(dish_list)
    global a
    global b


    #評価値が最大の皿をみつける
    max_id = 0
    max_p = 0
    for i, dish in enumerate(dish_list):
        if dish[2] > max_p:
            max_p = dish[2]
            max_id = i

    #幸福度を加算
    # print(max_id, max_p)
    if turn == -1:
        a+=dish_list[max_id][0]
    elif turn == 1:
        b+=dish_list[max_id][1]

    #評価値最大のさらを削除した新しい皿のリストを作る
    new_list = [list for i, list in enumerate(dish_list) if i!=max_id]
    dfs(new_list, turn*(-1))

dfs(dish_list, -1)

# print(a, b, a-b)

print(a-b)

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_a[max_b_index] == max(li_a) or li_b[max_a_index] == max(li_b):
        a_score += max(li_a)
        del li_a[max_a_index]
        del li_b[max_b_index]
    else:
        li_c = [li_a[j] - li_b[j] for j in range(len(li_a))]
        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))
    if li_a[max_b_index] == max(li_a) or li_b[max_a_index] == max(li_b):
        b_score += max(li_b)
        del li_a[max_a_index]
        del li_b[max_b_index]
    else:
        li_c = [li_a[j] - li_b[j] for j in range(len(li_a))]
        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())
d = []
A=[]
B=[]
for i in range(N):
    d.append(list(map(int, input().split())))
    A.append(d[i][0])
    B.append(d[i][1])

taka_point=0
aoki_point=0

for i in range(N//2):  
    mA=max(A)
    mB=max(B)
    if mA>mB:
        index=A.index(mA)
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    else:
        index=B.index(mB)
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    taka_point+=point
    #print(point)
    
    mA=max(A)
    mB=max(B)    
    if mA>mB:
        index=A.index(mA)
        point=d[index][1]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    else:
        index=B.index(mB)
        point=d[index][1]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    aoki_point+=point
    #print(point)
    
if N%2==1:
    mA=max(A)
    mB=max(B)
    if mA>mB:
        index=A.index(mA)
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    else:
        index=B.index(mB)
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    taka_point+=point
    #print(point)
    
print(taka_point-aoki_point)

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
        

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

Submission status: Time Limit Exceeded

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

sol = 0
while abd:
  player = (n - len(abd)) % 2
  if len(abd) % 2:
    eat = min(range(len(abd)), key=lambda x: 2e9*abd[x][2] - abd[x][player])
  else:
    eat = min(range(len(abd)), key=lambda x: abd[x][2] - 2e9*abd[x][player])
  if player:
    sol -= abd[eat][player]
  else:
    sol += abd[eat][player]
  del abd[eat]
print(sol)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = input()
takahashi_v = []
aoki_v = []
for _ in range(N):
    a, b  = map(int, raw_input().split())
    record = [a, b, True]
    takahashi_v.append(record)
    aoki_v.append(record)
takahashi_v.sort(key=lambda x: (x[0] + x[1], x[0], -x[1]), reverse=True)
aoki_v.sort(key=lambda x: (x[0] + x[1], x[1], -x[0]), reverse=True)

res = 0
for _ in range(N):
    j = 0
    while j < N:
        if takahashi_v[j][2]:
            res += takahashi_v[j][0]
            takahashi_v[j][2] = False
            break
        j += 1
    if j >= N:
        break

    k = 0
    while k < N:
        if aoki_v[k][2]:
            res -= aoki_v[k][1]
            aoki_v[k][2] = False
            break
        k += 1
    if k >= N:
        break
print res

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)
    
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:
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]
    takahashi = sum([A[i] for i in [j for idx, j in enumerate(l) if idx % 2 == 0]])
    aoki = sum([B[i] for i in [j for idx, j in enumerate(l) if idx % 2 != 0]])

    print(int(takahashi - aoki))


main()

Submission status: Time Limit Exceeded

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


main()

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter, attrgetter
#input
n = int(input())
H = [list(map(int, input().split())) for _ in range(n)]
C = sorted(H, key = itemgetter(0, 1), reverse = True)
A = sorted(H, key = itemgetter(1, 0), reverse = True)

ans_c = 0
ans_a = 0
turn = 0

while C != [] and A != []:
    if turn % 2 == 0: #chokudai 
        if C[0][0] - A[0][0] >= A[0][1] - C[0][1]:
            chokudai = C.pop(0)
            ans_c += chokudai[0]
            A = sorted(C, key = itemgetter(1, 0), reverse = True)
        else:
            chokudai = A.pop(0)
            ans_c += chokudai[0]
            C = sorted(A, key = itemgetter(0, 1), reverse = True)
    else: #aoki
        if A[0][1] - C[0][1] >= C[0][0] - A[0][0]:
            aoki = A.pop(0)
            ans_a += aoki[1]
            C = sorted(A, key = itemgetter(0, 1), reverse = True)
        else:
            aoki = C.pop(0)
            ans_a = aoki[1]
            A = sorted(C, key = itemgetter(1, 0), reverse = True)
    turn += 1

print(ans_c - ans_a)

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:
    c=max(d,key=(lambda x : (x[0],x[1])))
    a += c[0]
  else:
    c=max(d,key=(lambda x : (x[1],x[0])))
    b += c[1]
  d.remove(c)
print(a-b)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
taka = []
aoki = []
score = 0
for i in range(n):
  t, a = map(int, input().split())
  taka.append(t)
  aoki.append(a)
for i in range(n):
  maxT = max(taka)
  maxA = max(aoki)
  rm = 0
  if (maxT > maxA):
    rm = taka.index(maxT)
  else:
    rm = aoki.index(maxA)
  if (i % 2 == 0):
    score += taka.pop(rm)
    aoki.pop(rm)
  else:
    taka.pop(rm)
    score -= aoki.pop(rm)
print(score)

Submission status: Time Limit Exceeded

Solution code:
def res(D, p):
    if len(D)==1:
      return D[0][p] if p==0 else -D[0][p]
    else:
      m=max(D, key=lambda x:x[p])
      tmp=[d for d in D if d[p]==m[p]]
      t=max(tmp, key=lambda x:x[abs(p-1)])
      D.remove(t)
      return res(D, abs(p-1)) + (t[p] if p==0 else -t[p])
      
N = int(input())
D = [list(map(int, input().split())) for _ in range(N)]
  
print(res(D, 0))

Submission status: Time Limit Exceeded

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

takahashi = []
aoki = []

while True:
    if len(ab) == 0:
        break
    # 高橋
    ab = sorted(ab, key=lambda x:(x[0],x[1]), reverse=True)
    takahashi.append(ab[len(ab)-1][0])
    del ab[len(ab)-1]

    # 青木
    if len(ab) == 0:
        break
    ab = sorted(ab, key=lambda x:(x[1],x[0]), reverse=True)
    aoki.append(ab[len(ab)-1][1])
    del ab[len(ab)-1]
    

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [0] * N
B = [0] * N
y = [0] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())
 
A_score=[]
B_score=[]
for n in range(0, N):
    if n > 0:
        tmp = A
        A = B
        B = tmp
    max_index_list = [i for i, x in enumerate(A) if x == max(A)]
    j = max_index_list[0]
    if len(max_index_list) > 1:
        maxim = 0
        for i in max_index_list:
            if B[i] > maxim:
                j = i
                maxim = B[i]
    if n%2==0:
        A_score.append(A[j])
    else:
        B_score.append(A[j])
    A.pop(j)
    B.pop(j)
print(sum(A_score)-sum(B_score))

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
n = int(input())

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

for i in n_l:
    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)