Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n = int(input())
max_step = 0
step = 0
pre_h = 0
for h in map(int, input().split()):
  if h <= pre_h:
    step += 1
  else:
    if max_step < step:
      max_step = step
    step = 0
  pre_h = h
print(max[max_step, step])

Fixed submission:
n = int(input())
max_step = 0
step = 0
pre_h = 0
for h in map(int, input().split()):
  if h <= pre_h:
    step += 1
  else:
    if max_step < step:
      max_step = step
    step = 0
  pre_h = h
print(max([max_step, step]))
Buggy python submission (verdict: Runtime Error):
n = int(input())
words = dict()
count = 0
for i in range(n):
  s = sorted(input())
  if s in words:
    count += words[s]
    words[s] += 1
  else:
    words[s] = 0
print(count)

Fixed submission:
n = int(input())
words = dict()
count = 0
for i in range(n):
  s = ''.join(sorted(input()))
  if s in words:
    count += words[s]
    words[s] += 1
  else:
    words[s] = 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [sorted(input()) for _ in range(n)]
count = 0
for i in range(n-1):
  for j in range(i+1, n):
    if s[i] == s[j]:
      count += 1
print(count)

Fixed submission:
n = int(input())
words = dict()
count = 0
for i in range(n):
  s = ''.join(sorted(input()))
  if s in words:
    count += words[s]
    words[s] += 1
  else:
    words[s] = 1
print(count)
Buggy python submission (verdict: Runtime Error):
import heapq

n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort()

i = 0
works = []
reward = 0
for d in range(m):
  while i < n and jobs[i][0] <= d + 1:
    heapq.heappush(works, -jobs[i][1])
    i += 1
  if works:
    reward -= heapq.heappop(words)
print(reward)

Fixed submission:
import heapq

n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort()

i = 0
works = []
reward = 0
for d in range(m):
  while i < n and jobs[i][0] <= d + 1:
    heapq.heappush(works, -jobs[i][1])
    i += 1
  if works:
    reward -= heapq.heappop(works)
print(reward)
Buggy python submission (verdict: Runtime Error):
s = input()
n = len(s)

i = 0
is_r = True
rl_list = []
r, l = 0, 0
idx = None
for i in range(n)
  if s[i] == 'R':
    if not is_r:
      rl_list.append((r, l, idx))
      is_r = True
      r, l = 0, 0
    r += 1
  else:
    if is_r:
      is_r = False
      idx = i - 1
    l += 1
rl_list.append((r, l, idx))
children = [0] * n
for r, l, idx in rl_list:
  children[idx] = (r + 1) // 2 + l // 2
  children[idx + 1] = r // 2 + (l + 1) // 2
print(' '.join(map(str, children)))

Fixed submission:
s = input()
n = len(s)

i = 0
is_r = True
rl_list = []
r, l = 0, 0
idx = None
for i in range(n):
  if s[i] == 'R':
    if not is_r:
      rl_list.append((r, l, idx))
      is_r = True
      r, l = 0, 0
    r += 1
  else:
    if is_r:
      is_r = False
      idx = i - 1
    l += 1
rl_list.append((r, l, idx))
children = [0] * n
for r, l, idx in rl_list:
  children[idx] = (r + 1) // 2 + l // 2
  children[idx + 1] = r // 2 + (l + 1) // 2
print(' '.join(map(str, children)))
Buggy python submission (verdict: Runtime Error):
n = int(input())

wrong = 0
for i, p in enemerate(map(int, input().split()), start=1):
  wrong += int(i != p)

if wrong == 0 or wrong == 2:
  print('YES')
else:
  print('NO')

Fixed submission:
n = int(input())

wrong = 0
for i, p in enumerate(map(int, input().split()), start=1):
  wrong += int(i != p)

if wrong == 0 or wrong == 2:
  print('YES')
else:
  print('NO')
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input()))
b = list(map(int, input()))
monster = 0
for i in range(n):
  if b[i] <= a[i]:
    monster += b[i]
  elif b[i] <= a[i] + a[i+1]:
    monster += b[i]
    a[i+1] -= b[i] - a[i]
  else:
    monster += a[i] + a[i+1]
    a[i+1] = 0
print(monster)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
monster = 0
for i in range(n):
  if b[i] <= a[i]:
    monster += b[i]
  elif b[i] <= a[i] + a[i+1]:
    monster += b[i]
    a[i+1] -= b[i] - a[i]
  else:
    monster += a[i] + a[i+1]
    a[i+1] = 0
print(monster)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
mod = 10 ** 9 + 7

dp = [1] + [0] * 12
for c in s:
  tmp = dp[:]
  if c == '?':
    dp = [0] * 13
    for n in range(10):
      for i in range(13):
        index = (10 * i + n) % 13
        dp[index] = (dp[index] + tmp[i]) % mod
  else:
    n = int(c)
    for i in range(13):
      dp[(10 * i + n) % 13] = tmp[i]
print(dp[5])

Fixed submission:
s = input()
mod = 10 ** 9 + 7

dp = [1] + [0] * 12
for c in s:
  tmp = dp[:]
  if c == '?':
    dp = [0] * 13
    for n in range(10):
      for i in range(13):
        index = (10 * i + n) % 13
        dp[index] = (dp[index] + tmp[i]) % mod
  else:
    n = int(c)
    for i in range(13):
      dp[(10 * i + n) % 13] = tmp[i]
print(dp[5])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
mod = 10 ** 9 + 7

dp = [0] * 13
dp[0] = 1
for c in s:
  tmp = dp[:]
  if c == '?':
    dp = [0] * 13
    for n in range(10):
      for i in range(13):
        dp[(10 * i + n) % 13] += tmp[i]
    for i in range(13):
      dp[i] %= mod
  else:
    n = int(c)
    for i in range(13):
      dp[(10 * i + n) % 13] = tmp[i]
print(dp[5])

Fixed submission:
s = input()
mod = 10 ** 9 + 7

dp = [1] + [0] * 12
for c in s:
  tmp = dp[:]
  if c == '?':
    dp = [0] * 13
    for n in range(10):
      for i in range(13):
        index = (10 * i + n) % 13
        dp[index] = (dp[index] + tmp[i]) % mod
  else:
    n = int(c)
    for i in range(13):
      dp[(10 * i + n) % 13] = tmp[i]
print(dp[5])
Buggy python submission (verdict: Runtime Error):
mod = 10 ** 9 + 7

n, k = map(int, input().split())
edge = [[] for _ in range(n)]
parent = [0] * n
for _ in range(n):
  a, b = map(int, input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)
permutation_k2 = [1] * n
for i in range(n-1):
  permutation_k2[i+1] = permutation_k2[i] * (k - 2 - i) % mod
count = k * (k - 1) * permutation_k2[len(edge[0]) - 1] % mod
queue = edge[0]
while queue:
  vertex = queue.pop()
  count = count * permutation_k2[len(edge[vertex]) - 1] % mod
  p = parent[vertex]
  for v in edge[vertex]:
    if v == p:
      continue
    queue.append(v)
    parent[v] = vertex
print(count)

Fixed submission:
mod = 10 ** 9 + 7

n, k = map(int, input().split())
edge = [[] for _ in range(n)]
parent = [0] * n
permutation_k2 = [1] * n
for i in range(n-1):
  a, b = map(int, input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)
  permutation_k2[i+1] = permutation_k2[i] * (k - 2 - i) % mod
count = k * (k - 1) * permutation_k2[len(edge[0]) - 1] % mod
queue = edge[0]
while queue:
  vertex = queue.pop()
  count = count * permutation_k2[len(edge[vertex]) - 1] % mod
  p = parent[vertex]
  for v in edge[vertex]:
    if v == p:
      continue
    queue.append(v)
    parent[v] = vertex
if n == 1:
  print(k)
else:
  print(count)
Buggy python submission (verdict: Runtime Error):
s = input()
s.sort()
if s[0] == s[1] and s[1] != s[2] and s[2] == s[3]:
  print('Yes')
else:
  print('No')

Fixed submission:
s = input()
if s[0] == s[1] == s[2] == s[3]:
  print('No')
elif s[0] == s[1] and s[2] == s[3]:
  print('Yes')
elif s[0] == s[2] and s[1] == s[3]:
  print('Yes')
elif s[0] == s[3] and s[1] == s[2]:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
n = int(input())
d = sorted(map(int, input().split()))
print(d[n // 2 + 1] - d[n // 2])

Fixed submission:
n = int(input())
d = sorted(map(int, input().split()))
print(d[n // 2] - d[n // 2 - 1])
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
mod = 10 ** 9 + 7

fact = [1] * (n + 1)
for i in range(2, n + 1):
  fact[i] = fact[i-1] * i % mod
inv_fact = [1] * (n + 1)
inv_fact[n] = pow(fact[n], mod - 2, mod)
for i in range(n - 1, 1, -1):
  inv_fact[i] = inv_factor[i + 1] * (i + 1) % mod

def comb(_n: int, _k: int) -> int:
  return fact[_n] * inv_fact[_n - _k] * inv_vact[_k] % mod

for i in range(1, k + 1):
  red = n - k
  if i > red + 1:
    print(0)
  else:
    print(comb(red + 1, i) * comb(k - 1, k - i) % mod)

Fixed submission:
n, k = map(int, input().split())
mod = 10 ** 9 + 7

fact = [1] * (n + 1)
for i in range(2, n + 1):
  fact[i] = fact[i-1] * i % mod
inv_fact = [1] * (n + 1)
inv_fact[n] = pow(fact[n], mod - 2, mod)
for i in range(n - 1, 1, -1):
  inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod

def comb(_n: int, _k: int) -> int:
  return fact[_n] * inv_fact[_n - _k] * inv_fact[_k] % mod

for i in range(1, k + 1):
  red = n - k
  if i > red + 1:
    print(0)
  else:
    print(comb(red + 1, i) * comb(k - 1, k - i) % mod)
Buggy python submission (verdict: Runtime Error):
from collections import deque

n, m = map(int, input().split())
adj_list = [[] for _ in range(3 * n)]
for _ in range(m):
  u, v = map(int, input().split())
  adj_list[3 * u - 3].append(3 * v - 2)
  adj_list[3 * u - 2].append(3 * v - 1)
  adj_list[3 * u - 1].append(3 * v - 3)

s, t = map(int, input().split())
dist = [-1] * (3 * n)
queue = deque([3 * s - 3])
dist[3 * s - 3] = 0
while queue:
  vertex = queue.leftpop()
  for next_vertex in adj_list[vertex]:
    if dist[next_vertex] == -1:
      continue
    dist[next_vertex] = dist[vertex] + 1
    queue.append(next_vertex)
t_distance = dist[3 * t - 3]
if t_distance == -1:
  print(-1)
else:
  print(t_distance // 3)

Fixed submission:
from collections import deque

n, m = map(int, input().split())
adj_list = [[] for _ in range(3 * n)]
for _ in range(m):
  u, v = map(int, input().split())
  adj_list[3 * u - 3].append(3 * v - 2)
  adj_list[3 * u - 2].append(3 * v - 1)
  adj_list[3 * u - 1].append(3 * v - 3)

s, t = map(int, input().split())
dist = [-1] * (3 * n)
queue = deque([3 * s - 3])
dist[3 * s - 3] = 0
while queue:
  vertex = queue.popleft()
  for next_vertex in adj_list[vertex]:
    if dist[next_vertex] != -1:
      continue
    dist[next_vertex] = dist[vertex] + 1
    queue.append(next_vertex)
t_distance = dist[3 * t - 3]
if t_distance == -1:
  print(-1)
else:
  print(t_distance // 3)
Buggy python submission (verdict: Runtime Error):
n, l = map(int, input().split())

total = (l - 1) * n + n * (n + 1) // 2
min_diff = abs(total)
for i in range(n):
  apple = l + i
  apple_pie = total - apple
  diff = abs(apple)
  if diff < min_diff:
    min_diff = diff
    min_apple_pie = apple_pie
print(min_apple_pie)

Fixed submission:
n, l = map(int, input().split())

total = (l - 1) * n + n * (n + 1) // 2
min_diff = 1e20
for i in range(n):
  apple = l + i
  apple_pie = total - apple
  diff = abs(apple)
  if diff < min_diff:
    min_diff = diff
    min_apple_pie = apple_pie
print(min_apple_pie)
Buggy python submission (verdict: Runtime Error):
n = int(input())
cumsum_w = [0]
for w in map(int, input().split()):
  cumsum_w.append(cumsum_w[-1] + w)
minimum = 100
for s in cumsum_w[1:-1]:
  minimum = min(minimum, abs(w[-1] - 2 * s))
print(minimum)

Fixed submission:
n = int(input())
cumsum_w = [0]
for w in map(int, input().split()):
  cumsum_w.append(cumsum_w[-1] + w)
minimum = 100
for s in cumsum_w[1:-1]:
  minimum = min(minimum, abs(cumsum_w[-1] - 2 * s))
print(minimum)

Buggy python submission (verdict: Runtime Error):
n = int(input())
cumsum_w = [0]
for w in map(int, input().split()):
  cumsum_w.append(cumsum_w[-1] + w)
minimum = 100
for s in w[1:-1]:
  minimum = min(minimum, abs(w[-1] - 2 * s))
print(minimum)

Fixed submission:
n = int(input())
cumsum_w = [0]
for w in map(int, input().split()):
  cumsum_w.append(cumsum_w[-1] + w)
minimum = 100
for s in cumsum_w[1:-1]:
  minimum = min(minimum, abs(cumsum_w[-1] - 2 * s))
print(minimum)
Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
s = [input() for _ in range(h)]

l = [[0] * w for _ in range(h)]
r = [[0] * w for _ in range(h)]
u = [[0] * w for _ in range(h)]
d = [[0] * w for _ in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j] == '.':
      if j == 0:
        l[i][j] = 1
      else:
        l[i][j] = l[i][j-1] + 1
    if s[i][w-j-1] == '.':
      if j == 0:
        r[i][w-j-1] = 1
      else:
        r[i][w-j-1] = r[i][w-j] + 1

for j in range(w):
  for i in range(h):
    if s[i][j] == '.':
      if i == 0:
        u[i][j] = 1
      else:
        u[i][j] = u[i-1][j] + 1
    if s[h-i-1][j] == '.':
      if i == 0:
        d[h-i-1][j] = 1
      else:
        d[h-i-1][j] = d[h-i][j] + 1

maximum = 1
for i in range(h):
  for j in range(w):
    maximum = max(maximum, l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3)
print(maximum)

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

l = [[0] * w for _ in range(h)]
r = [[0] * w for _ in range(h)]
u = [[0] * w for _ in range(h)]
d = [[0] * w for _ in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j] == '.':
      if j == 0:
        l[i][j] = 1
      else:
        l[i][j] = l[i][j-1] + 1
    if s[i][w-j-1] == '.':
      if j == 0:
        r[i][w-j-1] = 1
      else:
        r[i][w-j-1] = r[i][w-j] + 1

for j in range(w):
  for i in range(h):
    if s[i][j] == '.':
      if i == 0:
        u[i][j] = 1
      else:
        u[i][j] = u[i-1][j] + 1
    if s[h-i-1][j] == '.':
      if i == 0:
        d[h-i-1][j] = 1
      else:
        d[h-i-1][j] = d[h-i][j] + 1

maximum = 1
for i in range(h):
  for j in range(w):
    maximum = max(maximum, l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3)
print(maximum)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
light = []
for _ in range(m):
  s_map = map(int, input().split())
  next(s_map)
  indices = [s-1 for s in s_map]
  light.append(int('0b' + ''.join(number)))
p = list(map(int, input().split()))
count = 0
for i in range(1 << n):
  list_i = [int(bit) for bit in format(i, '0{}b'.format(n))]
  flag = True
  for j in range(m):
    on = 0
    for idx in indices:
      on ^= list_i[idx]
    if on != p[j]:
      flag = False
      break
  if flag:
    count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
indices = []
for _ in range(m):
  s_map = map(int, input().split())
  next(s_map)
  indices.append([s-1 for s in s_map])
p = list(map(int, input().split()))
count = 0
for i in range(1 << n):
  list_i = [int(bit) for bit in format(i, '0{}b'.format(n))]
  flag = True
  for j in range(m):
    on = 0
    for idx in indices[j]:
      on ^= list_i[idx]
    if on != p[j]:
      flag = False
      break
  if flag:
    count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))
mini = min(n, k)
value = 0
for a in range(mini + 1):
  for b in range(mini - a + 1):
    c = k - a - b
    j = v[:a] + v[n-b:]
    j.sort(reverse=True)
    for _ in range(c):
      if j[-1] < 0:
        j.pop()
      else:
        break
    value = max(value, sum(j))
print(value)

Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))
mini = min(n, k)
value = 0
for a in range(mini + 1):
  for b in range(mini - a + 1):
    c = k - a - b
    j = v[:a] + v[n-b:]
    j.sort(reverse=True)
    for _ in range(c):
      if j and j[-1] < 0:
        j.pop()
      else:
        break
    value = max(value, sum(j))
print(value)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, m = map(int, input().split())
queue = []
for a in map(int, input().split()):
  heapq.heappush(queue, a)
for _ in range(m):
  b, c = map(int, input().split())
  for _ in range(b):
    if queue[0] < c:
      heapq.heapreplace(queue, c)
    else:
      break
print(sum(queue))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
cb = [list(map(int, input().split()[::-1])) for _ in range(m)]
a.sort()
cb.sort()
summation = 0
a_tmp = a.pop()
cb_tmp = cb.pop()
for i in range(n):
  if a_tmp >= cb_tmp[0]:
    summation += a_tmp
    if a:
      a_tmp = a.pop()
  else:
    summation += cb_tmp[0]
    cb_tmp[1] -= 1
    if cb_tmp[1] == 0:
      if cb:
        cb_tmp = cb.pop()
      else:
        cb_tmp = [0, n]
print(summation)

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

n, m = map(int, input().split())
queue = []
for a in map(int, input().split()):
  heapq.heappush(queue, a)
for _ in range(m):
  b, c = map(int, input().split())
  for _ in range(b):
    if queue[0] < c:
      heapq.heapreplace(queue, c)
    else:
      break
print(sum(queue))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
cb = [list(map(int, input().split()[::-1])) for _ in range(m)]
a.sort()
cb.sort()
summation = 0
a_tmp = a.pop()
cb_tmp = cb.pop()
for i in range(n):
  if a_tmp >= cb_tmp[0]:
    summation += a_tmp
    if a:
      a_tmp = a.pop()
  else:
    summation += cb_tmp[0]
    cb_tmp[1] -= 1
    if cb_tmp[1] == 0:
      if cb:
        cb_tmp = cb.pop()
      else:
        cb_tmp = [0, n]
print(summation)
Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9 + 7
n, m, k = map(int, input().split())
nm = n * m
fact = [1] * (nm-1)
inv = [1] * (nm-1)
inv_fact = [1] * (nm-1)
for i in range(2, nm-1):
  fact[i] = fact[i-1] * i % mod
  inv[i] = -(mod // i) * inv[mod % i] % mod
  inv_fact[i] = inv_fact[i-1] * inv[i]
comb = fact[nm-2] * inv_fact[k-2] * inv_fact[nm-k] % mod
cost_x = (n-1) * n * (n+1) // 6 * m * m % mod
cost_y = (m-1) * m * (m+1) // 6 * n * n % mod
print(comb * (cost_x + cost_y) % mod)

Fixed submission:
mod = 10**9 + 7
n, m, k = map(int, input().split())
nm = n * m
fact = [1] * (nm-1)
inv = [1] * (nm-1)
inv_fact = [1] * (nm-1)
for i in range(2, nm-1):
  fact[i] = fact[i-1] * i % mod
  inv[i] = -(mod // i) * inv[mod % i] % mod
  inv_fact[i] = inv_fact[i-1] * inv[i] % mod
comb = fact[nm-2] * inv_fact[k-2] * inv_fact[nm-k] % mod
cost_x = (n-1) * nm * (n+1) // 6 * m % mod
cost_y = (m-1) * nm * (m+1) // 6 * n % mod
print(comb * (cost_x + cost_y) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9 + 7
n, m, k = map(int, input().split())
nm = n * m
fact = [1] * (nm+1)
inv = [1] * (nm+1)
inv_fact = [1] * (nm+1)
for i in range(2, nm+1):
  fact[i] = fact[i-1] * i % mod
  inv[i] = -(mod // i) * inv[mod % i] % mod
  inv_fact[i] = inv_fact[i-1] * inv[i]
cost = 0
comb = fact[nm-2] * inv_fact[k-2] * inv_fact[nm-k] % mod
cost_x = 0
cost_y = 0
for dx in range(1, n):
  cost_x += dx * (n - dx) * m * m % mod
for dy in range(1, m):
  cost_y += dy * (m - dy) * n * n % mod
print(comb * (cost_x + cost_y) % mod)

Fixed submission:
mod = 10**9 + 7
n, m, k = map(int, input().split())
nm = n * m
fact = [1] * (nm-1)
inv = [1] * (nm-1)
inv_fact = [1] * (nm-1)
for i in range(2, nm-1):
  fact[i] = fact[i-1] * i % mod
  inv[i] = -(mod // i) * inv[mod % i] % mod
  inv_fact[i] = inv_fact[i-1] * inv[i] % mod
comb = fact[nm-2] * inv_fact[k-2] * inv_fact[nm-k] % mod
cost_x = (n-1) * nm * (n+1) // 6 * m % mod
cost_y = (m-1) * nm * (m+1) // 6 * n % mod
print(comb * (cost_x + cost_y) % mod)
Buggy python submission (verdict: Runtime Error):
n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
  u, v, w = map(int, input())
  adj[u-1].append((w, v-1))
  adj[v-1].append((w, u-1))
paint = [None] * n
paint[0] = 0
stack = [0]
while stack:
  u = stack.pop()
  for w, v in adj[u]:
    if paint[v] is None:
      stack.append(v)
      paint[v] = (paint[u] + w) % 2
for p in paint:
  print(p)

Fixed submission:
n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
  u, v, w = map(int, input().split())
  adj[u-1].append((w, v-1))
  adj[v-1].append((w, u-1))
paint = [None] * n
paint[0] = 0
stack = [0]
while stack:
  u = stack.pop()
  for w, v in adj[u]:
    if paint[v] is None:
      stack.append(v)
      paint[v] = (paint[u] + w) % 2
for p in paint:
  print(p)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for _ in range(m):
  x, y, z = map(int, input().split())
  adj[x-1].append(y-1)
  adj[y-1].append(x-1)
opn = [True] * n
count = 0
for i in range(n):
  if opn:
    count += 1
    opn[i] = False
    stack = [i]
    while stack:
      u = stack.pop()
      for v in adj[u]:
        if opn[v]:
          stack.append(v)
          opn[v] = False
print(count)

Fixed submission:
n, m = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(m):
  x, y, z = map(int, input().split())
  adj[x-1].append(y-1)
  adj[y-1].append(x-1)
opn = [True] * n
count = 0
for i in range(n):
  if opn[i]:
    count += 1
    opn[i] = False
    stack = [i]
    while stack:
      u = stack.pop()
      for v in adj[u]:
        if opn[v]:
          stack.append(v)
          opn[v] = False
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n, k = map(int, input().split())
h = np.array(input().split(), dtype=np.int)
cost = np.array([10 ** 9] * n)
cost[0] = 0
for i in range(n-1):
  cur_h = h[i]
  cur_cost = cost[i]
  for jump in range(1, k + 1):
    if i + jump >= n:
      break
    cost[i+jump] = min(cost[i+jump], cur_cost + abs(cur_h - h[i+jump]))
print(cost[-1])


Fixed submission:
import numpy as np

n, k = map(int, input().split())
h = np.array(input().split(), dtype=np.int)
cost = np.zeros(n, dtype=np.int)
for i in range(1, n):
  cur_h = h[i]
  start = max(0, i - k)
  cost[i] = np.min(cost[start : i] + np.abs(h[start : i] - cur_h))
print(cost[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))
cost = [10 ** 9] * n
cost[0] = 0
for i in range(1, n):
  cur_h = h[i]
  for jump in range(1, k + 1):
    if i < jump:
      break
    cost[i] = min(cost[i], cost[i-jump] + abs(h[i-jump] - cur_h))
print(cost[-1])


Fixed submission:
import numpy as np

n, k = map(int, input().split())
h = np.array(input().split(), dtype=np.int)
cost = np.zeros(n, dtype=np.int)
for i in range(1, n):
  cur_h = h[i]
  start = max(0, i - k)
  cost[i] = np.min(cost[start : i] + np.abs(h[start : i] - cur_h))
print(cost[-1])
Buggy python submission (verdict: Runtime Error):
N, W = map(int, input().split())
dp = np.zeros(W + 1, dtype=np.int)
for _ in range(N):
  w, v = map(int, input().split())
  dp[w:] = np.maximum(dp[w:], dp[:-w] + v)
print(dp[W])

Fixed submission:
import numpy as np

N, W = map(int, input().split())
dp = np.zeros(W + 1, dtype=np.int)
for _ in range(N):
  w, v = map(int, input().split())
  dp[w:] = np.maximum(dp[w:], dp[:-w] + v)
print(dp[W])
Buggy python submission (verdict: Runtime Error):
import numpy as np

N, W = map(int, input().split())
dp = np.zeros(W + 1, dtype=np.int)
for _ in range(N):
  w, v = map(int, input().split())
  dp[w:] = np.maximum(dp[w:], dp[:-w] + v)
print(dp[W])

Fixed submission:
import numpy as np

N, W = map(int, input().split())
dp = np.array([W + 1] * (N * 1000 + 1))
dp[0] = 0
for _ in range(N):
  w, v = map(int, input().split())
  dp[v:] = np.minimum(dp[v:], dp[:-v] + w)
max_value = 0
for v, w in enumerate(dp):
  if w <= W:
    max_value = v
print(max_value)