Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

s = list(input())
t = list(input())

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(a, b):
	return a * b // gcd (a, b)

l = lcm(n,m)

x = ['' for _ in range(l)]
step = l // n
for i in range(n):
    x[i * step] = s[i]

#print(x)
step = l // m
for i in range(m):
    if x[i * step] != t[i] and x[i * step] != '':
        print(-1)
        exit()

print(l)

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

s = list(input())
t = list(input())

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(a, b):
	return a * b // gcd (a, b)

l = lcm(n,m)

#x = ['' for _ in range(l)]
step = l // n
step_m = l // m
for i in range(n):
    if (step * i) % step_m == 0:
        if s[i] != t[(step * i) // step_m]:
            #print(step, i, (step * i) // step_m, s[i], t[(step * i) // step_m])
            print(-1)
            exit()

print(l)
Buggy python submission (verdict: Runtime Error):
import math
n, m = map(int, input().split())

def IsPrime(num):
    if num < 2:
        return False
    elif num == 2:
        return True
    elif num % 2 == 0:
        return False

    sqrtNum = math.sqrt(num)
    for i in (3, sqrtNum + 1, 2):
        if num % i == 0:
            return False

    return True

if IsPrime(m):
    print(1)
    exit()


ans = 0
for i in range(m // n, -1, -1):
    #print(i)
    if m % i == 0 and m // i >= n:
        ans = i
        break

print(ans)


Fixed submission:
n = int(input())

if n == 1:
    print('Hello World')
else:
    a = int(input())
    b = int(input())
    print(a + b)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

n, m = map(int, input().split())

MOD = 10 ** 9 + 7

if m == 1:
    print('1')
    exit()

factor = []
temp = 0
x = 2
while m > 1:
    if m % x == 0:
        m //= x
        temp += 1
    elif temp > 0:
        factor.append(temp)
        temp = 0
        x += 1
    else:
        x += 1
factor.append(temp)

nCr = {}
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n;
    if (n, r) in nCr: return nCr[(n, r)]

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p;
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    nCr[(n, r)] = result
    return result;

ans = 1
for x in factor:
    ans *= cmb(x + n - 1, n - 1)
    ans %= MOD

print(ans)

Fixed submission:
a = list(map(int, input().split()))

a = sorted(a)

print(a[2] * 10 + sum(a[:2]))
Buggy python submission (verdict: Runtime Error):
import math

n, X = map(int, input().split())
x = list(map(int, input().split()))

d = [0 for _ in range(n)]
for i in range(n):
    d[i] = abs(x[i] - X)

d = sorted(list(set(d)))

if n == 1:
    print(d[0])
    exit()

ans = d[0]
for i in range(1, len(d)):
    ans = math.gcd(ans, d[i])

print(ans)

Fixed submission:
n, X = map(int, input().split())
x = list(map(int, input().split()))

d = [0 for _ in range(n)]
for i in range(n):
    d[i] = abs(x[i] - X)

d = sorted(list(set(d)))

if n == 1:
    print(d[0])
    exit()

def calc_gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return calc_gcd(b, c)

ans = d[0]
for i in range(1, len(d)):
    ans = calc_gcd(ans, d[i])

print(ans)
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
x = list(map(int, input().split()))

for center in range(n):
    if x[center] >= 0:
        break

if center == n or (center == n - 1 and x[n - 1] == 0):
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - 1]))

print(ans)

Fixed submission:
n, k = map(int, input().split())
x = list(map(int, input().split()))

for center in range(n):
    if x[center] >= 0:
        break

if center == n - 1:
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - i]))

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())

a = []

if n > 0:
    while n > 0:
        a.append(n % 2)
        n //= 2

    a.append(0)
    a.append(0)
    for i in range(len(a) - 1):
        if i % 2 == 1:
            if a[i] == 1:
                a[i + 1] += 1
            elif a[i] == 2:
                a[i + 1] += 1
                a[i] = 0
        else:
            if a[i] == 2:
                a[i + 1] += 1
                a[i] = 0
        a[i] = str(a[i])

    for i in range(len(a) - 1, -1, -1):
        if a[i] == 0 or a[i] == '0':
            a[i] = ''
        else:
            break
    print(''.join(reversed(a)))
elif n == 0:
    print('0')
else:
    n = 0 - n
    while n > 0:
        a.append(n % 2)
        n //= 2

    a.append(0)
    a.append(0)
    for i in range(len(a) - 1):
        if i % 2 != 1:
            if a[i] == 1:
                a[i + 1] += 1
            elif a[i] == 2:
                a[i + 1] += 1
                a[i] = 0
        else:
            if a[i] == 2:
                a[i + 1] += 1
                a[i] = 0
        a[i] = str(a[i])

    for i in range(len(a) - 1, -1, -1):
        if a[i] == 0 or a[i] == '0':
            a[i] = ''
        else:
            break
    print(''.join(reversed(a)))

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

if n % k == 0:
    print('0')
else:
    print('1')

Buggy python submission (verdict: Runtime Error):
if n % k == 0:
    print('0')
else:
    print('1')

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

if n % k == 0:
    print('0')
else:
    print('1')
Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

n = int(input())
a = list(map(int, input().split()))

a = sorted(a)

def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

temp = lcm_list(a) - 1

result = 0
for i in a:
    result += temp % i

print(result)

Fixed submission:
from fractions import gcd
from functools import reduce

n = int(input())
a = list(map(int, input().split()))

a = sorted(a)

def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

temp = lcm_list(a) - 1

result = 0
for i in a:
    result += temp % i

print(result)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from collections import defaultdict

n, m, s, t = map(int, input().split())

money = defaultdict(set)
snuke = defaultdict(set)
for i in range(m):
    a, b, c, d = map(int, input().split())
    money[a-1].add((b-1, c))
    money[b-1].add((a-1, c))
    snuke[a-1].add((b-1, d))
    snuke[b-1].add((a-1, d))

def dijkstra(n, v, dedic):
    d = [float('inf')] * n
    d[v] = 0

    hq = [(0, v)]

    while hq:
        u = heapq.heappop(hq)[1]
        for nv, c in dedic[u]:
            if d[nv] > d[u] + c:
                d[nv] = d[u] + c
                heapq.heappush(hq, (d[u] + c, nv))

    return d

money_cost = dijkstra(n, s - 1, money)
snuke_cost = dijkstra(n, t - 1, snuke)

result = [10 ** 15 for _ in range(n)]
for i in range(n):
    result[i] -= money_cost[i] + snuke_cost[i]

for i in range(n):
    print(max(result[i:]))


Fixed submission:
import heapq
from collections import defaultdict

n, m, s, t = map(int, input().split())

money = defaultdict(set)
snuke = defaultdict(set)
for i in range(m):
    a, b, c, d = map(int, input().split())
    money[a-1].add((b-1, c))
    money[b-1].add((a-1, c))
    snuke[a-1].add((b-1, d))
    snuke[b-1].add((a-1, d))

def dijkstra(n, v, dedic):
    d = [float('inf')] * n
    d[v] = 0

    hq = [(0, v)]

    while hq:
        u = heapq.heappop(hq)[1]
        for nv, c in dedic[u]:
            if d[nv] > d[u] + c:
                d[nv] = d[u] + c
                heapq.heappush(hq, (d[u] + c, nv))

    return d

money_cost = dijkstra(n, s - 1, money)
snuke_cost = dijkstra(n, t - 1, snuke)

result = [10 ** 15 for _ in range(n)]
temp = 10 ** 15
for i in range(n - 1, -1, -1):
    temp = min(money_cost[i] + snuke_cost[i], temp)
    result[i] -= temp

for i in range(n):
    print(result[i])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

for i in range(n):
  a[i] -= i + 1

average = sum(a) // n

#print(average)

def sadness(a, ave):
  sad = 0
  for i in range(n):
    sad += abs(a[i] - ave)
  return sad

result = sadness(a, average)

temp = average
for i in range(1, n):
  temp_result = sadness(a, average + i)
  if result < temp_result:
    break
  else:
     temp = average + i
     result = temp_result

for i in range(1, n):
  temp_result = sadness(a, average - i)
  if result < temp_result:
    break
  else:
     temp = average - i
     result = temp_result

print(result)

Fixed submission:
from statistics import median

n = int(input())
a = list(map(int, input().split()))

for i in range(n):
  a[i] -= i + 1

ave = int(median(a))

sad = 0
for i in range(n):
  sad += abs(a[i] - ave)

print(sad)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(input())

result = n
for i in range(n):
    temp = s[:i].count("W") + s[i:].count("E")
    if temp < result:
        result = temp

print(result)

Fixed submission:
n = int(input())
s = list(input())

result = s[1:].count("E")
temp = result
for i in range(n-1):
    if s[i] == "W":
        temp += 1
    if s[i+1] == "E":
        temp -= 1
    if temp < result:
        result = temp

print(result)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

result = 0
for i in range(n):
    counter = 0
    temp = 0
    for j in range(i, n):
        if a[j] ^ temp != a[j] + temp:
            temp = a[j]
            break
        else:
            temp += a[j]
            counter += 1
    result += counter


print(result)

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

result = 0
counter = 1
temp = 0
for i in range(n):
    counter -= 1
    start = i + counter
    for j in range(start, n):
        if a[j] ^ temp != a[j] + temp:
            temp -= a[i]
            break
        else:
            temp += a[j]
            counter += 1
    result += counter


print(result)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

p = []
for i in range(n):
    p.append(int(input()))

dic = {p[0]:1}
for i in range(1,n):
    #print(dic)
    check = 0
    for k in list(dic):
        #print(k, dic[k])
        if p[i] == k + dic[k]:
            dic[k] += 1
            check = 1
            break
        elif p[i] == k - 1:
            dic[p[i]] = 1
            if dic[k] < max(dic.values()):
                dic.pop(k)
        else:
            dic[p[i]] = 1

print(n - max(dic.values()))

Fixed submission:
n = int(input())

p = []
for i in range(n):
    p.append(int(input()))


q = [0 for i in range(n)]
for i in range(n):
    q[p[i] - 1] = i

result = 1
temp = 1
for i in range(1, n):
    if q[i] < q[i-1]:
        if temp > result:
            result = temp
        temp = 1
    else:
        temp += 1

if temp == n:
    result = temp

print(n - result)
Buggy python submission (verdict: Runtime Error):
n, c = map(int, input().split())

x = [0 for _ in range(n + 2)]
v = [0 for _ in range(n + 1)]
for i in range(n):
    x[i + 1], v[i + 1] = map(int, input().split())

x[n + 1] = c

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(n + 1)]
left_rev = [0 for _ in range(n + 1)]
right_rev = [0 for _ in range(n + 1)]

for i in range(1, n + 1):
    left[i] = left[i - 1] + v[i] - (x[i] - x[i - 1])
    left_rev[i] = left[i] - x[i]
    right[i] = right[i - 1] + v[n - i + 1] - (x[n - i + 2] - x[n - i + 1])
    right_rev[i] = right[i] - (c - x[n - i + 1])

result = 0
for i in range(1, n + 1):
    temp = max( (left[i] + max(right_rev[:n - i + 1])) , (right[i] + max(left_rev[:n - i + 1])) )
    if temp > result:
        result = temp

print(result)

Fixed submission:
s = list(input())

print(700 + s.count('o') * 100)
Buggy python submission (verdict: Time Limit Exceeded):
n, c = map(int, input().split())

x = [0 for _ in range(n + 2)]
v = [0 for _ in range(n + 1)]
for i in range(n):
    x[i + 1], v[i + 1] = map(int, input().split())

x[n + 1] = c

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(n + 1)]
left_rev = [0 for _ in range(n + 1)]
right_rev = [0 for _ in range(n + 1)]

for i in range(1, n + 1):
    left[i] = left[i - 1] + v[i] - (x[i] - x[i - 1])
    left_rev[i] = left[i] - x[i]
    right[i] = right[i - 1] + v[n - i + 1] - (x[n - i + 2] - x[n - i + 1])
    right_rev[i] = right[i] - (c - x[n - i + 1])

result = 0
for i in range(1, n + 1):
    temp = max( (left[i] + max(right_rev[:n - i + 1])) , (right[i] + max(left_rev[:n - i + 1])) )
    if temp > result:
        result = temp

print(result)

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

x = [0 for _ in range(n + 2)]
v = [0 for _ in range(n + 1)]
for i in range(n):
    x[i + 1], v[i + 1] = map(int, input().split())

x[n + 1] = c

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(n + 1)]
left_rev = [0 for _ in range(n + 1)]
right_rev = [0 for _ in range(n + 1)]

left_temp = 0
right_temp = 0
for i in range(1, n + 1):
    left[i] = left[i - 1] + v[i] - (x[i] - x[i - 1])
    if left_temp < left[i] - x[i]:
        left_rev[i] = left[i] - x[i]
        left_temp = left_rev[i]
    else:
        left_rev[i] = left_temp
    right[i] = right[i - 1] + v[n - i + 1] - (x[n - i + 2] - x[n - i + 1])
    if right_temp < right[i] - (c - x[n - i + 1]):
        right_rev[i] = right[i] - (c - x[n - i + 1])
        right_temp = right_rev[i]
    else:
        right_rev[i] = right_temp

result = 0
for i in range(1, n + 1):
    result = max( (left[i] + right_rev[n - i]) , (right[i] + left_rev[n - i]) , result )

print(result)
Buggy python submission (verdict: Time Limit Exceeded):
import copy
from statistics import median

n = int(input())
x = list(map(int, input().split()))

y = copy.deepcopy(x)
for a in x:
    y.remove(a)
    print(median(y))
    y.append(a)

Fixed submission:
import copy

n = int(input())
x = list(map(int, input().split()))

if n == 2:
    print(x[1])
    print(x[0])
    exit()

y = copy.deepcopy(x)
y.sort()
mid_s = y[n // 2 - 1]
mid_l = y[n // 2]
for a in x:
    if a < mid_l:
        print(mid_l)
    else:
        print(mid_s)
Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())

n = len(s)

a = [0] * n

MOD = 998244353

for i in range(n):
    a[i] = ord(s[i]) - ord('a')

if max(a) == min(a):
    print(1)
    exit()
elif n == 3:
    def solver(m):
        x = m // 100
        y = ( m % 100 ) // 10
        z = m % 10
        if x != y:
            c = (3 - x - y) % 3
            temp = c * 110 + z
            if temp not in ans:
                ans.add(temp)
                solver(temp)
        if y != z:
            c = (3 - y - z) % 3
            temp = x * 100 + c * 11
            if temp not in ans:
                ans.add(temp)
                solver(temp)

    t = a[0] * 100 + a[1] * 10 + a[2]
    ans = set()
    ans.add(t)
    solver(t)
    print(len(ans))
    exit()
elif n == 2:
    print(2)
    exit()

dp = [[0,0,0] for _ in range(3)]

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

for i in range(n-1):
    T = [[0,0,0] for _ in range(3)]
    T[0][0] = dp[1][0] + dp[2][0]
    T[0][1] = dp[1][1] + dp[2][1]
    T[0][2] = dp[1][2] + dp[2][2]
    T[1][0] = dp[0][2] + dp[2][2]
    T[1][1] = dp[0][0] + dp[2][0]
    T[1][2] = dp[0][1] + dp[2][1]
    T[2][0] = dp[0][1] + dp[1][1]
    T[2][1] = dp[0][2] + dp[1][2]
    T[2][2] = dp[0][0] + dp[1][0]
    dp, T = T, dp

m = sum(a) % 3

ans = 3 ** (n-1) - (dp[0][m] + dp[1][m] + dp[2][m])

check = 1
for i in range(n-1):
    if a[i] == a[i+1]:
        check = 0
        break

ans += check

print(ans % MOD)


Fixed submission:
s = list(input())

n = len(s)

a = [0] * n

MOD = 998244353

for i in range(n):
    a[i] = ord(s[i]) - ord('a')

if max(a) == min(a):
    print(1)
    exit()
elif n == 3:
    def solver(m):
        x = m // 100
        y = ( m % 100 ) // 10
        z = m % 10
        if x != y:
            c = (3 - x - y) % 3
            temp = c * 110 + z
            if temp not in ans:
                ans.add(temp)
                solver(temp)
        if y != z:
            c = (3 - y - z) % 3
            temp = x * 100 + c * 11
            if temp not in ans:
                ans.add(temp)
                solver(temp)

    t = a[0] * 100 + a[1] * 10 + a[2]
    ans = set()
    ans.add(t)
    solver(t)
    print(len(ans))
    exit()
elif n == 2:
    print(2)
    exit()

dp = [[0,0,0] for _ in range(3)]

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

for i in range(n-1):
    T = [[0,0,0] for _ in range(3)]
    T[0][0] = (dp[1][0] + dp[2][0]) % MOD
    T[0][1] = (dp[1][1] + dp[2][1]) % MOD
    T[0][2] = (dp[1][2] + dp[2][2]) % MOD
    T[1][0] = (dp[0][2] + dp[2][2]) % MOD
    T[1][1] = (dp[0][0] + dp[2][0]) % MOD
    T[1][2] = (dp[0][1] + dp[2][1]) % MOD
    T[2][0] = (dp[0][1] + dp[1][1]) % MOD
    T[2][1] = (dp[0][2] + dp[1][2]) % MOD
    T[2][2] = (dp[0][0] + dp[1][0]) % MOD
    dp, T = T, dp

m = sum(a) % 3

ans = 3 ** (n-1) - (dp[0][m] + dp[1][m] + dp[2][m])

check = 1
for i in range(n-1):
    if a[i] == a[i+1]:
        check = 0
        break

ans += check

#print(dp, 2 ** (n-1))

print(ans % MOD)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

k = 1
ans = 0
for _ in range(32):
    t = k * 2
    temp_b = sorted([x % t for x in b])
    check = 0
    for temp_a in a:
        x = temp_a % t
        check += bisect_left(temp_b, 2 * k - x) - bisect_left(temp_b, k - x) + bisect_left(temp_b, 4 * k - x) - bisect_left(temp_b, 3 * k - x)
    ans += k * (check % 2)
    k = t

print(ans)


Fixed submission:
a, b, c = map(int, input().split())

if a + b >= c:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
a = list(input())
b = list(input())
c = list(input())

print(a[0] + b[1] + c[2]

Fixed submission:
a = list(input())
b = list(input())
c = list(input())

print(a[0] + b[1] + c[2])
Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())

p = [[] for _ in range(h * w)]

for i in range(h):
    temp = list(map(int, input().split()))
    for j in range(w):
        p[temp[j] - 1] = [i, j]

q = int(input())
a = [[int(x) - 1 for x in input().split()] for _ in range(q)]

for i in range(q):
    temp = 0
    j = a[i][1]
    while j > a[i][0]:
        temp += abs(p[j][0] - p[j - d][0]) + abs(p[j][1] - p[j - d][1])
        j -= d
    print(temp)


Fixed submission:
h, w, d = map(int, input().split())

p = [[] for _ in range(h * w)]

for i in range(h):
    temp = list(map(int, input().split()))
    for j in range(w):
        p[temp[j] - 1] = [i, j]

q = int(input())
a = [[int(x) - 1 for x in input().split()] for _ in range(q)]

s = [0 for _ in range(h * w)]
for i in range(d, h * w):
    s[i] = s[i - d] + abs(p[i][0] - p[i - d][0]) + abs(p[i][1] - p[i - d][1])

for i in range(q):
    print(s[a[i][1]] - s[a[i][0]])

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())

p = [[] for _ in range(h * w)]

for i in range(h):
    temp = list(map(int, input().split()))
    for j in range(w):
        p[temp[j] - 1] = [i, j]

q = int(input())
a = [[int(x) - 1 for x in input().split()] for _ in range(q)]

s = [0 for _ in range(h * w)]
for i in range(d, h * w):
    s[i] = abs(p[i][0] - p[i - d][0]) + abs(p[i][1] - p[i - d][1])

for i in range(q):
    temp = 0
    j = a[i][1]
    while j > a[i][0]:
        temp += s[j]
        j -= d
    print(temp)


Fixed submission:
h, w, d = map(int, input().split())

p = [[] for _ in range(h * w)]

for i in range(h):
    temp = list(map(int, input().split()))
    for j in range(w):
        p[temp[j] - 1] = [i, j]

q = int(input())
a = [[int(x) - 1 for x in input().split()] for _ in range(q)]

s = [0 for _ in range(h * w)]
for i in range(d, h * w):
    s[i] = s[i - d] + abs(p[i][0] - p[i - d][0]) + abs(p[i][1] - p[i - d][1])

for i in range(q):
    print(s[a[i][1]] - s[a[i][0]])
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
lrd = []

MAX = 999999

p = [[] for _ in range(n)]
for i in range(m):
    l, r, d = map(int, input().split())
    lrd.append([l-1, r-1, d, 0])
    p[l-1].append(i)
    p[r-1].append(i)

if m <= 1:
    print('Yes')
    exit()

a = [MAX for _ in range(n)]

a[lrd[0][0]] = 0
temp = [lrd[0][0]]
next = []
t = 0
s = 0
max_a = 0
min_a = 0
while 1:
    next = []
    for x in temp:
        for y in p[x]:
            if lrd[y][0] == x:
                t = lrd[y][1]
                s = a[x] + lrd[y][2]
            else:
                t = lrd[y][0]
                s = a[x] - lrd[y][2]

            lrd[y][3] = 1
            if a[t] == MAX:
                a[t] = s
                next.append(t)
            elif a[t] != s:
                print('No')
                exit()

    if len(next) == 0:
        target = 0
        i = 0
        check = 0
        for i in range(m):
            if lrd[i][3] == 0:
                target = lrd[i][0]
                check = 1
        if check == 0:
            break
        a[target] = 0
        next = [target]

    temp = [] + list(set(next))

print('Yes')


Fixed submission:
x = int(input())
a = int(input())
b = int(input())

x -= a

print(x % b)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

ans = n
for x in set(a):
    if a.count(x) >= x:
        ans -= x

print(ans)

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

b = set(a)

hoge = {}

for x in b:
    hoge[x] = 0

for x in a:
    hoge[x] += 1

ans = n
for x in b:
    if hoge[x] >= x:
        ans -= x

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, k  = map(int, input().split())
a = list(map(int, input().split()))

a = sorted(a)
p = list(set(a))

if len(p) <= k:
    print('0')
    exit()

j = 0
ans = [0 for _ in range(len(p))]
for i in range(len(p)):
    ans[i] += a.count(p[i])

ans = sorted(ans)

print(sum(ans[:len(p) - k]))

Fixed submission:
n, k  = map(int, input().split())
a = list(map(int, input().split()))

a = sorted(a)
p = list(set(a))

if len(p) <= k:
    print('0')
    exit()

j = 0
ans = [0 for _ in range(len(p))]
ans[0] = 1
for i in range(1, n):
    if a[i] == a[i - 1]:
        ans[j] += 1
    else:
        j += 1
        ans[j] += 1

ans = sorted(ans)

print(sum(ans[:len(p) - k]))