Buggy python submission (verdict: Runtime Error):
import sys

def point(sushi):
  d = 0
  t = []
  for s in sushi:
    d += s[1]
    t.append(s[0])
  return d + len(set(t))**2


def rm_ate(sushi, rest):
  t = set([s[0] for s in sushi])
  return [r for r in rest if not r[0] in t]


def rm_dup(sushi):
  sushi = sorted(sushi, key=lambda x:-x[1])
  d = []
  r = []
  for s in sushi:
    if s[0] in d:
      r.append(s)
    else:
      d.append(s[0])
  sushi.remove(r[-1])
  return sushi


def main():
  input = sys.stdin.readline
  n, k = map(int, input().split())
  sushi = []
  for _ in range(n):
    t, d = map(int, input().split())
    sushi.append([t,d])
  sushi = sorted(sushi, key=lambda x:x[1])
  temp = sushi[-k:]
  rest = rm_ate(temp, sushi[:-k])
  ans = point(temp)
  while rest:
    temp = rm_dup(temp)
    temp += [rest.pop()]
    res = point(temp)
    ans = max(ans, res)
    rest = rm_ate(temp, rest)
  print(ans)


if __name__ == '__main__':
  main()

Fixed submission:
import sys

def point(sushi, n, k):
  sushi = sorted(sushi, key=lambda x:-x[1])
  
  ate_t = set()
  dpoint = 0
  dup = []
  for s in sushi[:k]:
    t = s[0]
    d = s[1]
    if t in ate_t:
      dup.append(d)
    ate_t.add(t)
    dpoint += d
  
  ans = len(ate_t)**2 + dpoint
  
  for s in sushi[k:]:
    t = s[0]
    d = s[1]
    if len(dup) <= 0:
      break
    if t in ate_t:
      continue
    ate_t.add(t)
    rm = dup.pop()
    dpoint = dpoint - rm + d
    res = len(ate_t)**2 + dpoint
    ans = max(ans, res)
  
  return ans

def main():
  input = sys.stdin.readline
  n, k = map(int, input().split())
  sushi = []
  for _ in range(n):
    t, d = map(int, input().split())
    sushi.append([t,d])
  print(point(sushi, n, k))


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def get_point(l):
  deli = 0
  kind = set()
  for x in l:
    kind = kind | set([x[0]])
    deli += x[1]
  
  point = deli + len(kind) * len(kind)
  return point, kind


def remove_deplicated_kind(l, kind):
  l = [[x[0], x[1]] for x in l if x[0] not in kind]
  return l


def replace_data(curr, rest):
  alt = [rest[0]] + curr[:-1]
  replaced = curr[-1]
  _, kind1 = get_point(curr)
  _, kind2 = get_point(alt)
  
  i = 0
  while len(kind1) >= len(kind2):
    temp = [replaced] + alt[:-1]
    replaced = alt[-1]
    alt = temp
    _, kind2 = get_point(alt)
    i += 1
    if i >= len(curr):
      break
  
  return alt


n, k = map(int, input().split())
l = []
for _ in range(n):
  t, d = map(int, input().split())
  l.append([t, d])

l = sorted(l, key=lambda x: -x[1])
curr = l[:k]
rest = l[k:]

p, kind1 = get_point(curr)
ans = [p]

while len(rest) > 0:
  curr = replace_data(curr, rest)
  p, kind = get_point(curr)
  rest = rest[1:]
  rest = remove_deplicated_kind(rest, kind)
  ans.append(p)

print(max(ans))

Fixed submission:
import sys

def point(sushi, n, k):
  sushi = sorted(sushi, key=lambda x:-x[1])
  
  ate_t = set()
  dpoint = 0
  dup = []
  for s in sushi[:k]:
    t = s[0]
    d = s[1]
    if t in ate_t:
      dup.append(d)
    ate_t.add(t)
    dpoint += d
  
  ans = len(ate_t)**2 + dpoint
  
  for s in sushi[k:]:
    t = s[0]
    d = s[1]
    if len(dup) <= 0:
      break
    if t in ate_t:
      continue
    ate_t.add(t)
    rm = dup.pop()
    dpoint = dpoint - rm + d
    res = len(ate_t)**2 + dpoint
    ans = max(ans, res)
  
  return ans

def main():
  input = sys.stdin.readline
  n, k = map(int, input().split())
  sushi = []
  for _ in range(n):
    t, d = map(int, input().split())
    sushi.append([t,d])
  print(point(sushi, n, k))


if __name__ == '__main__':
  main()