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:
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 H != []:
    C = sorted(H, key = itemgetter(0, 1), reverse = True)
    A = sorted(H, key = itemgetter(1, 0), reverse = True)
    if turn % 2 == 0: #chokudai 
        if C[0][0] - A[0][0] >= A[0][1] - C[0][1]:
            chokudai = C.pop(0)
        else:
            chokudai = A.pop(0)
        ans_c += chokudai[0]
        H.remove(chokudai)
    else: #aoki
        if A[0][1] - C[0][1] >= C[0][0] - A[0][0]:
            aoki = A.pop(0)
        else:
            aoki = C.pop(0)
        ans_a += aoki[1]
        H.remove(aoki)
    turn += 1

print(ans_c - ans_a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dishes_a = list()
dishes_b = list()
dishes_diff = list()
for i in range(0, n):
    a, b = 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())
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
A.sort
countA = 0
countB = 0
flg = 0
for i in range (N):
  if flg%2 == 0:
    if min(A_) >= 0:
      x = A_.index(max(A_))
    else:
      x = A_.index(min(A_))
    countA = countA + A[x]
  else:
    x = B_.index(max(B_))
    countB = countB + B[x]
  A.pop(x)
  B.pop(x)
  A_.pop(x)
  B_.pop(x)
  flg = flg + 1

print(countA - countB)

Submission status: Time Limit Exceeded

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

PA=0
PB=0

LD=[0]*N
for i in range(N):
    LD[i]=LA[i]+LB[i]


SLD=sorted(LD)
SLD.reverse()

for i,x in enumerate(SLD):
    if i%2==0:
        PA+=LA[LD.index(x)]
    else:
        PB+=LB[LD.index(x)]

print(PA-PB)

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])
    
    # 自分に一番良い手と相手にとって一番良い手を考える
    # 自分にとって一番良い手が複数ある場合、その中で相手が一番良い手を選ぶ
    # 自分にとって一番良い手が１つの場合、相手にとって一番良い手との比較をして、差が大きい方を選ぶ
    # 自分にとって良いてと相手にとっていい手が同じ場合(相手にとっていい手が複数ある場合も)、それを選ぶ
    sub(N, A, B)


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:
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.append([p[0], p[1]])

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

print(a_point - b_point)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [ [], [] ]
s = []
for i in range(n):
  r = input().split()
  a[0].append(int(r[0]))
  a[1].append(int(r[1]))
  s.append(int(r[0]) + int(r[1]))
akoufuku = [0, 0]

idx2 = 0
for i in range(n):
  nmax = max(s)
  idx = s.index(nmax)
  akoufuku[idx2] += a[idx2][idx]
  idx2 = 1 - idx2
  del a[0][idx]
  del a[1][idx]
  del s[idx]

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

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[0], reverse = True)
        m = ab.index(max(ab,key=lambda x: x[1]))
        a += ab[m][0]
    else:
        ab = sorted(ab, key=lambda x : x[1], reverse = True)
        m = ab.index(max(ab, key=lambda x: x[0]))
        b += ab[m][1]
    del ab[m]
print(a-b)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
tlist=[]
alist=[]
dlist=[]

for i in range(0,N):
    t,a=map(int,input().split(" "))
    d=t+a
    tlist.append(t)
    alist.append(a)
    dlist.append(d)
#print(tlist,alist,dlist)

count=0
tscore=0
ascore=0
number=0

for count in range(0,N):
    if count%2==0:
        number=dlist.index(max(dlist))
        tscore+=tlist[number]
    else:
        number=dlist.index(max(dlist))
        ascore+=alist[number]

    #print(tscore,ascore)
    del tlist[number]
    del alist[number]
    del dlist[number]      

print(tscore-ascore)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = []
S_ta = []
S_ao = []


for i in range(N):
    a, b = map(int, input().split())
    tmp = [a, b]
    S.append(tmp)
    S_ta.append(tmp)
    S_ao.append(tmp)
    
ta = 0
ao = 0

S_ta.sort(key=lambda x:(x[0], x[1]))
S_ao.sort(key=lambda x:(x[1], x[0]))
S_ta.reverse()  
S_ao.reverse()  

al_num = []
  
  
for i in range(N):
    if i%2==0:
        
        f = 1
        num = 0
        al = S_ta[0]
        now = 0
           
        ta = ta+S_ta[now][0]
        
        for j in range(len(S_ao)-1, -1, -1):
            if S_ta[now]==S_ao[j]:
                del S_ao[j]
                break
        del S_ta[now]
            
    else:
        f = 1
        num = 0
        al = S_ao[0]
        now = 0
                   
        ao = ao+S_ao[now][1]
        
        for j in range(len(S_ta)-1, -1, -1):
            if S_ao[now]==S_ta[j]:
                del S_ta[j]
                break
        del S_ao[now]

print(ta-ao)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
#al = [i[0] for i in ab]
#bl = [i[1] for i in ab]

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

ind = 0

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

a_happy = 0
b_happy = 0
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(A):
                A_flag = 1
            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
            if set_list(B):
                B_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)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dishes_a = list()
dishes_b = list()
dishes_diff = list()
for i in range(0, n):
    a, b = 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
diff_flag = True
index = 0
for i in range(0, n):
    if i % 2 == 0:
        if max(dishes_diff) != 0:
            index = dishes_diff.index(max(dishes_diff))
        else:
            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:
# -*- coding: utf-8 -*-
import numpy as np

def main():
    """Function."""
    n = int(input())

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

    sorted_vals = sorted(vals, key=lambda x: (x[0], x[1]))
    # print(sorted_vals)

    takahashi = 0
    for _ in range(n):
        # print(sorted_vals)
        if sorted_vals:
            takahashi += sorted_vals[-1][0]
            del sorted_vals[-1]
        else:
            break

        if sorted_vals:
            max_val, max_key = -1, None
            for i in range(len(sorted_vals)):
                if sorted_vals[i][1] >= max_val:
                    max_val = sorted_vals[i][1]
                    max_key = i
            takahashi -= max_val
            del sorted_vals[max_key]

        else:
            break

    print(takahashi)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

#a_b = [(int(i[0]), int(i[1])) for i in input().split()]
#tkh = [(int(i[0])-int(i[1]),ind) for ind,i in enumerate(a_b)]
tkh.sort(key = lambda x : x[2])
tkh.sort(key = lambda x : x[1])
tkh.sort(key = lambda x : x[0])

takahashi, aoki, =0,0

for i in range(n):
    if i%2 == 0:
        takahashi += tkh.pop()[1]
        tkh.sort(key = lambda x : x[1])
        tkh.sort(key = lambda x : x[2])
        tkh.sort(key = lambda x : x[0])

    else:
        aoki += tkh.pop()[2]
        tkh.sort(key = lambda x : x[2])
        tkh.sort(key = lambda x : x[1])
        tkh.sort(key = lambda x : x[0])

print(takahashi-aoki)

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

while C != []:
    if C[0][0] - A[0][0] >= A[0][1] - C[0][1]:
        chokudai = C.pop(0)
        ans_c += chokudai[0]
        A.remove(chokudai)
    else:
        chokudai = A.pop(0)
        ans_c += chokudai[0]
        C.remove(chokudai)
    if C == []:
        print(ans_c - ans_a)
        exit()
    if A[0][1] - C[0][1] >= C[0][0] - A[0][0]:
        aoki = A.pop(0)
        ans_a += aoki[1]
        C.remove(aoki)
    else:
        aoki = C.pop(0)
        ans_a += aoki[1]
        A.remove(aoki)
    if C == []:
        print(ans_c - ans_a)
        exit()

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))]
    max_indices = [i for i, x in enumerate(diff) if x == max(diff)]
    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:
n = int(input())
a = []
b = []
s = []
for i in range(n):
  r = input().split()
  a.append(int(r[0]))
  b.append(int(r[1]))
  s.append(int(r[0]) + int(r[1]))
akoufuku = 0
bkoufuku = 0
for i in range(n):
  nmax = max(s)
  idx = s.index(nmax)
  if i % 2 == 0:
    akoufuku += a[idx]
  else:
    bkoufuku += b[idx]
  del a[idx]
  del b[idx]
  del s[idx]

print(akoufuku - bkoufuku)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[list(map(int,input().split(" "))) for k in range(n)]
ex=[]
for i in range(1,n):
  j=i
  while s[j][0]+s[j][1]>s[j-1][0]+s[j-1][1]:
    ex=s[j]
    s[j]=s[j-1]
    s[j-1]=ex
    j-=1
    if j==0:
      break
c=0
for l in range(n):
  if l%2==0:
    c+=s[l][0]
  else:
    c-=s[l][1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

kishutsu = []

score_diff_t = [[ryouri[i][0] - ryouri[i][1], ryouri[i][0], i] for i in range(n)]
score_diff_t = sorted(score_diff_t, key=lambda x: (x[0], x[1]*(-1)))
score_diff_a = [[ryouri[i][1] - ryouri[i][0], ryouri[i][0], i] for i in range(n)]
score_diff_a = sorted(score_diff_a, key=lambda x: (x[0], x[1]), reverse=True)

score_t = 0
score_a = 0

for i in range(n):
    if i % 2 == 0:
        for x in score_diff_t:
            idx = x[2]
            if idx not in kishutsu:
                score_t += ryouri[idx][0]
                kishutsu.append(idx)
                break
    else:
        for x in score_diff_a:
            idx = x[2]
            if idx not in kishutsu:
                score_a += ryouri[idx][1]
                kishutsu.append(idx)
                break
        
print(score_t - score_a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
#al = [i[0] for i in ab]
#bl = [i[1] for i in ab]

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = []
S_ta = []
S_ao = []


for i in range(N):
    a, b = map(int, input().split())
    tmp = [a, b]
    S.append(tmp)
    S_ta.append(tmp)
    S_ao.append(tmp)
    
ta = 0
ao = 0

S_ta.sort(key=lambda x:x[0])
S_ao.sort(key=lambda x:x[1])
S_ta.reverse()  
S_ao.reverse()  

al_num = []
  
  
for i in range(N):
    if i%2==0:
        
        f = 1
        num = 0
        al = S_ta[0]
        now = 0
        
        while num<len(S_ta)-1 and f>0:
            if S_ta[num][0]!=S_ta[num+1][0]:
                f = -1
            else:
                if S_ta[num+1][1]>al[1]:
                    al = S_ta[num+1]
                    now = num+1
                num = num+1
           
        ta = ta+S_ta[now][0]
        
        for j in range(len(S_ao)-1, -1, -1):
            if S_ta[now]==S_ao[j]:
                del S_ao[j]
                break
        del S_ta[now]
            
    else:
        f = 1
        num = 0
        al = S_ao[0]
        now = 0
        
        while num<len(S_ao)-1 and f>0:
            if S_ao[num][1]!=S_ao[num+1][1]:
                f = -1
            else:
                if S_ao[num+1][0]>al[0]:
                    al = S_ao[num+1]
                    now = num+1
                num = num+1
           
        ao = ao+S_ao[now][1]
        
        for j in range(len(S_ta)-1, -1, -1):
            if S_ao[now]==S_ta[j]:
                del S_ta[j]
                break
        del S_ao[now]

print(ta-ao)

Submission status: Time Limit Exceeded

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

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

taka=0
ao=0

for i in range(n):
    value=0
    diff=-10000000000
    ind=0
    for j in range(len(a)):
        if(i%2==0 and diff+value <= a[j][2]+a[j][0] and value <= a[j][0]):
            value=a[j][0]
            diff=a[j][2]
            ind=j
        elif(i%2!=0 and diff+value <= a[j][3]+a[j][1] and value <= a[j][1]):
            value=a[j][1]
            diff=a[j][3]
            ind=j
    if(i%2 == 0):
        taka += value
    else:
        ao += value
    a.pop(ind)

print(taka-ao)

Submission status: Time Limit Exceeded

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

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

print(taka - aoki)

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 len(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.pop(i)
        b.pop(i)
        ta = a
        a = b
        b = ta
        s *= -1


main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[0]*n
b=[0]*n
c=[0]
d=[0]
for i in range(n):
	a[i],b[i]=map(int,input().split())
	for j in range(len(c)):
		if a[i]+b[i]>=c[j]:
			c.insert(j,a[i]+b[i])
			d.insert(j,i)
			break
ta=0
ao=0
turn=0
for i in range(n):
	if turn==0:
		ta+=a[d[i]]
		turn=1
	else:
		ao+=b[d[i]]
		turn=0
print(ta-ao)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
sara = []
t_s = 0
a_s = 0
for i in range(N):
    A, B = map(int, input().split())
    sara.append([A, B])
while True:
    if len(sara) == 0:
        break
    sara.sort(key=lambda x:(x[0],x[1]-x[0]), reverse=True)
    res = sara.pop(0)
    t_s += res[0]
    if len(sara) == 0:
        break
    sara.sort(key=lambda x:(x[1],x[0]-x[1]), reverse=True)
    res = sara.pop(0)
    a_s += res[1]
print(t_s - a_s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
earned = np.zeros((N,1),dtype=np.int64)
loss = np.zeros((N,1),dtype=np.int64)
scores = np.zeros((N,1),dtype=np.int64)
for i in range(N):
    a, b = map(np.int64, input().split())
    earned[i] = a
    loss[i] = b
    scores[i] = a+b
 
order = [i[0] for i in sorted(enumerate(scores), key=lambda x:x[1])]
result = 0
for ind, item in enumerate(order):
    if ind%2==0:
        result += earned[item]
    else:
        result -= loss[item]
 
print(result[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
AB = []
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):
        AA = max(A)
        indexes = [j for j, x in enumerate(A) if x == AA]
        L = len(indexes)
        ab = []
        MAX = 0
        for k in range(L):
            if MAX <= B[indexes[k]]:
                MAX = B[indexes[k]]
                Index = indexes[k]
        
        if i%2 == 0:
            acount+=A.pop(Index)
            trash = B.pop(Index)
        else:
            bcount+=B.pop(Index)
            trash = A.pop(Index)
    else:
        BB = max(B)
        indexes = [j for j, x in enumerate(B) if x == BB]
        L = len(indexes)
        ab = []
        MAX = 0
        for k in range(L):
            if MAX <= A[indexes[k]]:
                MAX = A[indexes[k]]
                Index = indexes[k]
        
        if i%2 == 0:
            acount+=A.pop(Index)
            trash = B.pop(Index)
        else:
            bcount+=B.pop(Index)
            trash = A.pop(Index)
print(acount-bcount)

Submission status: Time Limit Exceeded

Solution code:
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):  
    if max(A)>max(B):
        index=A.index(max(A))
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    else:
        index=B.index(max(B))
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    taka_point+=point
    #print(point)
    
    if max(A)>max(B):
        index=A.index(max(A))
        point=d[index][1]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    else:
        index=B.index(max(B))
        point=d[index][1]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    aoki_point+=point
    #print(point)
    
if N%2==1:
    if max(A)>max(B):
        index=A.index(max(A))
        point=d[index][0]
        A.pop(index)
        B.pop(index)
        d.pop(index)
    else:
        index=B.index(max(B))
        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())  #数値入力
A=[]
B=[]
for i in range(N):
  a,b=map(int, input().split())  #複数数値入力
  A.append(a)
  B.append(b)

A_point=0
B_point=0

count=0

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

print(A_point-B_point)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

list_score = [[] for i in range(N)]

for i in range(N):
    s = input().split()
    A_i = int(s[0])
    B_i = int(s[1])
    Diff_i = A_i - B_i
    list_score[i] = [A_i, B_i, Diff_i]

sum_score_A = 0
sum_score_B = 0

import numpy as np
arr_2d = np.array(list_score)
#print(arr_2d)
#print(arr_2d[:,2])

turn_player = "A"

for i in range(N):
    
    if turn_player == "A":
        # 最大値を探す
        #print("playerA's turn")
        #print(arr_2d)
        min_index = arr_2d[:,2].argmin()
        sum_score_A += arr_2d[min_index ,0]
        arr_2d = np.delete(arr_2d, min_index, 0)
        turn_player = "B"
        #print(sum_score_A, sum_score_B)
    elif turn_player == "B":
        # 最小値を探す
        #print("playerB's turn")
        #print(arr_2d)        
        min_index = arr_2d[:,2].argmin()
        sum_score_B += arr_2d[min_index,1]
        arr_2d = np.delete(arr_2d, min_index, 0)
        turn_player = "A"
        #print(sum_score_A, sum_score_B)

print(sum_score_A - sum_score_B)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

###### input ######
n = int(input())
taka = []
aoki = []
for i in range(n):
  t,a= map(int,input().split())
  taka.append(t)
  aoki.append(a)

###### main ######
res = 0
for i in range(n):
  if i % 2 == 0: # turn TAKAHASHI
    if max(taka) < max(aoki):
      tmp = aoki.index(max(aoki))#[0]
    else:
      tmp = taka.index(max(taka))#[0]
    #print(tmp)
    res+= taka[tmp]
    taka[tmp] = 0
    aoki[tmp] = 0

  else:
    if max(taka) > max(aoki):
      tmp = taka.index(max(taka))#[0]
    else:
      tmp = aoki.index(max(aoki))#[0]  
    #print(tmp)
    res -= aoki[tmp]
    taka[tmp] = 0
    aoki[tmp] = 0

print(res)

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 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]
        tag=not tag
        a,b=b,a
        a.remove(tmp)
        b.remove(tmp)
    return pa-pb
    
print(func(a,b))

Submission status: Time Limit Exceeded

Solution code:
import numpy

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


AA = list(numpy.argsort(A))[::-1]
BB = list(numpy.argsort(B))[::-1]
aans,bans = 0,0
while True:
    a = AA.pop(0)
    aans += A[a]
    if BB[0] == a:
        BB.pop(0)
    if not BB:
        break
    b = BB.pop(0)
    bans += B[b]
    if AA[0] == b:
        AA.pop(0)
    if not AA:
        break

print(aans - bans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import functools

def cmp1(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (t1 - a0) - (t0 - a1)

def cmp2(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (a1 - t0) - (a0 - t1)

n = int(input())
AB = [tuple([i] + [int(x) for x in input().split()]) for i in range(n)]
# hey atcoder Python 3.4 is too old

A = sorted(AB, key=functools.cmp_to_key(cmp1))
B = sorted(AB, key=functools.cmp_to_key(cmp2))
used = {-1}
a, b = -1, -1
ans = 0

for _ in range(n // 2):
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

    while b in used:
        b, _, s = B.pop(0)
    used.add(b)
    ans -= s

if n % 2 == 1:
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

print(ans)

Submission status: Time Limit Exceeded

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