Buggy python submission (verdict: Time Limit Exceeded):
import sys

def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])
    tl, dl = zip(*sl)

    gmk = set()
    ce = []
    for s in sl[:k]:
        if s[0] in gmk:
            ce.append(s)
        gmk.add(s[0])

    gop = sum(dl[:k])
    bmk = max(len(set(tl)), k)

    gscr = len(gmk)**2 + gop

    ce.sort(key=lambda x: x[1])
    for c in ce[:bmk-len(gmk)]:
        for s in sl[k:]:
            if s[0] in gmk:
                continue
            k += 1
            gmk.add(s[0])
            gop = gop - c[1] + s[1]
            nscr = len(gmk)**2 + gop
            gscr = max(gscr, nscr)
            break

    return gscr


def main():
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    sl = [list(map(int, input().split())) for _ in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])

    gmk = set()
    gop = 0
    ce = []
    for ti, di in sl[:k]:
        if ti in gmk:
            ce.append(di)
        gmk.add(ti)
        gop += di

    gscr = len(gmk)**2 + gop

    for ti, di in sl[k:]:
        if 0 >= len(ce):
            break
        if ti in gmk:
            continue
        gmk.add(ti)
        odi = ce.pop()
        gop = gop - odi + di
        nscr = len(gmk)**2 + gop
        gscr = max(gscr, nscr)

    return gscr


def main():
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    sl = [tuple(map(int, input().split())) for _ in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])
    tl, dl = zip(*sl)

    gmk = set()
    ce = []
    for s in sl[:k]:
        if s[0] in gmk:
            ce.append(s)
        gmk.add(s[0])

    gop = sum(dl[:k])
    bmk = max(len(set(tl)), k)

    gscr = len(gmk)**2 + gop

    ce.sort(key=lambda x: x[1])
    for c in ce[:bmk-len(gmk)]:
        for s in sl[k:]:
            if s[0] in gmk:
                continue
            k += 1
            gmk.add(s[0])
            gop = gop - c[1] + s[1]
            nscr = len(gmk)**2 + gop
            gscr = max(gscr, nscr)
            break

    return gscr


def main():
    n, k = list(map(int, input().split()))
    sl = [list(map(int, input().split())) for i in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])

    gmk = set()
    gop = 0
    ce = []
    for ti, di in sl[:k]:
        if ti in gmk:
            ce.append(di)
        gmk.add(ti)
        gop += di

    gscr = len(gmk)**2 + gop

    for ti, di in sl[k:]:
        if 0 >= len(ce):
            break
        if ti in gmk:
            continue
        gmk.add(ti)
        odi = ce.pop()
        gop = gop - odi + di
        nscr = len(gmk)**2 + gop
        gscr = max(gscr, nscr)

    return gscr


def main():
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    sl = [tuple(map(int, input().split())) for _ in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import copy
import collections


def score(list):
    tl, dl = zip(*list)
    t_s = len(set(tl)) ** 2
    d_s = sum(dl)
    return t_s + d_s


def r(n, k, sl):
    
    tl, dl = zip(*sl)
    ztd = collections.Counter(tl)
    for _ in range(len(sl) - k):
        d_idx = dl.index(min(dl))
        tlv = tl[d_idx]
        if ztd[tlv] >= 2:
            del sl[d_idx]
        else:
            break

    for _ in range(len(sl) - k):

        ckl = {}
        for v in sl:
            if v[0] in ckl:
                if ckl[v[0]][1] > v[1]:
                    ckl[v[0]] = v
            else:
                ckl[v[0]] = v

        s_c = 0
        dtl = []
        for delckl in ckl.values():
            c_sl = copy.copy(sl)
            del c_sl[c_sl.index(delckl)]
            soe = score(c_sl)
            if s_c < soe:
                s_c = soe
                dtl = delckl

        del sl[sl.index(dtl)]

    return score(sl)


def main():
    n, k = list(map(int, input().split()))
    sl = [list(map(int, input().split())) for i in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])

    gmk = set()
    gop = 0
    ce = []
    for ti, di in sl[:k]:
        if ti in gmk:
            ce.append(di)
        gmk.add(ti)
        gop += di

    gscr = len(gmk)**2 + gop

    for ti, di in sl[k:]:
        if 0 >= len(ce):
            break
        if ti in gmk:
            continue
        gmk.add(ti)
        odi = ce.pop()
        gop = gop - odi + di
        nscr = len(gmk)**2 + gop
        gscr = max(gscr, nscr)

    return gscr


def main():
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    sl = [tuple(map(int, input().split())) for _ in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import copy


def score(list):
    tl, dl = zip(*list)
    t_s = len(set(tl)) ** 2
    d_s = sum(dl)
    return t_s + d_s


def r(n, k, sl):

    for _ in range(len(sl) - k):

        ckl = {}
        for v in sl:
            if v[0] in ckl:
                if ckl[v[0]][1] > v[1]:
                    ckl[v[0]] = v
            else:
                ckl[v[0]] = v

        s_c = 0
        dtl = []
        for delckl in ckl.values():
            c_sl = copy.copy(sl)
            del c_sl[c_sl.index(delckl)]
            soe = score(c_sl)
            if s_c < soe:
                s_c = soe
                dtl = delckl

        del sl[sl.index(dtl)]

    return score(sl)


def main():
    n, k = list(map(int, input().split()))
    sl = [list(map(int, input().split())) for i in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])

    gmk = set()
    gop = 0
    ce = []
    for ti, di in sl[:k]:
        if ti in gmk:
            ce.append(di)
        gmk.add(ti)
        gop += di

    gscr = len(gmk)**2 + gop

    for ti, di in sl[k:]:
        if 0 >= len(ce):
            break
        if ti in gmk:
            continue
        gmk.add(ti)
        odi = ce.pop()
        gop = gop - odi + di
        nscr = len(gmk)**2 + gop
        gscr = max(gscr, nscr)

    return gscr


def main():
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    sl = [tuple(map(int, input().split())) for _ in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from decimal import *
import itertools


def score(list):
    tl, dl = zip(*list)
    t_s = len(set(tl)) ** 2
    d_s = sum(dl)
    return t_s + d_s


def r(n, k, sl):
    s = 0
    for v in itertools.combinations(sl, k):
        cv = score(v)
        if cv > s:
            s = cv
    return s


def main():
    n, k = list(map(int, input().split()))
    sl = []
    for i in range(n):
        t, d = list(input().split())
        sl.append([int(t), Decimal(d)])

    print(r(n, k, sl))


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def r(n, k, sl):

    sl.sort(key=lambda x: -x[1])

    gmk = set()
    gop = 0
    ce = []
    for ti, di in sl[:k]:
        if ti in gmk:
            ce.append(di)
        gmk.add(ti)
        gop += di

    gscr = len(gmk)**2 + gop

    for ti, di in sl[k:]:
        if 0 >= len(ce):
            break
        if ti in gmk:
            continue
        gmk.add(ti)
        odi = ce.pop()
        gop = gop - odi + di
        nscr = len(gmk)**2 + gop
        gscr = max(gscr, nscr)

    return gscr


def main():
    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    sl = [tuple(map(int, input().split())) for _ in range(n)]
    print(r(n, k, sl))


if __name__ == '__main__':
    main()