Buggy python submission (verdict: Runtime Error):
import numpy as np
N, K = map(int,input().split())
td = np.empty((N,2),dtype=np.int64)
for i in range(N):
    td[i] = list(map(int,input().split()))

t = td[:,0]-1
d = td[:,1]
order1=np.argsort(d)[::-1]
mins = len(set(t[order1[:K]]))

best = -np.ones(N,dtype=np.int64)
for i in range(N):
    if best[t[i]] < 0 or d[i] > d[best[t[i]]]:
        best[t[i]]=i

bestmenu = np.array(list(set(best) - set([-1])),dtype=np.int64)
othermenu = np.array(list(set(range(N)) - set(bestmenu)),dtype=np.int64)

maxs = min(len(bestmenu),K)
ans = 0
bests=0
sorted_d_best=np.sort(d[bestmenu])
sorted_d_other=np.sort(d[othermenu])
bo
for s in range(mins,maxs+1):
    score = s*s + sorted_d_best[-s:].sum() + sorted_d_other[-(K-s):].sum()
    if score > ans:
        bests=s
        ans = score

print(ans)

Fixed submission:
import numpy as np
N, K = map(int,input().split())
td = np.empty((N,2),dtype=np.int64)
for i in range(N):
    td[i] = list(map(int,input().split()))

t = td[:,0]-1
d = td[:,1]
order1=np.argsort(d)[::-1]
mins = len(set(t[order1[:K]]))

best = -np.ones(N,dtype=np.int64)
for i in range(N):
    if best[t[i]] < 0 or d[i] > d[best[t[i]]]:
        best[t[i]]=i

bestmenu = np.array(list(set(best) - set([-1])),dtype=np.int64)
othermenu = np.array(list(set(range(N)) - set(bestmenu)),dtype=np.int64)

maxs = min(len(bestmenu),K)
ans = 0
bests=0
sorted_d_best=np.sort(d[bestmenu])[::-1]
sorted_d_other=np.sort(d[othermenu])[::-1]
for s in range(mins,maxs+1):
    score = s*s + sorted_d_best[:s].sum() + sorted_d_other[:(K-s)].sum()
    if score > ans:
        bests=s
        ans = score

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, K = map(int,input().split())
td = np.empty((N,2),dtype=np.int64)
for i in range(N):
    td[i] = list(map(int,input().split()))

t = td[:,0]-1
d = td[:,1]
order1=np.argsort(d)[::-1]
mins = len(set(t[order1[:K]]))

best = -np.ones(N,dtype=np.int64)
for i in range(N):
    if best[t[i]] < 0 or d[i] > d[best[t[i]]]:
        best[t[i]]=i

bestmenu = np.array(list(set(best) - set([-1])),dtype=np.int64)
othermenu = np.array(list(set(range(N)) - set(bestmenu)),dtype=np.int64)

maxs = min(len(bestmenu),K)
ans = 0
bests=0
for s in range(mins,maxs+1):
    score = s*s + np.sort(d[bestmenu])[::-1][:s].sum() + np.sort(d[othermenu])[::-1][:(K-s)].sum()
    if score > ans:
        bests=s
        ans = score

print(ans)

Fixed submission:
import numpy as np
N, K = map(int,input().split())
td = np.empty((N,2),dtype=np.int64)
for i in range(N):
    td[i] = list(map(int,input().split()))

t = td[:,0]-1
d = td[:,1]
order1=np.argsort(d)[::-1]
mins = len(set(t[order1[:K]]))

best = -np.ones(N,dtype=np.int64)
for i in range(N):
    if best[t[i]] < 0 or d[i] > d[best[t[i]]]:
        best[t[i]]=i

bestmenu = np.array(list(set(best) - set([-1])),dtype=np.int64)
othermenu = np.array(list(set(range(N)) - set(bestmenu)),dtype=np.int64)

maxs = min(len(bestmenu),K)
ans = 0
bests=0
sorted_d_best=np.sort(d[bestmenu])[::-1]
sorted_d_other=np.sort(d[othermenu])[::-1]
for s in range(mins,maxs+1):
    score = s*s + sorted_d_best[:s].sum() + sorted_d_other[:(K-s)].sum()
    if score > ans:
        bests=s
        ans = score

print(ans)