Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02686 (python)

Fixed submission:
import sys
input = sys.stdin.readline

n = int(input())
cnt = 0
decrement = 0
profit = []
loss = []

for _ in range(n):
    s = input().rstrip()
    min_p = 0
    end_p = 0
    point = 0
    for c in s:
        if c == '(':
            point += 1
        elif c == ')':
            point -= 1
            if point < min_p:
                min_p = point
    else:
        end_p = point
    if 0 <= min_p:
        cnt += end_p
    else:
        if min_p == end_p:
            decrement += end_p
        else:
            if 0 <= end_p:
                profit.append((min_p, end_p))
            else:
                loss.append((min_p, end_p))



profit.sort()
while profit:
    min_p, end_p = profit.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p


loss.sort(reverse=True)
while loss:
    min_p, end_p = loss.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p

print('Yes' if cnt+decrement == 0 else 'No')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n = int(input())
cnt = 0
decrement = 0
profit = []
loss = []

for _ in range(n):
    s = input()
    min_p = 0
    end_p = 0
    point = 0
    for c in s:
        if c == '(':
            point += 1
        elif c == ')':
            point -= 1
            if point < min_p:
                min_p = point
    else:
        end_p = point
    if 0 <= min_p:
        cnt += end_p
    else:
        if min_p == end_p:
            decrement += end_p
        else:
            if 0 <= end_p:
                profit.append((min_p, end_p))
            else:
                loss.append((min_p, end_p))


profit.sort(reverse=True)
while profit:
    min_p, end_p = profit.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p


loss.sort()
while loss:
    min_p, end_p = profit.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p

print('Yes' if cnt+decrement == 0 else 'No')
Problem p02714 (python)

Fixed submission:
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
if n < 3:
    print(0)
    exit()
elif len(c.keys()) < 3:
    print(0)
    exit()
ans = 0
for i in range(n):
    x = s[i]
    ans += mul(*[v for k, v in c.items()if k != x])
    c[x] -= 1
    for j in range(i+1, n):
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
if n < 3:
    print(0)
    exit()

ans = 0
for i in range(n):
    x = s[i]
    ans += mul(*[v for k, v in c.items()if k != x])
    c[x] -= 1
    for j in range(i+1, n):
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    ans += mul(*[v for k, v in c.items()if k != s[i]])
    c[s[i]] -= 1
    for j in range(i+1, n):
        x = s[i]
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)
Problem p02845 (python)

Fixed submission:
MOD = 10**9+7
n = int(input())
A = list(map(int, input().split()))

colors = [0]*3
dp = [0]*n
for i in range(n):
    c = colors.count(A[i])
    dp[i] = c
    if c == 0:
        break
    colors[colors.index(A[i])] += 1

ans = 1
for x in dp:
    ans = ans*x % MOD
print(ans % MOD)

Buggy submission (verdict: Runtime Error):
MOD = 10**9+7
n = int(input())
A = list(map(int, input().split()))

colors = [0]*3
dp = [0]*n
for i in range(n):
    c = colors.count(A[i])
    dp[i] = c
    colors[colors.index(A[i])] += 1
ans = 1
for x in dp:
    ans = ans*x % MOD
print(ans % MOD)
Problem p02918 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
s = input().rstrip()

def count(s):
    cnt = 0
    for i in range(n):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(min(count(s)+2*k, n-1))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()


def split(s):
    last = ''
    new = []
    for x in s:
        if last != x:
            new.append(x)
        else:
            new[-1] += x
        last = x
    return new


def transform(s, k):

    for _ in range(k):
        s = split(s)
        ls = list(map(len, s))
        idx = ls.index(min(ls))
        if idx == 0:
            idx = ls[1:].index(min(ls[1:]))+1

        # print(s, idx, ls)
        s[idx] = s[idx].replace('L', 'X').replace('R', 'L').replace('X', 'R')

        if ''.join(s) == s:
            break
        else:
            s = ''.join(s)
    # print(s)
    return s


s = transform(s, k)


def count(s):
    cnt = 0
    for i in range(len(s)):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(count(s))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()


def split(s):
    last = ''
    new = []
    for x in s:
        if last != x:
            new.append(x)
        else:
            new[-1] += x
        last = x
    return new


def transform(s, k):

    for _ in range(k):
        s = split(s)
        ls = list(map(len, s))
        idx = ls.index(min(ls))
        if idx == 0:
            idx = ls[1:].index(min(ls[1:]))+1

        # print(s, idx, ls)
        s[idx] = s[idx].replace('L', 'X').replace('R', 'L').replace('X', 'R')
        s = ''.join(s)
    # print(s)
    return s


s = transform(s, k)


def count(s):
    cnt = 0
    for i in range(len(s)):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(count(s))
Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, q = map(int, input().split())

graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)

values = [0]*n
for _ in range(q):
    p, x = map(int, input().split())
    values[p-1] += x

def dfs(v1, parent, add):
    values[v1] += add
    for v2 in graph[v1]:
        if v2 == parent:
            continue
        dfs(v2, v1, values[v1])

dfs(0, 0, 0)
print(*values)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)

values = [0]*n
for _ in range(q):
    p, x = map(int, input().split())
    values[p-1] += x

def dfs(v1, parent, add):
    values[v1] += add
    for v2 in graph[v1]:
        if v2 == parent:
            continue
        dfs(v2, v1, values[v1])


dfs(0, 0, 0)

print(*values)
Problem p02949 (python)

Fixed submission:
import sys
input = sys.stdin.readline
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)


can_reach = [0]*n
stack = [0]
while stack:
    v = stack.pop()
    if can_reach[v]:
        continue
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if can_reach[v2]:
            continue
        stack.append(v2)


INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        if not can_reach[v2]:
            continue
        dist[v2] = -INF
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))

Buggy submission (verdict: Runtime Error):
N, M, P = map(int, input().split())

class Edge:
    def __init__(self, from_, to_, point):
        self.from_, self.to_, self.point = from_, to_, point
    
edge_list = [Edge(*[int(s)for s in input().split()]) for _ in range(M)]
end_point = max(edge.to_ for edge in edge_list)
end_edge_list = [edge for edge in edge_list if edge.to_ == end_point]


def backToStart(edge, T=1, via=[], infinite=False):
    if edge.from_ == 1:
        return edge, T, via, infinite
    if edge.to_ == edge.from_:
        if edge.point - P > 0:
            ls = [edge_ for edge_ in edge_list if edge_.to_ == edge.from_ and edge_.to_ != edge_.from_]
            if len(ls) == 1:
                return backToStart(ls[0], T+1, infinite=True)
            else:
                return [backToStart(edge, T+1, infinite=True) for edge in ls]
        else:
            return "DEADEND"
    edge_list_ = [edge_ for edge_ in edge_list if edge_.to_ == edge.from_]
    via_ = via[:]
    via_.append(edge)
    if len(edge_list_) == 1:
        return backToStart(edge_list_[0], T+1, via_)
    else:
        return [backToStart(edge, T+1, via_) for edge in edge_list_]

def multiEdges(edge_list):
    return [backToStart(edge) for edge in edge_list]

origin_edge_list = multiEdges(end_edge_list)

# print(origin_edge_list)

while True:
    if 'DEADEND' in origin_edge_list:
        origin_edge_list.remove('DEADEND')
    elif [] in origin_edge_list:
        origin_edge_list.remove([])
    else:
        break

def hoge():
    point_list = []
    for route in origin_edge_list:
        if route[3]:
            return -1
        ls = route[2][:]
        ls.append(route[0])
        point_list.append(sum(e.point for e in ls) - route[1] * P)
    return point_list

ans = hoge()
if isinstance(ans, int):
    print(ans)
else:
    ans.append(0)
    print(max(ans))
Problem p02985 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = list(map(lambda x: int(x)-1, input().split()))
    edges[a].append(b)
    edges[b].append(a)
MOD = 10**9+7
INF = 10**10
dp = [INF]*n


def dfs(v, parent, x):
    dp[v] = x
    if parent == -1:
        next_x = k-1
    else:
        next_x = k-2
    for v2 in edges[v]:
        if v2 == parent:
            continue
        dfs(v2, v, next_x)
        next_x -= 1


dfs(0, -1, k)
ans = 1
for x in dp:
    ans = ans*x % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = list(map(lambda x: int(x)-1, input().split()))
    edges[a].append(b)
    edges[b].append(a)
MOD = 10**9+7
INF = 10**10
dp = [INF]*n

def dfs(v, parent, x):
    dp[v] = x
    if parent == -1:
        next_x = k-1
    else:
        next_x = k-2
    for v2 in edges[v]:
        if v2 == parent:
            continue
        dfs(v2, v, next_x)
        next_x -= 1


dfs(0, -1, k)
ans = 1
for x in dp:
    ans = ans*x % MOD
print(ans)
Problem p03006 (python)

Fixed submission:
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]
xy.sort()
vectors = set()
for i in range(n-1):
    for j in range(i+1, n):
        xi, yi = xy[i]
        xj, yj = xy[j]
        if xj-xi == 0 and yj-yi == 0:
            continue
        unit_vector = (xj-xi, yj-yi)
        vectors |= {unit_vector}
ans = n
for vector in vectors:
    cnt = 0
    for i in range(n-1):
        for j in range(i+1, n):
            xi, yi = xy[i]
            xj, yj = xy[j]
            if vector == (xj-xi, yj-yi):
                cnt += 1
    ans = min(ans, n-cnt)
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
nodes = []
for _ in range(n):
    x, y = map(int, input().split())
    nodes.append((x, y))

nodes.sort(reverse=True)
ans = 0
while nodes:
    ans += 1
    x, y = nodes.pop()
    for i, j in nodes[::-1]:
        if x-i != 0 and y-j != 0:
            vect = (i-x, j-y)
            break
    p, q = vect
    rm_list = []
    for i, j in nodes:
        if i-x == 0 or j-y == 0:
            continue
        if (i-x) % p == 0 and (j-y) % q == 0:
            rm_list.append((i, j))
    for item in rm_list:
        nodes.remove(item)
print(ans)
Problem p03026 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(input())
edges = [[]for _ in range(n)]
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    edges[a].append(b)
    edges[b].append(a)


cost = sorted(map(int, input().split()))
m = 0
ans = [None]*n


def dfs(v, parent):
    ans[v] = cost.pop()
    if parent != -1:
        global m
        m += min(ans[v], ans[parent])
    for v2 in edges[v]:
        if v2 == parent:
            continue
        dfs(v2, v)


dfs(0, -1)
print(m)
print(*ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
edges = [[]for _ in range(n)]
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    edges[a].append(b)
    edges[b].append(a)


cost = sorted(map(int, input().split()), reverse=True)

priority = [(len(edge), v) for v, edge in enumerate(edges)]
priority.sort(reverse=True)
ans = [None]*n
while priority:
    d, v = priority.pop()
    c = cost.pop()
    ans[v] = c

m = 0


def dfs(v, parent):
    if parent != -1:
        global m
        m += min(ans[v], ans[parent])

    for v2 in edges[v]:
        if v2 == parent:
            continue
        dfs(v2, v)


dfs(0, -1)
print(m)
print(*ans)
Problem p03031 (python)

Fixed submission:
n, m = map(int, input().split())
ls = []
for _ in range(m):
    k, *s, = map(lambda x: int(x)-1, input().split())
    ls.append(s)
p = list(map(int, input().split()))


def base_n(x, n, zero_padding=8):
    retval = [[], ['0']][x == 0]
    while x > 0:
        x, r = divmod(x, n)
        retval.append(str(r))
    cnt = zero_padding - len(retval)
    pad = ['', '0'*cnt][cnt > 0]
    return pad+''.join(retval[::-1])


ans = 0
for i in range(2**n):
    base_2 = base_n(i, 2, n)
    for j in range(m):
        switchs = ls[j]
        cond = p[j]

        on_cnt = 0
        for switch in switchs:
            if int(base_2[switch]):
                on_cnt += 1
        if on_cnt % 2 != cond:
            break

    else:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
from itertools import product
n, m = map(int, input().split())
switch = []
for _ in range(m):
    k, *s = map(lambda x: int(x)-1, input().split())
    switch.append(s)
p = map(int, input().split())

ans = 0
for subset in product((0, 1), n):
    for j in range(m):
        on_cnt = sum(subset[s] for s in switch[j])
        if on_cnt % 2 != p[j]:
            break
    else:
        ans += 1
print(ans)
Problem p03032 (python)

Fixed submission:
n, k = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for L in range(k+1):
    for R in range(k-L+1):
        if n < L+R:
            break
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        val -= sum(neg[:k-L-R])
        if ans < val:
            ans = val
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for L in range(k+1):
    for R in range(k-L+1):
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        if k-L-R < len(neg):
            val -= sum(neg[:k-L-R])
        else:
            val -= sum(neg)
        if ans < val:
            ans = val
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for L in range(k+1):
    for R in range(k-L+1):
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        val -= sum(neg[:min(k-L-R, len(neg))])
        if ans < val:
            ans = val
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for L in range(k+1):
    for R in range(k-L+1):
        val = 0
        neg = []
        for i in range(L):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        for i in range(n-1, n-1-R, -1):
            val += V[i]
            if V[i] < 0:
                neg.append(V[i])
        neg.sort()
        val -= sum(neg[:k-L-R])
        if ans < val:
            ans = val
print(ans)
Problem p03126 (python)

Fixed submission:
from collections import defaultdict
n, m = map(int, input().split())
s = []
for _ in range(n):
    k, *a = map(int, input().split())
    s.append(a)
dd = defaultdict(int)
for l in s:
    for k in l:
        dd[k] += 1

print(sum(1 if v == n else 0 for k, v in dd.items()))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
s = {*range(1, m+1)}
for _ in range(n):
    k, *a = map(int, input().split())
    s &= set(a)
print(len(s))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
s = {*range(1, m+1)}
for _ in range(n):
    k, *a = map(int, input().split())
    s &= set(a)
print(len(s))
Problem p03166 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    x, y = list(map(lambda x: int(x)-1, input().split()))
    edges[x].append(y)


def memoize(f):
    memo = dict()

    def memoized(*args):
        if args in memo.keys():
            return memo[args]
        else:
            memo[args] = f(*args)
            return memo[args]
    return memoized

@memoize
def longest_path(v):
    retval = 0
    for v2 in edges[v]:
        retval = max(retval, longest_path(v2)+1)
    return retval


ans = 0
for i in range(n):
    ans = max(ans, longest_path(i))
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
edges = [[]for _ in range(n)]
for _ in range(m):
    x, y = map(lambda x: int(x)-1, input().split())
    edges[x].append(y)



def dfs(v, parent):
    visited[v] = 1
    for v2 in edges[v]:
        if v2 == parent:
            continue
        dfs(v2, v)
        dist[v] = max(dist[v], dist[v2]+1)


dist = [0]*n
visited = [0]*n

for i in range(n):
    if visited[i]:
        continue
    dfs(i, -1)

print(max(dist))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    x, y = list(map(lambda x: int(x)-1, input().split()))
    edges[x].append(y)


def memoize(f):
    memo = dict()

    def memoized(*args):
        if args in memo.keys():
            return memo[args]
        else:
            memo[args] = f(*args)
            return memo[args]
    return memoized

@memoize
def longest_path(v):
    retval = 0
    for v2 in edges[v]:
        retval = max(retval, longest_path(v2)+1)
    return retval


ans = 0
for i in range(n):
    ans = max(ans, longest_path(i))
print(ans)
Problem p03361 (python)

Fixed submission:
h, w = map(int, input().split())
m = [list(input()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            cnt = sum([0 <= i-1, i+1 < h, 0 <= j-1, j+1 < w])

            # up
            if 0 <= i-1:
                if m[i-1][j] != '#':
                    cnt -= 1

            # down
            if i+1 < h:
                if m[i+1][j] != '#':
                    cnt -= 1
            # left
            if 0 <= j-1:
                if m[i][j-1] != '#':
                    cnt -= 1

            # right
            if j+1 < w:
                if m[i][j+1] != '#':
                    cnt -= 1

            if cnt == 0:
                print('No')
                exit(0)
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
m = [list(input()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            cnt = 0
            # left
            if 0 <= i-1:
                if m[i-1][j] != '#':
                    cnt += 1
            # right
            if i+1 < w:
                if m[i+1][j] != '#':
                    cnt += 1
            # up
            if 0 >= j-1:
                if m[i][j-1] != '#':
                    cnt += 1
            # down
            if j+1 < h:
                if m[i][j+1] != '#':
                    cnt += 1
            if cnt == 4:
                print('No')
                exit(0)
            cnt = 0
else:
    print('Yes')
Problem p03407 (python)

Fixed submission:
a, b, c = map(int, input().split())
print('Yes' if a+b >= c else 'No')

Buggy submission (verdict: Runtime Error):
from collections import Counter

n = int(input())
b = [input() for _ in range(n)]
m = int(input())
r = [input() for _ in range(m)]

db, dr = [Counter(x) for x in (b, r)]
print(max(*[v-dr[k] for k, v in db.items()]+[0]))
Problem p03408 (python)

Fixed submission:
from collections import Counter

n = int(input())
b = [input() for _ in range(n)]
m = int(input())
r = [input() for _ in range(m)]

db, dr = [Counter(x) for x in (b, r)]
print(max(*[v-dr[k] for k, v in db.items()]+[0]))

Buggy submission (verdict: Runtime Error):
from collections import Counter

n = int(input())
b = [input() for _ in range(n)]
m = int(input())
r = [input() for _ in range(m)]

db, dr = [Counter(x) for x in (b, r)]

print(max(*(v-dr[k] for k, v in db.items()), 0))

Buggy submission (verdict: Runtime Error):
from collections import Counter
n = int(input())
b = [input() for _ in range(n)]
m = int(input())
r = [input() for _ in range(m)]

db = Counter(b)
dr = Counter(r)

print(max(*(v-dr[k] for k, v in db.items()), 0))
Problem p03497 (python)

Fixed submission:
from collections import Counter
n, k = map(int, input().split())
*a, = map(int, input().split())

c = Counter(a)
kv = sorted(c.items(), key=lambda x: x[1], reverse=True)

if n <= k:
    print(0)
else:
    ans = n
    for i in range(k):
        try:
            ans -= kv[i][1]
        except:
            pass
    print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter
n, k = map(int, input().split())
*a, = map(int, input().split())

c = Counter(a)
kv = sorted(c.items(), key=lambda x: x[1], reverse=True)

if n <= k:
    print(0)
else:
    ans = n
    for i in range(k):
        ans -= kv[i][1]
    print(ans)