Buggy python submission (verdict: Runtime Error):
from collections import deque


def main():
    N, K = map(int, input().split())
    sushis = [None] * N
    for i in range(N):
        t, d = map(int, input().split())
        sushis[i] = (t, d)
    sushis.sort(key=lambda a: (-a[1], a[0]))
    tmp = 0
    types = set()
    stack = deque()
    ans = 0
    for i in range(len(sushis)):
        t, d = sushis[i]
        if i < K:
            if t in types:
                stack.append(d)
            types.add(t)
            tmp += d
        else:
            if t not in types:
                types.add(t)
                tmp += d
                tmp -= stack.pop()
        ans = max(ans, tmp + len(types)**2)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    N, K = map(int, input().split())
    sushis = [None] * N
    for i in range(N):
        t, d = map(int, input().split())
        sushis[i] = (t, d)
    sushis.sort(key=lambda a: (-a[1], a[0]))
    tmp = 0
    types = set()
    stack = deque()
    ans = 0
    for i in range(len(sushis)):
        t, d = sushis[i]
        if i < K:
            if t in types:
                stack.append(d)
            types.add(t)
            tmp += d
        else:
            if t not in types and len(stack):
                types.add(t)
                tmp += d
                tmp -= stack.pop()
        ans = max(ans, tmp + len(types)**2)
    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K = map(int, input().split())
    sushis = [[] for _ in range(N)]
    for i in range(N):
        t, d = map(int, input().split())
        sushis[t-1].append(d)
    for i in range(N):
        sushis[i].sort(reverse=True)
        cumsum = 0
        for j in range(len(sushis[i])):
            cumsum += sushis[i][j]
            sushis[i][j] = cumsum
    dp = [[None] * (K+1) for _ in range(2)]
    dp[1][0] = (0, 0)
    dp[0][0] = (0, 0)
    for i in range(N):
        dp[i % 2] = [x for x in dp[(i-1) % 2]]
        for j in range(len(sushis[i])):
            for k in range(K-j):
                if dp[(i-1) % 2][k] is None:
                    continue
                pt, pd = dp[(i-1) % 2][k]
                if dp[i % 2][k+j+1] is None or pd + sushis[i][j] + 2 * pt + 1 >= dp[i % 2][k+j+1][1]:
                    dp[i % 2][k+j+1] = (pt+1, pd + sushis[i][j] + 2 * pt + 1)
    print(dp[(N-1) % 2][K][1])


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    N, K = map(int, input().split())
    sushis = [None] * N
    for i in range(N):
        t, d = map(int, input().split())
        sushis[i] = (t, d)
    sushis.sort(key=lambda a: (-a[1], a[0]))
    tmp = 0
    types = set()
    stack = deque()
    ans = 0
    for i in range(len(sushis)):
        t, d = sushis[i]
        if i < K:
            if t in types:
                stack.append(d)
            types.add(t)
            tmp += d
        else:
            if t not in types and len(stack):
                types.add(t)
                tmp += d
                tmp -= stack.pop()
        ans = max(ans, tmp + len(types)**2)
    print(ans)


if __name__ == "__main__":
    main()