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

Fixed submission:
from collections import deque

H, W = map(int, input().split())
ch, cw = map(lambda x: int(x)-1, input().split())
dh, dw = map(lambda x: int(x)-1, input().split())
S = [list(input()) for _ in range(H)]
dist = [[-1] * W for _ in range(H)]

q = deque()
q.append((0, ch, cw))
while q:
    c, h, w = q.popleft()
    if dist[h][w] >= 0:
        continue
    dist[h][w] = c
    for hh in range(-2, 3):
        for ww in range(-2, 3):
            if 0 <= h+hh < H and 0 <= w+ww < W and S[h+hh][w+ww] == "." and dist[h+hh][w+ww] < 0:
                if (hh, ww) in ((0,1), (1,0), (-1,0), (0,-1)):
                    q.appendleft((c, h+hh, w+ww)) # 徒歩は前に追加
                else:
                    q.append((c+1, h+hh, w+ww)) # ワープは後ろに追加

print(dist[dh][dw])

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

    H, W = map(int, input().split())
    ch, cw = map(lambda x: int(x)-1, input().split())
    dh, dw = map(lambda x: int(x)-1, input().split())
    S = [list(input()) for _ in range(H)]
    dist = [[-1] * W for _ in range(H)]

    q = deque()
    q.append((0, ch, cw))
    while q:
        c, h, w = q.popleft()
        if dist[h][w] >= 0:
            continue
        dist[h][w] = c
        for hh in range(-2, 3):
            for ww in range(-2, 3):
                if 0 <= h+hh < H and 0 <= w+ww < W and S[h+hh][w+ww] == "." and dist[h+hh][w+ww] < 0:
                    if (hh, ww) in ((0,1), (1,0), (-1,0), (0,-1)):
                        q.appendleft((c, h+hh, w+ww)) # 徒歩は前に追加
                    else:
                        q.append((c+1, h+hh, w+ww)) # ワープは後ろに追加

    print(dist[dh][dw])
Problem p02608 (python)

Fixed submission:
from math import sqrt
N = int(input())
if N == 1:
    print(0)
    import sys
    sys.exit()
ans = [0] * (N+1)
x = int(sqrt(N-2))+1
for j in range(1, x):
    for k in range(1, x):
        for l in range(1, x):
            y = j**2+k**2+l**2+j*k+k*l+l*j
            if y <= N:
                ans[y] += 1
for n in ans[1:N+1]:
    print(n)

Buggy submission (verdict: Runtime Error):
from math import sqrt
N = int(input())
ans = [0] * (N+1)
x = int(sqrt(N-2))+1
for j in range(1, x):
    for k in range(1, x):
        for l in range(1, x):
            y = j**2+k**2+l**2+j*k+k*l+l*j
            if y <= N:
                ans[y] += 1
for n in ans[1:N+1]:
    print(n)
Problem p02617 (python)

Fixed submission:
N = int(input())
ans = 0
for i in range(1, N+1):
    ans += i * (N-i+1)
for _ in range(N-1):
    u, v = map(int, input().split())
    if u > v:
        u, v = v, u
    ans -= u * (N-v+1)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
mod = 10**9 + 7
plus = []
minus = []
zlen = 0
for i in map(int, input().split()):
    if i >= 0:
        plus.append(i)
    elif i < 0:
        minus.append(i)
    else:
        zlen += 1
plen = len(plus)
mlen = len(minus)
if K > plen + mlen:
    print(0)
    import sys
    sys.exit()
plus.sort(reverse=True)
minus.sort()
pp = [1]
mp = [1]
for p in plus:
    pp.append((pp[-1]*p))
for m in minus:
    mp.append((mp[-1]*(-m)))
ppm = [1]
mpm = [1]
for p in plus[::-1]:
    ppm.append((ppm[-1]*p))
for m in minus[::-1]:
    mpm.append((mpm[-1]*(-m)))
ans = -float("inf")
ans_m = float("inf")
# kこマイナスから選ぶ
for k in range(K+1):
    if mlen < k:
        continue
    if plen < K-k:
        continue
    # 最大を目指す
    if k % 2 == 0:
        tmp = mp[k] * pp[K-k]
        ans = max(ans, tmp)
    # 絶対値最小を目指す
    else:
        tmp = mpm[k] * ppm[K-k]
        ans = min(ans, tmp)
if ans == -float("inf"):
    ans = -ans_m
if ans < 0 and zlen > 0:
    print(0)
    import sys
    sys.exit()
print(ans%mod)
Problem p02744 (python)

Fixed submission:
N = int(input())

S = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

def dfs(s, i):
    if len(s) == N:
        return [s]
    result = []
    for j in range(i+1):
        if j == i:
            result += dfs(s+S[j], i+1)
        else:
            result += dfs(s+S[j], i)
    return result
print("\n".join(dfs("", 0)))

Buggy submission (verdict: Runtime Error):
N = int(input())

dic = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

old = ["a"]
new = []
for i in range(1, N):
    for word in old:
        for j in range(i+1):
            if word[-1] == dic[j] or word[-1] == dic[j-1]:
                new.append(word+dic[j])
    old = new.copy()
    new = []
for i in old:
    print(i)

Buggy submission (verdict: Runtime Error):
N = int(input())

dic = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

old = ["a"]
new = []
for i in range(1, N):
    print(old)
    for word in old:
        for j in range(i+1):
            if word[-1] == dic[j] or word[-1] == dic[j-1]:
                new.append(word+dic[j])
    old = new.copy()
    new = []
for i in old:
    print(i)
Problem p02768 (python)

Fixed submission:
def comb_mod(n, k, p):
    """
    nCk mod p
    """
    numerator = 1
    denominator = 1
    for i in range(k):
        numerator *= n - i
        numerator %= p
        denominator *= i + 1
        denominator %= p
    return numerator * pow(denominator, p-2, p) % p

mod = 10**9 + 7
n, a, b = map(int, input().split())
result = pow(2, n, mod) - 1

for i in (a, b):
    result -= comb_mod(n, i, mod)
result %= mod

print(result)

Buggy submission (verdict: Runtime Error):
def pre_cmb_mod(n, mod):
    if n == 0:
        return [1], [1]
    fact = [1, 1]
    inv = [0, 1]
    factinv = [1, 1]
    for i in range(2, n+1):
        fact.append((fact[-1] * i) % mod)
        inv.append((-inv[mod % i] * (mod//i)) % mod)
        factinv.append((factinv[-1] * inv[-1]) % mod)
    return fact, factinv


def cmb_mod(n, r, mod, fact, factinv):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n - r] % mod
  
  mod = 10**9 + 7
n, a, b = map(int, input().split())

fact, factinv = pre_cmb_mod(n, mod)

result = 0
for r in range(1, n+1):
    if r == a or r == b:
        continue
    result += cmb_mod(n, r, mod, fact, factinv)
    result %= mod

print(result)
Problem p02852 (python)

Fixed submission:
from sys import exit
n, m = map(int, input().split())
s = input()

one_sequence = 0
for i in range(1, n):
    if s[i] == "1":
        one_sequence += 1
        if one_sequence == m:
            print(-1)
            exit()
    else:
        one_sequence = 0

pos = n
ans = ""
while pos != 0:
    for i in range(1, m+1)[::-1]:
        if pos-i < 0:
            continue
        if s[pos-i] != "1":
            ans = str(i) + " " + ans
            pos -= i
            break
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
s = input()
len_s = len(s)
inf = 10 ** 18
dp = [[inf,inf] for _ in range(len_s)] # [iまでの手数,一つ前のマス]
for i in range(1, m+1):
    if s[i] == "0":
        dp[i][0] = 1
        dp[i][1] = 0
for i in range(m+1, n+1):
    if i == "1":
        continue
    for j in range(1, m+1)[::-1]:
        if dp[i][0] > dp[i-j][0] + 1:
            dp[i][0] = dp[i-j][0] + 1
            dp[i][1] = i - j
if dp[-1][0] == inf:
    print(-1)
else:
    ans = ""
    x = len_s - 1
    while x > 0:
        ans = str(x - dp[x][1]) + " " + ans
        x = dp[x][1]
    print(ans.strip())
Problem p02936 (python)

Fixed submission:
from sys import setrecursionlimit
from sys import stdin
input = stdin.readline
setrecursionlimit(10**7)

N, Q = map(int, input().split())

def update(v, p):
    for next in tree[v]:
        if next == p:
            continue
        counter[next] += counter[v]
        update(next, v)

tree = [[] for _ in range(N)]
counter = [0] * N

for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
for _ in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x
update(0, -1)
print(" ".join(map(str, counter)))

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

def update(p, x):
    counter[p] += x
    for pp in tree[p]:
        update(pp, x)
    
tree = {i: [] for i in range(N)}
counter = [0] * N
for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
for _ in range(Q):
    p, x = map(int, input().split())
    update(p-1, x)
print(" ".join(map(str, counter)))
Problem p02947 (python)

Fixed submission:
from collections import defaultdict
counter = defaultdict(int)
N = int(input())
for i in range(N):
    counter[tuple(sorted(list(input())))] += 1
ans = 0
for n in counter.values():
    ans += n * (n - 1) // 2
print(ans)

Buggy submission (verdict: Runtime Error):

def create_count_dic(text):
    count_dic = {}
    [count_dic[t] +=1 if t in count_dic else count_dic[t] = 1 for t in text]
    for t in text:
        if t in count_dic:
            count_dic[t] += 1
        else:
            count_dic[t] = 1
    return count_dic

def is_anagram(text1, text2):
    text1_dic = create_count_dic(text1)
    text2_dic = create_count_dic(text2)
    if text1_dic == text2_dic:
        return True
    else:
        return False

N = int(input())
S = [input() for x in range(N)]
ans = 0

for i in range(N):
    for j in range(i+1, N):
        if is_anagram(S[i], S[j]):
            ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):

def create_count_dic(text):
    count_dic = {}
    [count_dic[t] +=1 if t in count_dic else count_dic[t] = 1 for t in text]
    for t in text:
        if t in count_dic:
            count_dic[t] += 1
        else:
            count_dic[t] = 1
    return count_dic

def is_anagram(text1, text2):
    text1_dic = create_count_dic(text1)
    text2_dic = create_count_dic(text2)
    if text1_dic == text2_dic:
        return True
    else:
        return False

N = int(input())
S = [input() for x in range(N)]
ans = 0

for i in range(N):
    for j in range(i+1, N):
        if is_anagram(S[i], S[j]):
            ans += 1

print(ans)
Problem p02949 (python)

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

def dfs(edges, s):
    use = {s}
    stack = [s]
    while stack:
        v = stack.pop()
        for w in edges[v]:
            if w in use: continue
            use.add(w)
            stack.append(w)
    return use

def bellmanford(num_v, edges, source):
    inf = float('inf')
    dist = [inf] * num_v
    dist[source] = 0
    for i in range(num_v):
        flag = False
        for s, t, cost in edges:
            if dist[s] == inf: continue
            if dist[s] + cost < dist[t]:
                dist[t] = dist[s] + cost
                flag = True
        if not flag: break
    else:
        return False
    return dist

def main():
    N, M, P = map(int, input().split())
    G = [[] for _ in range(N)]
    G_rev = [[] for _ in range(N)]
    edges = []
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G_rev[b].append(a)
        edges.append((a, b, P-c))
    
    use = dfs(G_rev, N-1)
    edges = [(a, b, c) for a, b, c in edges if a in use and b in use]
    dist = bellmanford(N, edges, 0)
    if dist:
        print(max(0, -dist[N-1]))
    else:
        print(-1)

if __name__ == '__main__':
    main()

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

def bellmanford(G, s, N):
    n = len(G)
    inf = float('inf')
    dist = [inf] * n
    dist[s] = 0
    for i in range(n+1):
        for v in range(n):
            for w, c in G[v]:
                if dist[w] > dist[v] + c:
                    dist[w] = dist[v] + c
                    if i == n and w == N - 1:
                        return False
    return dist

def bellmanford(G, s):
    n = len(G)
    inf = float('inf')
    dist = [inf] * n
    dist[s] = 0
    for i in range(n):
        for v in range(n):
            for w, c in G[v]:
                if dist[w] > dist[v] + c:
                    dist[w] = dist[v] + c
                    if i == n - 1 and w == n - 1:
                        return -1
    return dist

def main():
    N, M, P = map(int, input().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append((b, -c+P))
    ans = bellmanford(G, 0, N)
    if ans:
        print(max(0, -ans[N-1]))
    else:
        print(-1)

if __name__ == '__main__':
    main()
Problem p02985 (python)

Fixed submission:
N, K = map(int, input().split())
graph = [[] for i in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

stack = [0]
ans = K
mod = 10**9+7
r = [True] * N
r[0] = False
while stack:
    v = stack.pop()
    tmp = K - 1
    if v > 0:
        tmp -= 1
    for child in graph[v]:
        if r[child]:
            r[child] = False
            stack.append(child)
            ans *= tmp
            tmp -= 1
            ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
import math
from functools import lru_cache
mod = 10 ** 9 + 7
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

@lru_cache(maxsize=10**11)
def fact(x):
    return math.factorial(x)

@lru_cache(maxsize=10**11)
def permutation_count(n, r):
    return fact(n) // fact(n-r)

stack = []
for v in graph[0]:
    stack.append((v, 0))
ans = K * permutation_count(K-1, len(graph[0]))
while stack:
    v, parent = stack.pop()
    ans *= permutation_count(K-2, len(graph[v])-1)
    ans %= mod
    for child in graph[v]:
        if child == parent:
            continue
        stack.append((child, v))

print(ans)

Buggy submission (verdict: Runtime Error):
import math
from functools import lru_cache
mod = 10 ** 9 + 7
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

@lru_cache(maxsize=10**6)
def fact(x):
    return math.factorial(x)

@lru_cache(maxsize=10**6)
def permutation_count(n, r):
    return fact(n) // fact(n-r)

stack = []
for v in graph[0]:
    stack.append((v, 0))
ans = K * permutation_count(K-1, len(graph[0]))
while stack:
    v, parent = stack.pop()
    ans *= permutation_count(K-2, len(graph[v])-1)
    ans %= mod
    for child in graph[v]:
        if child == parent:
            continue
        stack.append((child, v))

print(ans)

Buggy submission (verdict: Runtime Error):
import math
from functools import lru_cache
mod = 10 ** 9 + 7
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

@lru_cache()
def fact(x):
    return math.factorial(x)

@lru_cache()
def permutation_count(n, r):
    return fact(n) // fact(n-r)

stack = []
for v in graph[0]:
    stack.append((v, 0))
ans = K * permutation_count(K-1, len(graph[0]))
while stack:
    v, parent = stack.pop()
    ans *= permutation_count(K-2, len(graph[v])-1)
    ans %= mod
    for child in graph[v]:
        if child == parent:
            continue
        stack.append((child, v))

print(ans)
Problem p03047 (python)

Fixed submission:
N, K = map(int, input().split())
print(N-K+1)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = []
a = 0
b = 0
both = 0
count = 0
for i in range(N):
    s = input()
    S.append(s)
    if "AB" in s:
        count += 1
    if s[0] == "B":
        b += 1
        if s[-1] == "A":
            both += 1
    if s[-1] == "A":
        a += 1
a -= both
b -= both
if a>0 and b>0:
    count += both+1 + min(a-1, b-1)
elif (a>0 and b==0) or (a==0 and b>0):
    count += both
else:
    count += both -1


print(count)
Problem p03050 (python)

Fixed submission:
n = int(input())

result = 0

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

for i in make_divisors(n):
    x = n//i - 1
    if x!=0:
        if n%x == n//x:
            result+=x

print(result)

Buggy submission (verdict: Runtime Error):
n = input()

result = 0

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

for i in make_divisors(n):
    x = n//c - 1
    if n%x == n//x:
        result+=x

print(result)
Problem p03061 (python)

Fixed submission:
from functools import reduce

result = 0

def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)

n = int(input())
A = list(map(int, input().split()))
X = [0 for i in range(n)]
Y = [0 for i in range(n)]
for i in range(1, n):
    X[i] = gcd(X[i-1], A[i-1])
for i in range(n-1)[::-1]:
    Y[i] = gcd(Y[i+1], A[i+1])
print(max(gcd(X[i], Y[i]) for i in range(n)))

Buggy submission (verdict: Runtime Error):
from functools import reduce

result = 0

def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)

n = int(input())
A = list(map(int, input().split()))
X = [0 for i in range n]
Y = [0 for i in range n]
for i in range(1, N):
    X[i] = gcd(X[i-1], A[i-1])
for i in range(n-1)[::-1]:
    Y[i] = gcd(Y[i+1], A[i+1])
print(max(gcd(X[i], Y[i]) for i in range(n)))

Buggy submission (verdict: Runtime Error):
import math
from functools import reduce
import fraction

result = 0

n = int(input())
A = list(map(int, input().split()))
for i in range(n):
    result = max(result, reduce(fraction.gcd, A[:i]+A[i+1:]))

print(result)

Buggy submission (verdict: Runtime Error):
import math
from functools import reduce

result = 0

n = int(input())
A = list(map(int, input().split()))
for i in range(n):
    result = max(result, reduce(math.gcd, A[:i]+A[i+1:]))

print(result)
Problem p03068 (python)

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

correct = s[k-1]
ans =""
for i in s:
    if i != correct:
        ans += "*"
    else:
        ans+= i

print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
k=input()

correct = s[k-1]
ans =""
for i in s:
    if i != correct:
        ans += "*"
    else:
        ans+= i

print(ans)
Problem p03076 (python)

Fixed submission:
li = [int(input()) for _ in range(5)]

time = 0
non10li = []
for i in li:
    if i%10==0:
        time+=i
    else:
        non10li.append(i)
if len(non10li)!=0:
    minili = [i%10 for i in non10li]
    ind = non10li.pop(minili.index(min(minili)))
    for i in non10li:
        time+=(i//10)*10+10

    time+=ind
print(time)

Buggy submission (verdict: Runtime Error):
li = [int(input()) for _ in range(5)]

time = 0
non10li = []
for i in li:
    if i%10==0:
        time+=i
    else:
        non10li.append(i)

minili = [i%10 for i in non10li]
ind = non10li.pop(minili.index(min(minili)))
for i in non10li:
    time+=(i//10)*10+10

time+=ind
print(time)
Problem p03079 (python)

Fixed submission:
A, B, C = map(int, input().split())

if A == B and B == C:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())

num=0

for s in input():
    if s=="R":
        num+=1
    else:
        num-=1

if num>0:
    print("Yes")
else:
    print("No")
Problem p03160 (python)

Fixed submission:
inf = 10**9 + 7
n = int(input())
nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0


for i in range(1, n):
    dp[i] = min(dp[i], dp[i-1]+abs(nodes[i]-nodes[i-1]))
    if i > 1:
        dp[i] = min(dp[i], dp[i-2]+abs(nodes[i]-nodes[i-2]))

print(dp[n-1])

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

dp = [0] * (N+2)

for i in range(N):
    dp[i+1] = min(dp[i+1], dp[i]+abs(h[i]-h[i+1]))
    dp[i+2] = min(dp[i+2], dp[i]+abs(h[i]-h[i+2]))

print(dp[N-1])
Problem p03161 (python)

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

nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0

for i in range(n):
    for j in range(i+1, min(i+k+1, n)):
        dp[j] = min(dp[j], dp[i] + abs(nodes[i] - nodes[j]))
# for i in range(n):
#     for j in range(1, min(k+1, n-i)):
#         dp[i+j] = min(dp[i+j], dp[i]+abs(nodes[i]-nodes[i+j]))
print(dp[n-1])

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

nodes = list(map(int, input().split()))
dp = [inf] * n

dp[0] = 0

# for i in range(n):
#     for j in range(i+1, min(i+k+1, n)):
#         dp[j] = min(dp[j], dp[i] + abs(nodes[i] - nodes[j]))
for i in range(n):
    for j in range(k):
        dp[i+j] = min(dp[i+j], dp[i]+abs(nodes[i]-nodes[i+j]))
print(dp[n-1])
Problem p03164 (python)

Fixed submission:
N, W = map(int, input().split())
V = 10 ** 5
inf = 10 ** 18
dp = [inf] * (V + 1)
dp[0] = 0

for n in range(1, N+1):
    w, v = map(int, input().split())
    for i in range(v, V+1)[::-1]:
        if dp[i-v]+w <= W:
            dp[i] = min(dp[i], dp[i-v]+w)

for i in range(V+1)[::-1]:
    if dp[i] < inf:
        print(i)
        break

Buggy submission (verdict: Runtime Error):
import numpy as np

n, W = map(int, input().split())
w, v = [0] * n, [0] * n
for i in range(n):
    w[i], v[i] = map(int, input().split())
ndp = np.zeros(W + 1, dtype=np.int64)

for w, v in zip(w, v):
    # [:w]は品物の重量がナップサックの空きよりも重いので更新しない。
    # ndp[w:] は通常DPでのdp[i][w]=dp[i-1][w]。そのままでの更新。
    # ndp[:-w] + v は[:-w]が[w-weight]に相当。
    np.maximum(ndp[:-w] + v, ndp[w:], out=ndp[w:])

print(ndp[-1])
Problem p03166 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5+1)

N, M = map(int, input().split())
graph = {i+1:[] for i in range(N)}

for _ in range(M):
    x, y = map(int, input().split())
    graph[x].append(y)

"""
dp[v]: ノードv(1~N)を始点とした最大長
"""
dp = [0] * (1+N)

def max_length_from(v):
    if dp[v] != 0:
        return dp[v]
    max_length = 0
    for vv in graph[v]:
        tmp = max_length_from(vv) + 1
        if tmp > max_length:
            max_length = tmp
    dp[v] = max_length
    return max_length

for v in range(1, N+1):
    max_length_from(v)

print(max(dp))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
graph = {i+1:[] for i in range(N)}

for _ in range(M):
    x, y = map(int, input().split())
    graph[x].append(y)

"""
dp[v]: ノードv(1~N)を始点とした時の、最大長
"""
dp = [0] * (1+N)

def max_length_from(v):
    if dp[v] != 0:
        return dp[v]
    max_length = 0
    for vv in graph[v]:
        tmp = max_length_from(vv) + 1
        if tmp > max_length:
            max_length = tmp
    dp[v] = max_length
    return max_length

for v in range(1, N+1):
    max_length_from(v)

print(max(dp))
Problem p03281 (python)

Fixed submission:
N = int(input())
ans = 0
for i in range(1, N+1, 2):
    count = 0
    for j in range(1, i//2+1):
        if i % j == 0:
            count += 1
    if count == 7:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
ans = 0
for i in range(1, N+1, 2):
    count = 0
    for j in range(1, i//2+1):
        if i % j == 0:
            count += 1
    if count == 7:
        ans += 1
print(ans)