Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

n, k = map(int, input().split())
td = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = sorted(td, key=itemgetter(1), reverse=True)

ans = [0]
s = []
t = []
for i in range(k):
    ans[0] += a[i][1]
    if a[i][0] not in s:
        s.append(a[i][0])
    else:
        t.append(a[i][1])

p = len(s)
q = len(t) - 1

if q < 0:
    print(ans[0] + k * k)
    exit()

v = []
for i in range(k, n):
    if a[i][0] not in s:
        s.append(a[i][0])
        v.append(a[i][1])

ans[0] += p * p
d = 0

for x in v:
    ans.append(ans[d] + x - t[q] + (p + 1) * (p + 1) - p * p)
    p += 1
    d += 1
    q -= 1
    if q < 0:
        break

print(max(ans))

Fixed submission:
from operator import itemgetter

n, k = map(int, input().split())
td = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = sorted(td, key=itemgetter(1), reverse=True)

ans = [0]
s = set()
t = []
for i in range(k):
    ans[0] += a[i][1]
    if a[i][0] not in s:
        s.add(a[i][0])
    else:
        t.append(a[i][1])

p = len(s)
q = len(t) - 1

if q < 0:
    print(ans[0] + k * k)
    exit()

v = []
for i in range(k, n):
    if a[i][0] not in s:
        s.add(a[i][0])
        v.append(a[i][1])

ans[0] += p * p
d = 0

for x in v:
    ans.append(ans[d] + x - t[q] + (p + 1) * (p + 1) - p * p)
    p += 1
    d += 1
    q -= 1
    if q < 0:
        break

print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

n, k = map(int, input().split())
td = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = sorted(td, key=itemgetter(1), reverse=True)

ans = [0]
v = []
s = []
for i in range(k):
    ans[0] += a[i][1]
    if a[i][0] not in s:
        s.append(a[i][0])
        v.append([a[i][1]])
    else:
        v[s.index(a[i][0])].append(a[i][1])
ans[0] += (len(s)) ** 2

t = []
d = []
for x, y in a[k:]:
    if x in s:
        continue
    elif x not in t:
        t.append(x)
        d.append([y])
    else:
        d[t.index(x)].append(y)

td = []
for i in range(len(t)):
    td.append((t[i], max(d[i])))

td = sorted(td, key=itemgetter(1), reverse=True)

ms = len(s)
p = 0
g = ms

for i in range(ms):
    v[i] = sorted(v[i])

#print(v)

for x, y in td:
    min_s = -1
    min_v = a[0][1]
    for i in range(ms):
        if len(v[i]) > 1:
            if min_v > v[i][0]:
                min_s = i
                min_v = v[i][0]
    if min_s == -1:
        break
    ans.append(ans[p] - min_v + y - g * g + (g + 1) * (g + 1))
    v[min_s].remove(v[min_s][0])
    g += 1
    p += 1

print(max(ans))



Fixed submission:
from operator import itemgetter

n, k = map(int, input().split())
td = [tuple(int(x) for x in input().split()) for _ in range(n)]

a = sorted(td, key=itemgetter(1), reverse=True)

ans = [0]
s = set()
t = []
for i in range(k):
    ans[0] += a[i][1]
    if a[i][0] not in s:
        s.add(a[i][0])
    else:
        t.append(a[i][1])

p = len(s)
q = len(t) - 1

if q < 0:
    print(ans[0] + k * k)
    exit()

v = []
for i in range(k, n):
    if a[i][0] not in s:
        s.add(a[i][0])
        v.append(a[i][1])

ans[0] += p * p
d = 0

for x in v:
    ans.append(ans[d] + x - t[q] + (p + 1) * (p + 1) - p * p)
    p += 1
    d += 1
    q -= 1
    if q < 0:
        break

print(max(ans))