Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from collections import deque
X, Y, A, B, C = map(int,input().split())
p = sorted(list(map(int,input().split())))[::-1]
q = sorted(list(map(int,input().split())))[::-1]
r = sorted(list(map(int,input().split())))[::-1]
p = deque(p)
q = deque(q)
r = deque(r)
ans = 0

while len(r) > 0 and X > 0 and p[0] > r[0]:
    ans += p.popleft()
    X -= 1
while len(r) > 0 and Y > 0 and q[0] > r[0]:
    ans += q.popleft()
    Y -= 1

while X > 0 or Y > 0:
    if X > 0 and Y > 0:
        if p[0] > q[0]:
            ans += r.popleft()
            Y -= 1
        else:
            ans += r.popleft()
            X -= 1
    elif X == 0 and Y > 0:
        if len(r) == 0:
            ans += q.popleft()
            Y -= 1
        elif r[0] > q[0]:
            ans += r.popleft()
            Y -= 1
        else:
            ans += q.popleft()
            Y-= 1
    else:
        if len(r) == 0:
            ans += p.popleft()
            X -= 1
        elif r[0] > p[0]:
            ans += r.popleft()
            X -= 1
        else:
            ans += p.popleft()
            X -= 1
print(ans)


Fixed submission:
X, Y, A, B, C = map(int,input().split())
p = sorted(list(map(int,input().split())))[::-1][:X]
q = sorted(list(map(int,input().split())))[::-1][:Y]
r = sorted(list(map(int,input().split())))
print(sum(sorted((p+q+r),reverse = True)[:X+Y]))
Buggy python submission (verdict: Runtime Error):
H, N = map(int,input().split())
M = []
for k in range(N):
    M.append(list(map(int,input().split())))
dp = [10**9]*(H+1)
dp[0] = 0
for A, B in M:
    for k in range(A+1):
        dp[k] = min(dp[k],B)
    for k in range(A+1,H+1):
        dp[k] = min(dp[k],dp[k-A]+B)
print(dp[H])


Fixed submission:
import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    H, N = map(int,input().split())
    M = [list(map(int,input().split())) for k in range(N)]

    dp = [10**9 for k in range(10**4+1)]
    dp[0] = 0
    for e in M:
        A, B = e[0], e[1]
        for k in range(A+1):
            dp[k] = min(dp[k],B)
        for k in range(A+1,H+1):
            dp[k] = min(dp[k],dp[k-A]+B)

    print(dp[H])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
H, N = map(int,input().split())
M = [list(map(int,input().split())) for k in range(N)]
dp = [float("inf")]*(H+1)
dp[0] = 0

for e in M:
    a, b = e[0], e[1]
    for k in range(a+1):
        dp[k] = min(dp[k],b)
    for k in range(a+1,H+1):
        dp[k] = min(dp[k],dp[k-a]+b)
print(dp[H])


Fixed submission:
import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    H, N = map(int,input().split())
    M = [list(map(int,input().split())) for k in range(N)]

    dp = [10**9 for k in range(10**4+1)]
    dp[0] = 0
    for e in M:
        A, B = e[0], e[1]
        for k in range(A+1):
            dp[k] = min(dp[k],B)
        for k in range(A+1,H+1):
            dp[k] = min(dp[k],dp[k-A]+B)

    print(dp[H])

if __name__ == '__main__':
    main()

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

def input():
    return sys.stdin.readline()[:-1]

def main():
    H, N = map(int,input().split())
    M = [list(map(int,input().split())) for k in range(N)]

    dp = [10**9 for k in range(H+1)]
    dp[0] = 0
    for e in M:
        A, B = e[0], e[1]
        for k in range(A+1):
            dp[k] = min(dp[k],B)
        for k in range(A+1,H+1):
            dp[k] = min(dp[k],dp[k-A]+B)

    print(dp[H])

if __name__ == '__main__':
    main()


Fixed submission:
import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    H, N = map(int,input().split())
    M = [list(map(int,input().split())) for k in range(N)]

    dp = [10**9 for k in range(10**4+1)]
    dp[0] = 0
    for e in M:
        A, B = e[0], e[1]
        for k in range(A+1):
            dp[k] = min(dp[k],B)
        for k in range(A+1,H+1):
            dp[k] = min(dp[k],dp[k-A]+B)

    print(dp[H])

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

def check(num,s):
    curr = 0
    num += "_"
    for k in range(len(s)):
        if num[curr] == s[k]:
            curr += 1
    if curr == 3:
        return True
    else:
        return False

def main():
    N = int(input())
    S = input()
    ans = 0
    for k in range(1000):
        t = str(k).zfill(3)
        if check(t,S) == True:
            ans += 1
    print(ans)
if __name__ == '__main__':
    main()


Fixed submission:
N = int(input())
S = input()

def check(k,s):
    j = 0
    for i in range(len(s)):
        if k[j] == s[i]:
            j += 1
            if j == 3:
                return True
    return False

ans = 0
for k in range(1000):
    if check(str(k).zfill(3),S):
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
G = [[int(x) for x in input()] for k in range(N)]
es = [[] for k in range(N)]

for k in range(N):
    for l in range(N):
        if G[k][l] == 1:
            es[k].append(l)

#
# 二部グラフ判定by prdさん
#

#n個の頂点の色を初期化。0:未着色、1:黒、-1:白
colors = [0 for i in range(N)]

#頂点vをcolor(1 or -1)で塗り、再帰的に矛盾がないか調べる。矛盾があればFalse
def dfs(v,color):
    #今いる点を着色
    colors[v] = color
    #今の頂点から行けるところをチェック
    for to in es[v]:
        #同じ色が隣接してしまったらFalse
        if colors[to] == color:
            return False
        #未着色の頂点があったら反転した色を指定し、再帰的に調べる
        if colors[to] == 0 and not dfs(to, -color):
            return False
    #調べ終わったら矛盾がないのでTrue
    return True

#2部グラフならTrue, そうでなければFalse
def is_bipartite():
    return dfs(0,1) # 頂点0を黒(1)で塗ってDFS開始

if is_bipartite() == False:
    print(-1)
    exit(0)

# ワーシャルフロイドで距離を求める
D = [[1000 for k in range(N)] for l in range(N)]
for k in range(N):
    for l in range(N):
        if k == l:
            D[k][l] = 0
        elif G[k][l] == 1:
            D[k][l] = 1

for p in range(N):
    for q in range(N):
        for r in range(N):
            D[p][q] = min(D[p][q],D[p][r]+D[r][q])
ans = 0
for k in range(N):
    for l in range(N):
        ans = max(ans, D[k][l]+1)

print(ans)


Fixed submission:
N = int(input())
G = [[int(x) for x in input()] for k in range(N)]
es = [[] for k in range(N)]

for k in range(N):
    for l in range(N):
        if G[k][l] == 1:
            es[k].append(l)

#
# 二部グラフ判定by prdさん
#

#n個の頂点の色を初期化。0:未着色、1:黒、-1:白
colors = [0 for i in range(N)]

#頂点vをcolor(1 or -1)で塗り、再帰的に矛盾がないか調べる。矛盾があればFalse
def dfs(v,color):
    #今いる点を着色
    colors[v] = color
    #今の頂点から行けるところをチェック
    for to in es[v]:
        #同じ色が隣接してしまったらFalse
        if colors[to] == color:
            return False
        #未着色の頂点があったら反転した色を指定し、再帰的に調べる
        if colors[to] == 0 and not dfs(to, -color):
            return False
    #調べ終わったら矛盾がないのでTrue
    return True

#2部グラフならTrue, そうでなければFalse
def is_bipartite():
    return dfs(0,1) # 頂点0を黒(1)で塗ってDFS開始

if is_bipartite() == False:
    print(-1)
    exit(0)

# ワーシャルフロイドで距離を求める
D = [[float("inf") for k in range(N)] for l in range(N)]
for k in range(N):
    for l in range(N):
        if k == l:
            D[k][l] = 0
        elif G[k][l] == 1:
            D[k][l] = 1

for p in range(N):
    for q in range(N):
        for r in range(N):
            D[q][r] = min(D[q][r],D[q][p]+D[p][r])
ans = 0
for k in range(N):
    for l in range(N):
        ans = max(ans, D[k][l]+1)

print(ans)
Buggy python submission (verdict: Runtime Error):
import math
A, B = map(int,input().split())
def yakusuu(N):
    P = set([1,N])
    for k in range(2,math.floor(math.sqrt(N))+1):
        if N % k == 0:
            P.add(k)
            P.add(N//k)
    return P
C = sorted(list(yakusuu(A)&yakusuu(B)))
N = len(C)
if N == 1:
    print(1)
else:
    ans = 0
    for k in range(N):
        f = 1
        for l in range(k-1):
            if math.gcd(C[k],C[l]) != 1:
                f = 0
                break
        ans += f
    print(ans)


Fixed submission:
import sys,math

def input():
    return sys.stdin.readline()[:-1]
def yakusuu(N):
    P = [1,N]
    for k in range(2,math.floor(math.sqrt(N))+1):
        if N % k == 0:
            P.append(k)
            P.append(N//k)
    return P
def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

def main():
    A, B = map(int,input().split())
    C = sorted(list(set(yakusuu(A))&set(yakusuu(B))))

    if len(C) == 1:
        print(1)
        exit(0)
    D = [1]
    for k in range(1,len(C)):
        f = 1
        for l in range(k):
            if gcd(C[k],C[l]) != 1:
                f = 0
                break
        if f == 1:
            D.append(C[k])
    print(len(D))


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
N, K = map(int,input().split())
S = input()
A = []

t = 1
for k in range(1,N):
    if S[k] == S[k-1]:
        t += 1
    else:
        A.append(t)
        t = 1
A.append(t)
if len(A) <= 2:
    print(N-1)
else:
    B = []
    for k in range(len(A)-2):
        B.append(A[k]+A[k+1]+A[k+2])
    C = sorted(B)[::-1]
    S = C[min(K-1,len(C))]
    F = K
    k = -1
    while k < len(A)-3:
        k += 1
        if F == 0:
            break
        if A[k]+A[k+1]+A[k+2] >= S:
            t = A[k]+A[k+1]+A[k+2]
            A[k] = 0
            A[k+1] = 0
            A[k+2] = t
            F -= 1
            k += 1
    ans = 0
    for e in A:
        ans += max(0,e-1)
    print(ans)


Fixed submission:
N, K = map(int,input().split())
S = input()
ans = 0
for k in range(1,N):
    if S[k] == S[k-1]:
        ans += 1
print(min(ans+2*K,N-1))
Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
L = [list(map(int,input().split())) for k in range(N)]
L = sorted(L,key = lambda x: (-x[1],-x[0]))

ans = 0
c = 0
D = [1 for k in range(M+2)]
D[M+1] = 0
for k in range(N):
    if D[L[k][0]] == 1:
        D[M-c] = 0
        c += 1
        ans += L[k][1]
    if c == M:
        break
print(ans)


Fixed submission:
import heapq

N, M = map(int,input().split())
J = [[] for k in range(M+1)]
for k in range(N):
    A, B = map(int,input().split())
    if A <= M:
        J[A].append(B)

Q = []
ans = 0
for d in range(1,M+1):
    for e in J[d]:
        heapq.heappush(Q,-e)
    if len(Q) > 0:
        ans += -heapq.heappop(Q)
print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()
N = len(S)
dp = [[0 for i in range(13)] for j in range(N+1)]

MOD = 10**9 + 7
dp[0][0] = 1

for i in range(1,N+1):
    if S[i-1] == "?":
        for j in range(13):
            for k in range(10):
                dp[i][(j*10+k)%13] += dp[i-1][j]
                dp[i][(j*10+k)%13] %= MOD
    else:
        t = int(S[i-1])
        for j in range(13):
            dp[i][(j*10+t)%13] += dp[i-1][j]
            dp[i][(j*10+k)%13] %= MOD

print(dp[N][5])


Fixed submission:
S = input()
N = len(S)
dp = [[0 for i in range(13)] for j in range(N+1)]

MOD = 10**9 + 7
dp[0][0] = 1

for i in range(1,N+1):
    if S[i-1] == "?":
        for j in range(13):
            for k in range(10):
                dp[i][(j*10+k)%13] += dp[i-1][j]
                dp[i][(j*10+k)%13] %= MOD
    else:
        t = int(S[i-1])
        for j in range(13):
            dp[i][(j*10+t)%13] += dp[i-1][j]
            dp[i][(j*10+t)%13] %= MOD

print(dp[N][5])
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    A = [int(input())]
    for k in range(N-1):
        t = int(input())
        if A[0] >= t:
            A = [t] + A
        else:
            A[bisect.bisect_left(A,t)-1] = t
    print(len(A))

if __name__ == '__main__':
    main()



Fixed submission:
import bisect
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N = int(input())
    A = deque([int(input())])
    for k in range(N-1):
        t = int(input())
        if A[0] >= t:
            A.appendleft(t)
        else:
            A[bisect.bisect_left(A,t)-1] = t
    print(len(A))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
A = [int(input())]
for k in range(N-1):
    t = int(input())
    if A[0] >= t:
        A = [t] + A
    else:
        A[bisect.bisect_left(A,t)-1] = t
print(len(A))


Fixed submission:
import bisect
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N = int(input())
    A = deque([int(input())])
    for k in range(N-1):
        t = int(input())
        if A[0] >= t:
            A.appendleft(t)
        else:
            A[bisect.bisect_left(A,t)-1] = t
    print(len(A))

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math
N, K = map(int,input().split())
MOD = 10**9 + 7

def nCr(n,r):
    if n == 0 or r == 0:
        return 1
    else:
        return (math.factorial(n)//(math.factorial(r)*math.factorial(n-r)))%MOD

for i in range(1,K+1):
    ans = 1
    b = K-i
    ans *= nCr(K-1,i-1)
    ans *= nCr(N-K+1,i)
    print(ans%MOD)


Fixed submission:
import math
N, K = map(int,input().split())
MOD = 10**9 + 7

def nCr(n,r):
    if n <= 0 or r <= 0:
        return 1
    elif n-r < 0:
        return 0
    else:
        return (math.factorial(n)//(math.factorial(r)*math.factorial(n-r)))%MOD

for i in range(1,K+1):
    ans = 1
    b = K-i
    ans *= nCr(K-1,i-1)
    ans *= nCr(N-K+1,i)
    print(ans%MOD)
Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = [int(input()) for k in range(M)]
dp = [0 for k in range(N+1)]
MOD = 10**9+7

dp[0] = 1
dp[1] = 1
dp[2] = 1

t = 0
if A[0] == 1:
    t += 1
    dp[1] = 0

for k in range(2,N+1):
    if t < len(A):
        if A[t] == k:
            dp[k] = 0
            t += 1
        else:
            dp[k] = dp[k-1] + dp[k-2]
            dp[k] %= MOD
    else:
        dp[k] = dp[k-1] + dp[k-2]
        dp[k] %= MOD
print(dp[N])


Fixed submission:
N, M = map(int,input().split())
dp = [0 for k in range(N+1)]
MOD = 10**9+7

if M == 0:
    if N == 1:
        print(1)
        exit(0)
    dp[0] = 1
    dp[1] = 1
    dp[2] = 1
    for k in range(2,N+1):
        dp[k] = dp[k-1] + dp[k-2]
        dp[k] %= MOD
    print(dp[N])
else:
    A = [int(input()) for k in range(M)]
    dp[0] = 1
    dp[1] = 1
    dp[2] = 1
    if N == 1:
        if M == 1:
            print(0)
        exit(0)
    else:
        t = 0
        if A[0] == 1:
            t += 1
            dp[1] = 0
        for k in range(2,N+1):
            if t < len(A) and A[t] == k:
                dp[k] = 0
                t += 1
            else:
                dp[k] = dp[k-1] + dp[k-2]
                dp[k] %= MOD
        print(dp[N])

Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = [int(input()) for k in range(M)]
dp = [0 for k in range(N+1)]
MOD = 10**9+7

dp[0] = 1
dp[1] = 1
dp[2] = 1

t = 0
if A[0] == 1:
    t += 1
    dp[1] = 0

for k in range(2,N+1):
    if t < len(A) and A[t] == k:
        dp[k] = 0
        t += 1
    else:
        dp[k] = dp[k-1] + dp[k-2]
        dp[k] %= MOD
print(dp[N])


Fixed submission:
N, M = map(int,input().split())
dp = [0 for k in range(N+1)]
MOD = 10**9+7

if M == 0:
    if N == 1:
        print(1)
        exit(0)
    dp[0] = 1
    dp[1] = 1
    dp[2] = 1
    for k in range(2,N+1):
        dp[k] = dp[k-1] + dp[k-2]
        dp[k] %= MOD
    print(dp[N])
else:
    A = [int(input()) for k in range(M)]
    dp[0] = 1
    dp[1] = 1
    dp[2] = 1
    if N == 1:
        if M == 1:
            print(0)
        exit(0)
    else:
        t = 0
        if A[0] == 1:
            t += 1
            dp[1] = 0
        for k in range(2,N+1):
            if t < len(A) and A[t] == k:
                dp[k] = 0
                t += 1
            else:
                dp[k] = dp[k-1] + dp[k-2]
                dp[k] %= MOD
        print(dp[N])
Buggy python submission (verdict: Time Limit Exceeded):
L = input()
l = len(L)
MOD = 10**9 + 7
ans = 0
c = 0
for k in range(l):
    if L[k] == "1":
        ans += (3**(l-k-1)) * (2**c)
        ans %= MOD
        c += 1
ans += 2**c
print(ans%MOD)

Fixed submission:
L = input()
l = len(L)
MOD = 10**9 + 7
ans = 0
c = 0
for k in range(l):
    if L[k] == "1":
        ans += pow(3,l-k-1,MOD) * pow(2,c,MOD)
        ans %= MOD
        c += 1
ans += pow(2,c,MOD)
print(ans%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
L = input()
l = len(L)
MOD = 10**9 + 7
ans = 0
c = 0
for k in range(l):
    if L[k] == "1":
        ans += (3**(l-k-1)) * (2**c)
        ans %= MOD
        c += 1
ans += 2**c
print(ans%MOD)

Fixed submission:
L = input()
l = len(L)
MOD = 10**9 + 7
ans = 0
c = 0
for k in range(l):
    if L[k] == "1":
        ans += pow(3,l-k-1,MOD) * pow(2,c,MOD)
        ans %= MOD
        c += 1
ans += pow(2,c,MOD)
print(ans%MOD)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
l = len(S)
A = ""
k = 0
while k < l-1:
    if S[k:k+2] == "BC":
        A += "D"
        k += 2
    else:
        A += S[k]
        k += 1
S = A
S = S.replace("B","C")
L = list(S.split("C"))

def keisan(s):
    if len(s) == 0:
        return 0
    l = len(s)
    t = 1
    ret = 0
    for k in range(len(s)):
        if s[k] == "A":
            ret += l-k-t
            t += 1
    return ret

ans = 0
for s in L:
    ans += keisan(s)
print(ans)


Fixed submission:
S = input()
l = len(S)
A = ""
k = 0
while k < l-1:
    if S[k:k+2] == "BC":
        A += "D"
        k += 2
    else:
        A += S[k]
        k += 1
S = A
S = S.replace("B","C")
L = list(S.split("C"))

def keisan(s):
    if len(s) == 0:
        return 0
    l = len(s)
    t = 1
    ret = 0
    for k in range(len(s)):
        if s[k] == "A":
            ret += l-k-t
            t += 1
    return ret

ans = 0
for s in L:
    ans += keisan(s)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
l = len(S)
for k in range(1,l):
    if S[k-1:k+1] == "BC":
        S = S[:k-1] + "D" + S[k+1:]
        l -= 1
S = "".join(S)
S = S.replace("B","C")
L = list(S.split("C"))

def keisan(s):
    if len(s) == 0:
        return 0
    l = len(s)
    t = 1
    ret = 0
    for k in range(len(s)):
        if s[k] == "A":
            ret += l-k-t
            t += 1
    return ret

ans = 0
for s in L:
    ans += keisan(s)
print(ans)


Fixed submission:
S = input()
l = len(S)
A = ""
k = 0
while k < l-1:
    if S[k:k+2] == "BC":
        A += "D"
        k += 2
    else:
        A += S[k]
        k += 1
S = A
S = S.replace("B","C")
L = list(S.split("C"))

def keisan(s):
    if len(s) == 0:
        return 0
    l = len(s)
    t = 1
    ret = 0
    for k in range(len(s)):
        if s[k] == "A":
            ret += l-k-t
            t += 1
    return ret

ans = 0
for s in L:
    ans += keisan(s)
print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = sorted(list(map(int,input().split())))
D = [list(map(int,input().split())) for k in range(M)]
D = sorted(D, key = lambda x: -x[1])

now = 0
for d in D:
    for k in range(now,now+d[0]):
        if A[k] < d[1]:
            A[k] = d[1]
            now += 1
        else:
            now = k
            break
    if now == N:
        break
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = sorted(list(map(int,input().split())))
D = [list(map(int,input().split())) for k in range(M)]
D = sorted(D, key = lambda x: -x[1])

now = 0
for d in D:
    for k in range(now,now+d[0]):
        if now == N:
            break
        if A[k] < d[1]:
            A[k] = d[1]
            now += 1
        else:
            now = k
            break

print(sum(A))
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappush, heappop
N = int(input())
s = list(map(int,input().split()))
a, b = s[1], s[2]
L = []
R = []
heappush(L,-a)
heappush(R,a)

bsum = b
t = 0
for k in range(N-1):
    s = input()
    if s[0] == "1":
        s = list(map(int,s.split()))
        a = s[1]
        b = s[2]
        bsum += b
        if -L[0] <= a <= R[0]:
            pass
        else:
            t += min(abs(a+L[0]),abs(a-R[0]))
        heappush(L,-a)
        heappush(R,a)
        if -L[0] > R[0]:
            c = heappop(L)
            d = heappop(R)
            heappush(R,-c)
            heappush(L,-d)
    else:
        print(-L[0], t+bsum)

Fixed submission:
from heapq import heappush, heappop
N = int(input())
s = list(map(int,input().split()))
a, b = s[1], s[2]
L = []
R = []
heappush(L,-a)
heappush(R,a)

bsum = b
t = 0
for k in range(N-1):
    s = input()
    if s[0] == "1":
        s = list(map(int,s.split()))
        a = s[1]
        b = s[2]
        bsum += b
        if -L[0] <= a <= R[0]:
            pass
        else:
            t += min(abs(a+L[0]),abs(a-R[0]))
        heappush(L,-a)
        heappush(R,a)
        if -L[0] > R[0]:
            c = heappop(L)
            d = heappop(R)
            heappush(R,-c)
            heappush(L,-d)
    else:
        print(-L[0], t+bsum)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100010)

N, M = map(int,input().split())
E = [[] for k in range(N)]

for k in range(M):
    X, Y, Z = map(int,input().split())
    E[X-1].append(Y-1)
    E[Y-1].append(X-1)
G = [-1]*N

def dfs(now,group):
    if G[now] == -1:
        G[now] = group
        for tsugi in E[now]:
            dfs(tsugi,group)

ans = 0
for k in range(N):
    if G[k] == -1:
        dfs(k,ans)
        ans += 1
print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(100010)

N, M = map(int,input().split())
E = [[] for k in range(N)]

for k in range(M):
    X, Y, Z = map(int,input().split())
    E[X-1].append(Y-1)
    E[Y-1].append(X-1)
G = [-1]*N

def dfs(now,group):
    if G[now] == -1:
        G[now] = group
        for tsugi in E[now]:
            dfs(tsugi,group)

ans = 0
for k in range(N):
    if G[k] == -1:
        dfs(k,ans)
        ans += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
E = [[] for k in range(N)]

for k in range(M):
    X, Y, Z = map(int,input().split())
    E[X-1].append(Y-1)
    E[Y-1].append(X-1)
G = [-1]*N

def dfs(now,group):
    if G[now] == -1:
        G[now] = group
        for tsugi in E[now]:
            dfs(tsugi,group)

ans = 0
for k in range(N):
    if G[k] == -1:
        dfs(k,ans)
        ans += 1
print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(100010)

N, M = map(int,input().split())
E = [[] for k in range(N)]

for k in range(M):
    X, Y, Z = map(int,input().split())
    E[X-1].append(Y-1)
    E[Y-1].append(X-1)
G = [-1]*N

def dfs(now,group):
    if G[now] == -1:
        G[now] = group
        for tsugi in E[now]:
            dfs(tsugi,group)

ans = 0
for k in range(N):
    if G[k] == -1:
        dfs(k,ans)
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int,input().split())
ans = 0

for r in range(1+N//R):
    for g in range(1+N//G):
        if N-(R*r+G*g)>=0 and (N-(R*r+G*g))%B==0:
           ans += 1

print(ans)

Fixed submission:
R, G, B, N = map(int,input().split())
ans = 0

for r in range(1+N//R):
    for g in range(1+N//G):
        if N-(R*r+G*g)>=0 and (N-(R*r+G*g))%B==0:
           ans += 1

print(ans)