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())
A, B = [], []

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

A = sorted(A, key = lambda x: x[0])
B = sorted(B, key = lambda x: x[0])
flag = [0] * n
sum_t, sum_a = 0, 0

def search_num(l):
    ret = 0
    while flag[l[-1 - ret][1] - 1] == 1:
        ret += 1
    
    return ret

for i in range(n):
    if i % 2 == 0:
        index = search_num(A)
        sum_t += A[-1 - index][0]
        flag[A[-1 - index][1] - 1] = 1
    else:
        index = search_num(B)
        sum_a += B[-1 - index][0]
        flag[B[-1 - index][1] - 1] = 1

print(sum_t - sum_a)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = [0] * N
    B = [0] * N
    C = []
    for i in range(N):
        A[i], B[i] = map(int, input().split())
        C.append([A+B, i])

    C.sort(reverse=True)
    ans = 0
    for i, c in enumerate(C):
        if i % 2 == 0:
            ans += A[c[1]]
        else:
            ans -= B[c[1]]

    print(ans) 

    
    



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

ab.sort(key=lambda x:x[0])
ba.sort(key=lambda x:x[0])

ans = 0
for i in range(n):
    if i % 2 == 0:
        if ab[0][0] == ab[-1][0]:
            b, a = ba.pop()
            ab.remove((a, b))
        else:
            a, b = ab.pop()
            ba.remove((b, a))
        ans += a
    else:
        if ba[0][0] == ba[-1][0]:
            a, b = ab.pop()
            ba.remove((b, a))
        else:
            b, a = ba.pop()
            ab.remove((a, b))
        ans -= b
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

t=0
a=0

for i in range(N):
  if i%2==0: #高橋くんの番
    C.sort(key= lambda x: x[3],reverse=True)
    C.sort(key= lambda x: x[0],reverse=True)
    #print(i,C)
    t += C.pop(0)[0]
  if i%2==1: #青木さんの番
    C.sort(key= lambda x: x[2],reverse=True)
    C.sort(key= lambda x: x[1],reverse=True)
    #print(i,C)
    a += C.pop(0)[1]
  #print(i,C)
print(t-a)

Submission status: Time Limit Exceeded

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

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

t=0
a=0

for i in range(N):
  if i%2==0: #高橋くんの番
    C.sort(key= lambda x: x[3],reverse=True)
    C.sort(key= lambda x: x[0],reverse=True)
    #print(i,C)
    t += C.pop(0)[0]
  if i%2==1: #青木さんの番
    C.sort(key= lambda x: x[2],reverse=True)
    C.sort(key= lambda x: x[1],reverse=True)
    #print(i,C)
    a += C.pop(0)[1]
  #print(i,C)
print(t-a)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=[]
for _ in range(n):
    L.append(tuple(map(int,input().split())))
tkh=sorted(L,reverse=True)
aok=sorted(L,key=lambda x:x[1],reverse=True)
tkp=0
aop=0
for i in range(n):
    tkn=tkh[0]
    aon=aok[0]
    if i%2==0:
        tk=tkn[0]-aon[1]
        ao=aon[0]-tkn[1]
        p=max(tkn[0],aon[0])
        tkp += p
    else:
        ao=aon[1]-tkn[0]
        tk=tkn[1]-aon[0]
        p=max(tkn[1],aon[1])
        aop += p
    if tk>=ao:
        dish=tkn
    else:
        dish=aon
    tkh.remove(dish)
    aok.remove(dish)
print(tkp-aop)

Submission status: Time Limit Exceeded

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

AB = sorted([[A[i] + B[i], i] for i in range(N)])[::-1]
indexes = sorted([AB[i][1] for i in range(0, N, 2)])[::-1]
ans = 0

for i in range(N):
    if i == indexes[-1]:
        indexes = indexes[:-1]
        ans += A[i]
    else:
        ans -= B[i]
print(ans)

Submission status: Time Limit Exceeded

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

a = b = 0
for i in range(N):
  if i % 2 == 0:
    AB.sort(key=lambda x:x[1])
    a += AB[-1][0]
  else:
    AB.sort()
    b += AB[-1][1]
  AB[-1] = [0, 0]
print(a-b)

Submission status: Time Limit Exceeded

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

a = b = 0
for i in range(N):
  if i % 2 == 0: #高橋くんの番
    x, y = A, B
  else:#青木さんの番
    x, y = B, A
  indexes = [i for i, j in enumerate(y) if j == max(y)] #yの最大値のインデックスすべて
  l = len(indexes)
  m = 0
  ind = 0
  for j in indexes: #このうちxで最大値を取るインデックス
    if x[j] > m:
      m = x[j]
      ind = j
  if i % 2 == 0: a += x[ind]
  else: b += x[ind]
  x[ind] = y[ind] = 0
print(a-b)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
ab = [LIST() for _ in range(n)]
a_b = [ab[i][0] - ab[i][1] for i in range(n)]
b_a = [ab[i][1] - ab[i][0] for i in range(n)]
arr1 = [[i, ab[i][0], ab[i][1]] for i in range(n)]
arr2 = deepcopy(arr1)
arr1.sort(key=lambda x: (x[1], x[2]))
arr2.sort(key=lambda x: (x[2], x[1]))
tmp = [[arr2[i][0], arr1[i][0]] for i in range(n)]
tmp.sort(key=lambda x:x[1])
idx1 = [tmp[i][1] for i in range(n)]
tmp.sort()
idx2 = [tmp[i][0] for i in range(n)]
t = 0
a = 0
for i in range(n):
    if not i % 2:
        tmp = arr1.pop()
        t += tmp[1]
        del arr2[idx1[tmp[0]]]
    elif i % 2:
        tmp = arr2.pop()
        a += tmp[2]
        del arr1[idx2[tmp[0]]]
    debug(arr1, t)
    debug(arr2, a)
print(t - a)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
ab = [LIST() for _ in range(n)]
a_b = [ab[i][0] - ab[i][1] for i in range(n)]
b_a = [ab[i][1] - ab[i][0] for i in range(n)]
arr1 = [[i, ab[i][0], ab[i][1]] for i in range(n)]
arr2 = deepcopy(arr1)
arr1.sort(key=lambda x: (x[1], x[2]))
arr2.sort(key=lambda x: (x[2], x[1]))
debug(arr1)
debug(arr2)
t = 0
a = 0
for i in range(n):
    if not i % 2:
        tmp = arr1.pop()
        t += tmp[1]
        del arr2[arr2.index(tmp)]
    elif i % 2:
        tmp = arr2.pop()
        a += tmp[2]
        del arr1[arr1.index(tmp)]
    debug(arr1, t)
    debug(arr2, a)
print(t - a)

Submission status: Time Limit Exceeded

Solution code:
import heapq 

N = int(input())
finish = [False] * N
As, Bs = [], []
for i in range(N):
    a,b = map(int, input().split())
    # As.append((a,b,i))
    # Bs.append((b,a,i))
    heapq.heappush(As,(a,b,i))
    heapq.heappush(Bs,(b,a,i))

# cnt = 0
# As.sort(reverse=True)
# Bs.sort(reverse=True)




cnt = 0
p_a = 0
p_b = 0
while sum(finish)<N:
    # 高橋くんのターン
    while True:
        a,_,i = heapq.heappop(As)
        # a,_,i = As.pop()
        if not finish[i]:
            p_a += a
            finish[i] = True
            cnt += 1
            break
    # 青木
    while True and cnt < N:
        # b,_,i = Bs.pop()
        b,_,i = heapq.heappop(Bs)
        
        if not finish[i]:
            p_b += b
            finish[i] = True
            cnt += 1
            break
    # print(cnt,p_a,p_b)

print(p_a-p_b)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

N = int(input())
finish = [False] * N
As, Bs = [], []
A_,B_ = [],[]
for i in range(N):
    a,b = map(int, input().split())
    As.append((a,b,i))
    Bs.append((b,a,i))

cnt = 0
As.sort(reverse=True)
Bs.sort(reverse=True)

p_a = 0
p_b = 0
while sum(finish)<N:
    # 高橋くんのターン
    while True:
        a,_,i = As.pop()
        if not finish[i]:
            p_a += a
            finish[i] = True
            cnt += 1
            break
    # 青木
    while True and cnt < N:
        b,_,i = Bs.pop()
        if not finish[i]:
            p_b += b
            finish[i] = True
            cnt += 1
            break
    # print(cnt,p_a,p_b)

print(p_a-p_b)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
finish = [False] * N
As, Bs = [], []
A_,B_ = [],[]
for i in range(N):
    a,b = map(int, input().split())
    As.append((a*10**10+b,i))
    Bs.append((b*10**10+a,i))
    A_.append(a)
    B_.append(b)
from operator import itemgetter
cnt = 0
As.sort(reverse=True)
Bs.sort(reverse=True)

p_a = 0
p_b = 0
while sum(finish)<N:
    # 高橋くんのターン
    while True:
        a,i = As.pop()
        if not finish[i]:
            p_a += (a-B_[i])//10**10
            finish[i] = True
            cnt += 1
            break
    # 青木
    while True and cnt < N:
        b,i = Bs.pop()
        if not finish[i]:
            p_b += (b-A_[i])//10**10
            finish[i] = True
            cnt += 1
            break
    # print(cnt,p_a,p_b)

print(p_a-p_b)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
finish = [False] * N
As, Bs = [], []
for i in range(N):
    a,b = map(int, input().split())
    As.append((a,b,i))
    Bs.append((b,a,i))
from operator import itemgetter
cnt = 0
As = sorted(As, key=itemgetter(0,1),reverse=True)
Bs = sorted(Bs, key=itemgetter(0,1),reverse=True)

p_a = 0
p_b = 0
while sum(finish)<N:
    # 高橋くんのターン
    while True:
        a,_,i = As.pop()
        if not finish[i]:
            p_a += a
            finish[i] = True
            cnt += 1
            break
    # 青木
    while True and cnt < N:
        b,_,i = Bs.pop()
        if not finish[i]:
            p_b += b
            finish[i] = True
            cnt += 1
            break
    # print(cnt,p_a,p_b)

print(p_a-p_b)

Submission status: Time Limit Exceeded

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

flag=1
A,B=0,0
for i in range(n):
    if flag==1:
        AB=sorted(AB, key=lambda x:x[0])
        A +=AB.pop()[0]
        flag *=-1
    else:
        AB=sorted(AB, key=lambda x:x[1])
        B +=AB.pop()[1]
        flag *=-1

print(A-B)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

flags = [1 for _ in range(N)]
Asum, Bsum = 0, 0
ans = 0
turn = 1
while sum(flags)>0:
  if turn == 1:
    while len(A)>0:
      neg_a, _, ii = heapq.heappop(A)
      if flags[ii] == 1:
        flags[ii] = 0
        break
    Asum += -neg_a
  else:
    while len(B)>0:
      neg_b, _, ii = heapq.heappop(B)
      if flags[ii] == 1:
        flags[ii] = 0
        break
    Bsum += -neg_b
  turn *= -1
print(Asum-Bsum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

D=[]

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

t=0
a=0

for i in range(N):
    if i%2==0:
        mxA=max(D,key=lambda x:x[0])
        mind=10**9
        for j in range(N-i):
            if D[j][0]<mxA[0]:
                continue
            else:
                if D[j][2]<mind:
                    mind=min(mind,D[j][2])
                    idx=j
        t+=D[idx][0]
        D.pop(idx)
    elif i%2==1:
        mxB=max(D,key=lambda x:x[1])
        mxd=-10**9
        for j in range(N-i):
            if D[j][1]<mxB[1]:
                continue
            else:
                if D[j][2]>mxd:
                    mxd=max(mxd,D[j][2])
                    idx=j
        a+=D[idx][1]
        D.pop(idx)

print(t-a)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

D=[]

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

t=0
a=0

for i in range(N):
    if i%2==0:
        D.sort(key=lambda x:x[0],reverse=True)
        mxA=D[0][0]
        mind=10**9
        for j in range(len(D)):
            if D[j][0]<mxA:
                break
            else:
                if D[j][2]<mind:
                    mind=min(mind,D[j][2])
                    idx=j
        t+=D[idx][0]
        D.pop(idx)
    elif i%2==1:
        D.sort(key=lambda x:x[1],reverse=True)
        mxB=D[0][1]
        mxd=-10**9
        for j in range(len(D)):
            if D[j][1]<mxB:
                break
            else:
                if D[j][2]>mxd:
                    mxd=max(mxd,D[j][2])
                    idx=j
        a+=D[idx][1]
        D.pop(idx)

print(t-a)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
arr = []

for i in range(n):
    arr.append(list(map(int, input().split(' '))))

arr = np.array(arr)

takahashi = 0
aoki = 0


for i in range(n):
    max_i = np.argmax(np.sum(arr, axis=1))
    if i % 2 == 0:
        takahashi += arr[max_i, 0]
    else:
        aoki += arr[max_i, 1]
    arr = np.delete(arr, max_i, axis=0)
    
print(takahashi - aoki)

Submission status: Time Limit Exceeded

Solution code:
import heapq 
import copy
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
r=copy.deepcopy(l)

for i in range(n):
    l[i][0],l[i][1]=-l[i][0],-l[i][1]
    r[i][0],r[i][1]=-r[i][1],-r[i][0]
heapq.heapify(l)
heapq.heapify(r)
ans=0
for i in range(n):
    if i%2==0:
        ans-=l[0][0]*((-1)**i)
        r.remove(l[0][::-1])
        minimum=heapq.heappop(l)
    else:
        ans-=r[0][0]*((-1)**i)
        l.remove(r[0][::-1])
        minimum=heapq.heappop(r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
imput=sys.stdin.readline
import heapq 
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    l[i][0],l[i][1]=-l[i][0],-l[i][1]
heapq.heapify(l)
ans=0
for i in range(n):
    ans-=l[0][0]*((-1)**i)
    minimum=heapq.heappop(l)
    for j in range(n-i-1):
        l[j][0],l[j][1]=l[j][1],l[j][0]
    #print(l)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq 
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    l[i][0],l[i][1]=-l[i][0],-l[i][1]
heapq.heapify(l)
ans=0
for i in range(n):
    ans-=l[0][0]*((-1)**i)
    minimum=heapq.heappop(l)
    for j in range(n-i-1):
        l[j][0],l[j][1]=l[j][1],l[j][0]
    #print(l)
print(ans)

Submission status: Time Limit Exceeded

Solution code:

def main():

    N = int(input())
    food1, food2 = [], []
    for i in range(N):
        a, b = map(int, input().split())
        food1.append([a+b, a, b, i])
        food2.append([-a-b, a, b, i])
    food1.sort(reverse = True)
    food2.sort()
    used = set()
    ans = 0
    while food1 or food2:
        while food1 and food1[0][3] in used:
            food1.pop(0)
        if food1:
            _, a, _, i = food1.pop(0)
            used.add(i)
            ans += a
        while food2 and food2[0][3] in used:
            food2.pop(0)
        if food2:
            _, _, b, i = food2.pop(0)
            used.add(i)
            ans -= b
    return ans


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:

import time



import numpy as np

N = int(input())
A = [0] * N
B = [0] * N
C = [[] for _ in range(N) ]
takahashi = 0
aoki = 0
for i in range(N):
  A[i], B[i] = map(int,input().split())
  C[i].append(i)
  C[i].append((np.array(A)[i] + np.array(B)[i]))



C = sorted(C, key=lambda x:x[1])




while len(C) > 0:
  tmp_max = C[-1][0]
  takahashi += A[tmp_max]
  A.pop(tmp_max)
  B.pop(tmp_max)
  C.pop(tmp_max)

  if len(C)>0 :
    tmp_max = C[-1][0]
    aoki += B[tmp_max]
    A.pop(tmp_max)
    B.pop(tmp_max)
    C.pop(tmp_max)

print(takahashi-aoki)

Submission status: Time Limit Exceeded

Solution code:
#%%
import time

start = time.time()

import numpy as np

N = int(input())
A = [0] * N
B = [0] * N
C = [[] for _ in range(N) ]
takahashi = 0
aoki = 0
for i in range(N):
  A[i], B[i] = map(int,input().split())
  C[i].append(i)
  C[i].append((np.array(A) + np.array(B))[i])

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
start = time.time()

C = sorted(C, key=lambda x:x[1])


elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
start = time.time()

while len(C) > 0:
  tmp_max = C[-1][0]
  takahashi += A[tmp_max]
  A.pop(tmp_max)
  B.pop(tmp_max)
  C.pop(tmp_max)

  if len(C)>0 :
    tmp_max = C[-1][0]
    aoki += B[tmp_max]
    A.pop(tmp_max)
    B.pop(tmp_max)
    C.pop(tmp_max)

print(takahashi-aoki)

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

while len(C) > 0:
  tmp_max = np.argmax(np.array(C))
  takahashi += A[tmp_max]
  A.pop(tmp_max)
  B.pop(tmp_max)
  C.pop(tmp_max)

  if len(C)>0 :
    tmp_max = np.argmax(np.array(C))
    aoki += B[tmp_max]
    A.pop(tmp_max)
    B.pop(tmp_max)
    C.pop(tmp_max)

print(takahashi-aoki)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
listTakahashi = []
listPoint = []
i=0
while i < n:
    a, b = map(int, input().split())
    listTakahashi.append([a+b, i])
    listPoint.append([a, b])
    i+=1
listTakahashi.sort()
listTakahashi.reverse()
index2=0
takaP=0
aokiP=0
while index2 < n:
    if index2%2==0:#takahashi's turn
        index = listTakahashi[0][1]
        takaP+=listPoint[index][0]
        del listTakahashi[0]
    else:
        index = listTakahashi[0][1]
        aokiP+=listPoint[index][1]
        del listTakahashi[0]
    index2+=1

print(takaP-aokiP)

Submission status: Time Limit Exceeded

Solution code:
n = input()
dishes = []
for i in range(int(n)):
    tmp = [int(each) for each in input().split()]
    dishes.append((abs(tmp[0] - tmp[1]), tmp[0], tmp[1]))

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

point = [0, 0]
player = 0
while len(dishes) > 0:
    tmp_point = -1
    dish = None
    for tmp_dish in dishes:
        if dish is None:
            dish = tmp_dish
        elif tmp_point > dish[0]:
            break
        else:
            if dish[player + 1] > tmp_dish[player + 1] and tmp_dish[player + 1] - tmp_dish[int(not player) + 1] >= 0:
                dish = tmp_dish
            else:
                pass
    dishes.remove(dish)
    point[player] += dish[player + 1]
    player = int(not player)
print(point[0] - point[1])

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

n = input()
dishes = []
for i in range(int(n)):
    tmp = [int(each) for each in input().split()]
    dishes.append((tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1]))

dishes = [sorted(dishes, key=itemgetter(0, 2), reverse=True), sorted(dishes, key=itemgetter(0, 3), reverse=True)]

# print(dishes[0])
# print(dishes[1])

point = [0, 0]
player = 0
while len(dishes[0]) > 0:
    dish = dishes[player].pop(0)
    # if player == 0:
    #     dish = dishes.pop(0)
    # else:
    #     dish = dishes.pop(len(dishes) - 1)

    dishes[int(not player)].remove(dish)
    # print(dish)
    point[player] += dish[player + 2]
    player = int(not player)
    # print(point)
print(point[0] - point[1])

Submission status: Time Limit Exceeded

Solution code:
import copy

n = input()
dishes = [[], []]
for i in range(int(n)):
    tmp = [int(each) for each in input().split()]
    # dish.append((tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1], i))
    dish = (tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1], i)
    dishes[0].append(dish)
    dishes[1].append(dish)

sorted(dishes[0], key=lambda v: v[0], reverse=True)
sorted(dishes[1], key=lambda v: v[1], reverse=True)

point = [0, 0]
player = 0
while len(dishes[0]) > 0:
    dish = dishes[player].pop(0)

    # remove dish
    dishes[int(not player)].remove(dish)

    point[player] += dish[player + 2]
    player = int(not player)
print(point[0] - point[1])

Submission status: Time Limit Exceeded

Solution code:
n = input()
dishes = [[], []]
for i in range(int(n)):
    tmp = [int(each) for each in input().split()]
    # dish.append((tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1], i))
    dishes[0].append((tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1], i))
    dishes[1].append((tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1], i))

# dishes = [copy.deepcopy(dish), copy.deepcopy(dish)]
sorted(dishes[0], key=lambda v: v[0], reverse=True)
sorted(dishes[1], key=lambda v: v[1], reverse=True)

point = [0, 0]
player = 0
while len(dishes[0]) > 0:
    dish = dishes[player].pop(0)

    # remove dish
    dishes[int(not player)].remove(dish)

    point[player] += dish[player + 2]
    player = int(not player)
print(point[0] - point[1])

Submission status: Time Limit Exceeded

Solution code:
def ex():
    n = input()
    dish = []
    for i in range(int(n)):
        tmp = [int(each) for each in input().split()]
        dish.append((tmp[0] - tmp[1], tmp[1] - tmp[0], tmp[0], tmp[1]))

    point = [0, 0]

    player = 0
    while len(dish) > 0:
        # print('player', player)
        sorted(dish, key=lambda v: v[player], reverse=True)
        # print(dish[0])
        point[player] += dish.pop(0)[player + 2]
        player = int(not player)
        # print('point', point)

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


ex()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
 
N=int(input())
A, B, C = np.zeros((3, N))
for num in range(N):
    A[num], B[num] = map(int, input().split())
 
SUM = 0
R = int(N / 2)
for num in range(R) :
    max_index = A.argmax() + B[np.where(A == A.max())].argmax()
    SUM += A[max_index]
    A[max_index] = 0
    B[max_index] = 0
    max_index = B.argmax() + A[np.where(B == B.max())].argmax()
    SUM -= B[max_index]
    A[max_index] = 0
    B[max_index] = 0
 
if N % 2 != 0 :
    max_index = A.argmax() + B[np.where(A == A.max())].argmax()
    SUM += A[max_index]
    A[max_index] = 0
    B[max_index] = 0
    
print(int(SUM))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A, B, C = np.zeros((3, N))
for num in range(N):
    A[num], B[num] = map(int, input().split())

SUM = 0
R = int(N / 2)
for num in range(R) :
    max_index = A.argmax() + B[np.where(A == A.max())].argmax()
    SUM += A[max_index]
    A = np.delete(A, max_index)
    B = np.delete(B, max_index)
    max_index = B.argmax() + A[np.where(B == B.max())].argmax()
    SUM -= B[max_index]
    A = np.delete(A, max_index)
    B = np.delete(B, max_index)

if N % 2 != 0 :
    max_index = A.argmax() + B[np.where(A == A.max())].argmax()
    SUM += A[max_index]
    A = np.delete(A, max_index)
    B = np.delete(B, max_index)
    
print(int(SUM))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A, B, C = np.zeros((3, N))
for num in range(N):
    A[num], B[num] = map(int, input().split())
SUM = 0
if N % 2 == 0 :
    R = int(N / 2)
else :
    R = int(N / 2 + 1)

for num in range(R) :
    max_index = A.argmax() + B[np.where(A == A.max())].argmax()
    SUM += A[max_index]
    A = np.delete(A, max_index)
    B = np.delete(B, max_index)
    if len(A) == 0 :
        break
    
    max_index = B.argmax() + A[np.where(B == B.max())].argmax()
    SUM -= B[max_index]
    A = np.delete(A, max_index)
    B = np.delete(B, max_index)
print(int(SUM))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
 
N=int(input())
A, B, C = np.zeros((3, N))
for num in range(N):
    A[num], B[num] = map(int, input().split())
 
SUM = 0
for num in range(N) :
    if num % 2 == 0 :
        max_index = A.argmax() + B[np.where(A == A.max())].argmax()
        SUM += A[max_index]
        A = np.delete(A, max_index)
        B = np.delete(B, max_index)
    else :
        max_index = B.argmax() + A[np.where(B == B.max())].argmax()
        SUM -= B[max_index]
        A = np.delete(A, max_index)
        B = np.delete(B, max_index)
 
print(int(SUM))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
B = []
C = []
for _ in range(N):
  a, b = map(int, input().split())
  A.append(a)
  B.append(b)
  C.append(a+b)

taka = 0
ao = 0

for i in range(N):
  c = max(C)
  ci = C.index(c)
  C.remove(c)

  if ((i % 2) == 0):
    taka += A[ci]
    #ao += B[ci]
  else:
    #taka += A[ci]
    ao += B[ci]

print(taka - ao)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
AB=[[0,0]for i in range(N)]
for i in range(N):
    AB[i][0],AB[i][1]=map(int,input().split())
t_delta_AB=[[0,0,i] for i in range(N)]
for i in range(N):
    t_delta_AB[i][1]=AB[i][1]-AB[i][0]
    t_delta_AB[i][0]=AB[i][0]
t_delta_AB.sort(key= lambda x:(x[0],x[1]),reverse=True)
a_delta_AB=[[0,0,i] for i in range(N)]
for i in range(N):
    a_delta_AB[i][1]=AB[i][0]-AB[i][1]
    a_delta_AB[i][0]=AB[i][1]
a_delta_AB.sort(key= lambda x:(x[0],x[1]),reverse=True)
takahashi_score=0
aoki_score=0
buf=[]
t=0
a=0
for i in range(N):
    if i%2==0:
        n=t
        while 1:
            if not(t_delta_AB[n][2] in buf):
                takahashi_score+=t_delta_AB[n][0]
                buf.append(t_delta_AB[n][2])
                t=n
                break
            n+=1
    else:
        n=a
        while 1:
            if not(a_delta_AB[n][2] in buf):
                aoki_score+=a_delta_AB[n][0]
                buf.append(a_delta_AB[n][2])
                a=n
                break
            n+=1
print(takahashi_score-aoki_score)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
AB=[[0,0]for i in range(N)]
for i in range(N):
    AB[i][0],AB[i][1]=map(int,input().split())
t_delta_AB=[[0,0,i] for i in range(N)]
for i in range(N):
    t_delta_AB[i][1]=AB[i][1]-AB[i][0]
    t_delta_AB[i][0]=AB[i][0]
t_delta_AB.sort(key= lambda x:(x[0],x[1]),reverse=True)
a_delta_AB=[[0,0,i] for i in range(N)]
for i in range(N):
    a_delta_AB[i][1]=AB[i][0]-AB[i][1]
    a_delta_AB[i][0]=AB[i][1]
a_delta_AB.sort(key= lambda x:(x[0],x[1]),reverse=True)
takahashi_score=0
aoki_score=0
buf=[]
for i in range(N):
    if i%2==0:
        n=i
        while 1:
            if not(t_delta_AB[n][2] in buf):
                takahashi_score+=t_delta_AB[n][0]
                buf.append(t_delta_AB[n][2])
                break
            n+=1
    else:
        n=i
        while 1:
            if not(a_delta_AB[n][2] in buf):
                aoki_score+=a_delta_AB[n][0]
                buf.append(a_delta_AB[n][2])
                break
            n+=1
print(takahashi_score-aoki_score)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    # A, B = [], []
    score = []
    for n in range(N):
        a, b = map(int, input().split())
        # A.append([n, a])
        # B.append([n, b])
        score.append([n, a-b, a, b])

    score.sort(key=lambda x: x[1])
    closed = [False for _ in range(N)]

    tak_p, ao_p = 0, 0
    while True:
        if len(score) == 0:
            break
        tak_p += score[0][2]
        score = score[1:]

        if len(score) == 0:
            break
        ao_p += score[0][3]
        score = score[1:]

    print(tak_p - ao_p)

Submission status: Time Limit Exceeded

Solution code:
import sys

n=int(sys.stdin.readline())
a,b,M = [0]*n,[0]*n,[]
for i in range(n):
    a[i],b[i] = map(int,sys.stdin.readline().split())
    M.append(a[i]+b[i])

cnt = 0
tflg = True
for i in range(n):
    mat = min(M)
    id = M.index(mat)
    if tflg:
        cnt += a[id]
    else:
        cnt -= b[id]
    tflg = not(tflg)
    M.pop(id)
    a.pop(id)
    b.pop(id)
print(cnt)