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

Fixed submission:
N, M = map(int, input().split())		# 5 7

h=[]
for i in range(N+1):
	h.append([])

a=[]
b=[]

for i in range(M):
	A, B = map(int, input().split())		# 5 7
	h[A].append(B)
	h[B].append(A)
	a.append(A)
	b.append(B)




def dfs(Graph, Start, Visited=None):
	if(Visited==None): Visited = []
	Visited.append(Start)
	#print(Start, Visited)
	for Next in Graph[Start]:
		if( Next in Visited ): 
			#print("   Visited", Next, Visited)
			continue
		dfs(Graph, Next, Visited)

z=[_ for _ in range(1, N+1)]
#print(z)
ans=0

import copy
for i in range(M):
	g = copy.deepcopy(h)
	
	A = a[i]
	B = b[i]

	g[A].remove(B)
	g[B].remove(A)
	
	Visited = []
	dfs(g, 1, Visited)
	
	#print(A, B, i, Visited)
	if len( list(set(z) - set(Visited)) ) > 0:
	    ans += 1

	
print(ans)

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

h=[]
for i in range(N+1):
	h.append([])

a=[]
b=[]

for i in range(M):
	A, B = map(int, input().split())		# 5 7
	h[A].append(B)
	h[B].append(A)
	a.append(A)
	b.append(B)



def dfs(Graph, Start, Visited=None):
	if(Visited==None): Visited = []
	Visited.append(Start)
	#print(Start, Visited)
	for Next in Graph[Start]:
		if( Next in Visited ): 
			#print("   Visited", Next, Visited)
			continue
		dfs(Graph, Next, Visited)

z=[_ for _ in range(1, N+1)]
#print(z)
ans=0

import copy
for i in range(N):
	g = copy.deepcopy(h)
	
	A = a[i]
	B = b[i]
	n = i+1

	g[A].remove(B)
	g[B].remove(A)
	
	Visited = []
	dfs(g, n, Visited)
	
	#print(Visited)
	if len( list(set(z) - set(Visited)) ) > 0:
	    ans += 1

	
print(ans)
Problem p03612 (python)

Fixed submission:
N = int(input())						# 5

s = "0 " + input()
a = [int(i) for i in s.split()]	# 1 2 3 4 5 ...


ans = 0
bSkip = False

for i in range(N+1):
	if i==0: continue;
	if bSkip:
		bSkip = False
		continue;
#	print(i, a[i])
	
	if i==a[i]:
		ans += 1
		if(i<N):
			if(i+1)==a[i+1]:
				bSkip = True

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())						# 5
#a = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
s = "0 " + input()
a = [int(i) for i in s.split()]	# 1 2 3 4 5 ...

N = len(a)
#print(a)

ans = 0
bSkip = False

for i in range(N):
	if i==0: continue;
	if bSkip:
		bSkip = False
		continue;
#	print(i, a[i])
	
	if i==a[i]:
		ans += 1
		if(i<N):
			if(i+1)==a[i+1]:
				bSkip = True

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())						# 5
#a = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
s = "0 " + input()
a = [int(i) for i in s.split()]	# 1 2 3 4 5 ...

N = len(a)
#print(a)

ans = 0
bSkip = False

for i in range(N):
	if i==0: continue;
	if bSkip:
		bSkip = False
		continue;
#	print(i, a[i])
	
	if i==a[i]:
		ans += 1
		if(i+1)==a[i+1]:
			bSkip = True

print(ans)
Problem p03651 (python)

Fixed submission:
import sys
from fractions import gcd

PO = "POSSIBLE"
IM = "IMPOSSIBLE"
ans = ""


N, K = map(int, input().split())		# 5 7
a = sorted(list(map(int, input().split())))	# 1 2 3 4 5 ...

if max(a)<K:
	print(IM)
	sys.exit()

if K in a:
	print(PO)
	sys.exit()

g=a[0]
for i in range(1,N):
	g = gcd(g,a[i])

if K%g==0:
	ans=PO
else:
	ans=IM


print(ans)

Buggy submission (verdict: Runtime Error):
from fractions import gcd

PO = "POSSIBLE"
IM = "IMPOSSIBLE"
ans = ""


N, K = map(int, input().split())		# 5 7
a = sorted(list(map(int, input().split())))	# 1 2 3 4 5 ...

if max(a)<K:
	print(IM)
	sys.exit()

if K in a:
	print(PO)
	sys.exit()

g=a[0]
for i in range(1,N):
	g = gcd(g,a[i])

if K%g==0:
	ans=PO
else:
	ans=IM


print(ans)

Buggy submission (verdict: Runtime Error):
from fractions import gcd

PO = "POSSIBLE"
IM = "IMPOSSIBLE"
ans = ""


N, K = map(int, input().split())		# 5 7
a = sorted(list(map(int, input().split())))	# 1 2 3 4 5 ...
#p(a)

if max(a)<K:
	print(IM)
	sys.exit()

if K in a:
	print(PO)
	sys.exit()

g=a[0]
for i in range(N):
	g = gcd(g,a[i])

if K%g==0:
	ans=PO
else:
	ans=IM


print(ans)

Buggy submission (verdict: Runtime Error):
from fractions import gcd

PO = "POSSIBLE"
IM = "IMPOSSIBLE"
ans = ""


N, K = map(int, input().split())		# 5 7
a = sorted(list(map(int, input().split())))	# 1 2 3 4 5 ...
#p(a)

if max(a)<K:
	print(IM)
	sys.exit()

if K in a:
	print(PO)
	sys.exit()

g=a[0]
for i in range(1, N):
	g = gcd(g,a[i])
	if K%g==0:
		ans=PO
		break
else:
	ans=IM


print(ans)

Buggy submission (verdict: Runtime Error):
from fractions import gcd

PO = "POSSIBLE"
IM = "IMPOSSIBLE"
ans = ""


N, K = map(int, input().split())		# 5 7
a = sorted(list(map(int, input().split())))	# 1 2 3 4 5 ...
#p(a)

if max(a)<K:
	print(IM)
	sys.exit()

if K in a:
	print(PO)
	sys.exit()

g=a[0]
for i in range(N):
	g = gcd(g,a[i])
	if K%g==0:
		ans=PO
		break
else:
	ans=IM


print(ans)
Problem p02573 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

N,Q=map(int,input().split())
par=[i for i in range(N+1)]
def find(x):
    if par[x] ==x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
def same(x,y):
    return find(x) == find(y)
def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return 0
    par[x]=y
for i in range(Q):
    a,b=map(int,input().split())
    unite(a,b)
ans=dict()
for i in par:
    tmp=find(i)
    if tmp in ans:
        ans[tmp]+=1
    else:
        ans[tmp]=1
print(max(ans.values()))

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
par=[i for i in range(N+1)]
def find(x):
    if par[x] ==x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
def same(x,y):
    return find(x) == find(y)
def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return 0
    par[x]=y
for i in range(Q):
    a,b=map(int,input().split())
    unite(a,b)
ans=dict()
for i in par:
    tmp=find(i)
    if tmp in ans:
        ans[tmp]+=1
    else:
        ans[tmp]=1
print(max(ans.values()))
Problem p02630 (python)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[list(map(int,input().split()))for i in range(Q)]
dic_A=dict()
sum_A=sum(A)
ans=list()
tmp=0
for i in A:
    if i in dic_A:
        dic_A[i]+=1
    else:
        dic_A[i]=1
for j in range(Q):
    if B[j][0] in dic_A:
        tmp=sum_A+ dic_A[B[j][0]]*(B[j][1]-B[j][0])
        ans.append(tmp)
        sum_A=tmp
        if B[j][1] not in dic_A:
            dic_A[B[j][1]]=dic_A[B[j][0]]
        else:
            dic_A[B[j][1]]+=dic_A[B[j][0]]
        dic_A[B[j][0]]=0
    else:
        if tmp==0:
            ans.append(sum_A)
        else:
            ans.append(tmp)
for k in ans:
    print(k)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[list(map(int,input().split()))for i in range(Q)]
dic_A=dict()
sum_A=sum(A)
ans=list()
for i in A:
    if i in dic_A:
        dic_A[i]+=1
    else:
        dic_A[i]=1
for j in range(Q):
    if B[j][0] in dic_A:
        tmp=sum_A+ dic_A[B[j][0]]*(B[j][1]-B[j][0])
        ans.append(tmp)
        sum_A=tmp
        if B[j][1] not in dic_A:
            dic_A[B[j][1]]=dic_A[B[j][0]]
        else:
            dic_A[B[j][1]]+=dic_A[B[j][0]]
        dic_A[B[j][0]]=0
    else:
        ans.append(tmp)
for k in ans:
    print(k)
Problem p02696 (python)

Fixed submission:
import math
A,B,N=map(float,input().split())
if N >= B:
    N = B-1
ans = math.floor(A*N/B) - A*math.floor(N/B)
print(int(ans))

Buggy submission (verdict: Runtime Error):
import math
A,B,N=map(float,input().split())
if N = B:
    N = N-1
ans = math.floor((A*N/B)) - A*math.floor(N/B)
print(int(ans))

Buggy submission (verdict: Runtime Error):
A,B,N=map(int,input().split())
if B==1 or A==1:
    ans = 0
elif A==10**6 and B ==10**12
    ans = 10**6
else:
    while N%B==0:
        N = N-1
    ans = (A*N)//B - A*(N//B)
print(ans)
Problem p02748 (python)

Fixed submission:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
xyc=[list(map(int,input().split()))for i in range(M)]
ans=min(a)+min(b)
for i in range(M):
    tmp = a[xyc[i][0]-1]+b[xyc[i][1]-1]-xyc[i][2]
    if ans > tmp:
        ans=tmp
print(ans)

Buggy submission (verdict: Runtime Error):
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(M):
    x,y,c=map(int,input().split())
    tmp = a[x-1]+b[x-1]-c
    if ans==0:
        ans=tmp
    if min(a)+min(b) < tmp:
        ans=min(a)+min(b)
    else:
        ans=tmp
print(ans)
Problem p03076 (python)

Fixed submission:
def last_num(a):
    while a//10!=0:
        a=a%10
    return a

def marume(a):
    b=a
    while a//10!=0:
        a=a%10
    if a!=0:
        b=b-a+10
    return b

A=[int(input())for _ in range(5)]
last_order=9
order_num=0
ans=0
for i in range(5):
    if 0 < last_num(A[i]) < last_order:
        last_order=last_num(A[i])
        order_num=i
for j in range(5):
    if j !=order_num:
        ans+=marume(A[j])
    else:
        ans+=A[j]
print(ans)

Buggy submission (verdict: Runtime Error):
def last_num(a):
    while a//10!=0:
        a=a%10
    return a

def marume(a):
    b=a
    while a//10!=0:
        a=a%10
    if a!=0:
        b=b-a+10
    return b

A=[int(input())for _ in range(5)]
last_order=9
ans=0
for i in range(5):
    if 0 < last_num(A[i]) < last_order:
        last_order=last_num(A[i])
        order_num=i
for j in range(5):
    if j !=order_num:
        ans+=marume(A[j])
    else:
        ans+=A[j]
print(ans)
Problem p03103 (python)

Fixed submission:
N,M=map(int,input().split())
A=dict()
ans=0
A=[list(map(int,input().split()))for i in range(N)]
A.sort()
for i in range(N):
    if M>A[i][1]:
        M-=A[i][1]
        ans+=A[i][0]*A[i][1]
    else:
        ans+=A[i][0]*M
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=dict()
ans=0
for i in range(N):
    a,b=map(int,input().split())
    A[a]=b
A=list(A.items())
A.sort()
for i in range(N):
    if M>A[i][1]:
        M-=A[i][1]
        ans+=A[i][0]*A[i][1]
    else:
        ans+=A[i][0]*M
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=dict()
ans=0
for i in range(N):
    a,b=map(int,input().split())
    A[a]=b
A=list(A.items())
A.sort()
for i in range(N):
    if M>=A[i][1]:
        M-=A[i][1]
        ans+=A[i][0]*A[i][1]
    else:
        ans+=A[i][0]*M
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=dict()
ans=0
for i in range(N):
    a,b=map(int,input().split())
    A[a]=b
A=list(A.items())
A.sort()
for i in range(N):
    if M>=A[i][0]:
        M-=A[i][1]
        ans+=A[i][0]*A[i][1]
    else:
        ans+=A[i][0]*M
        break
print(ans)
Problem p03261 (python)

Fixed submission:
N=int(input())
B=list()
B.append(input())
ans='Yes'
for i in range(N-1):
    tmp=input()
    if tmp in B:
        ans='No'
    if tmp[0]!=B[i][-1]:
        ans='No'
    B.append(tmp)
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
B=list()
B.append(input())
for i in range(N-1):
	tmp=input()
	if tmp in B:
		print('No')
		exit()
	if tmp[0]!=B[i-1][-1]:
		print('No')
		exit()
print('Yes')
Problem p03370 (python)

Fixed submission:
N,X = map(int,input().split())
m=[]
for i in range(N):
	m.append(int(input()))
print(N + (X-sum(m))//min(m))

Buggy submission (verdict: Runtime Error):
1 N, X = map(int, input().split())
2 m = []
3 for i in range(N):
4 m.append(int(input()))
5 print(N + (X - sum(m)) // min(m))
Problem p02642 (python)

Fixed submission:
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    A = LI()

    cnt = collections.Counter(A)
    A = list(set(A))
    A.sort()
    A_max = A[-1]
    divisible = [False] * (A_max + 1)

    for i in A:
        for j in range(2 * i, A_max+1, i):
            divisible[j] = True

    ans = 0
    for i in A:
        tmp = 0
        if not divisible[i] and cnt[i] == 1:
            tmp += 1
        ans += tmp

        
    print(ans)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
find / -name pip

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A.sort()
    divisible = set(list(range(1, 10**6+1)))

    for i in A:
        j = 1
        while i*j <= 10**6:
            if i*j in divisible:
		        divisible.remove(i*j)
                j += 1

    ans = 0
    for i in A:
        if j in divisible:
            ans += 1
        
    print(ans)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A.sort()
    divisible = set(list(range(1, 10**6+1)))

    for i in A:
        j = 1
        while i*j <= 10**6:
            if i*j in divisible:
		        divisible.remove(i*j)
                j += 1

    ans = 0
    for i in A:
        if j in divisible:
            ans += 1
        
    print(ans)

if __name__ == '__main__':
    resolve()
Problem p02684 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    A = LI_()

    ord = [-1]*N

    city = 0
    cnt = 0
    teleport_path = []
    while ord[city]==-1:
        ord[city] = cnt
        teleport_path.append(city)
        city = A[city]
        cnt += 1
    loop_head = ord[city]
    loop_length = len(teleport_path) - loop_head
    # print(teleport_path, loop_head, loop_length)

    if K-1<loop_head:
        print(teleport_path[K]+1)
    else:
        K = loop_head + (K-loop_head)%loop_length
        print(teleport_path[K]+1)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    A = LI_()

    ord = [-1]*N

    city = 0
    cnt = 0
    teleport_path = []
    while ord[city]==-1:
        ord[city] = cnt
        teleport_path.append(city)
        city = A[city]
        cnt += 1
    loop_head = ord[city]
    loop_length = len(teleport_path) - loop_head
    # print(teleport_path, loop_head, loop_length)

    if K-1<loop_head:
        print(teleport_path[K]+1)
    else:
        K = loop_head + (K-loop_head)%loop_length
        print(teleport_path[K]+1)

if __name__ == '__main__':
    r
Problem p02706 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))

    s = sum(A)
    if s<=N:
        print(N-s)
    else:
        print(-1)

if __name__ == '__main__':
    resolve()

Buggy submission (verdict: Runtime Error):
from b import resolve
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """41 2
5 6"""
        output = """30"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """10 2
5 6"""
        output = """-1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """11 2
5 6"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3"""
        output = """9"""
        self.assertIO(input, output)
    def test_Sample_Input_1(self):
        input = """41 2
5 6"""
        output = """30"""
        self.assertIO(input, output)
    def test_Sample_Input_2(self):
        input = """10 2
5 6"""
        output = """-1"""
        self.assertIO(input, output)
    def test_Sample_Input_3(self):
        input = """11 2
5 6"""
        output = """0"""
        self.assertIO(input, output)
    def test_Sample_Input_4(self):
        input = """314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3"""
        output = """9"""
        self.assertIO(input, output)

if __name__ == "__main__":
    unittest.main()
Problem p02768 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

def combMod(n, r, p):
    numer = 1
    denom = 1
    for i in range(1, r+1):
        numer = numer * (n-r+i) % p
        denom = denom * i % p
    return numer * pow(denom, p-2, p) % p

def resolve():
    n, a, b = map(int, input().split())
    mod = 10**9+7
    print((pow(2, n, mod) - 1 - combMod(n, a, mod) - combMod(n, b, mod)) % mod)

if __name__ == '__main__':
    resolve()

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

resource.setrlimit(resource.RLIMIT_DATA, (-1, -1))

def combMod(n, r, p):
    fact = [0] * (n+1)
    fact[0] = fact[1] = 1
    factinv = [0] * (n+1)
    factinv[0] = factinv[1] = 1
    inv = [0] * (n+1)
    inv[1] = 1
    for i in range(2, n+1):
        fact[i] = (fact[i-1] * i) % p
        inv[i] = (-inv[p % i] * (p // i)) % p
        factinv[i] = (factinv[i-1] * inv[i]) % p
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    # print(fact)
    # print(factinv)
    return fact[n] * factinv[r] * factinv[n-r] % p

def resolve():
    n, a, b = map(int, input().split())
    mod = 10**9+7
    print((pow(2, n, mod) - 1) - (combMod(n, a, mod) + combMod(n, b, mod)))
    # print(combMod(n, a, mod))
    # print(combMod(n, b, mod))

if __name__ == '__main__':
    resolve()
Problem p02787 (python)

Fixed submission:
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]

# インデックスはライフ 値はそれを以下にする最小MP
dp = np.array([10**10 for i in range(H+1)], dtype=int)
# dp = np.array([np.inf for i in range(H+1)], dtype=int)
dp[0] = 0

for i in range(1, H+1):
    # print(np.maximum(i-A, 0))
    dp[i] = min(dp[np.maximum(i-A, 0)] + B)

print(dp[-1])

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

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]

# インデックスはライフ 値はそれを以下にする最小MP
dp = [np.inf for i in range(H+1)]
dp[0] = 0

for i in range(1, H+1):
    quietus_MP = np.inf
    for a, b in AB:
        # if i-a >= 0:
        dp[i] = min(dp[max(i-a, 0)] + b, dp[i])
        # 一回で倒せて一番MP使わない魔法
        # if a > i:
        #     quietus_MP = min(b, quietus_MP)
    # 一回で倒す場合
    # dp[i] = min(quietus_MP, dp[i])

print(dp[-1])

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

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]

# インデックスはライフ 値はそれを以下にする最小MP
dp = [np.inf for i in range(H+1)]
dp[0] = 0

for i in range(1, H+1):
    quietus_MP = np.inf
    for j in AB:
        if i-j[0] >= 0:
            dp[i] = min(dp[i-j[0]]+j[1], dp[i])
        # 一回で倒せて一番MP使わない魔法
        if j[0] > i:
            quietus_MP = min(j[1], quietus_MP)
    # 一回で倒す場合
    dp[i] = min(quietus_MP, dp[i])

print(dp[-1])
Problem p02803 (python)

Fixed submission:
from collections import deque
import itertools
import copy

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

def solve(s):
    M = copy.deepcopy(S)
    d = ((1, 0), (-1, 0), (0, -1), (0, 1))
    queue = deque([s])
    steps = [[-1 for _ in range(W)] for _ in range(H)]
    steps[s[0]][s[1]] = 0

    while len(queue) > 0:
        current = queue.popleft()
        cy = current[0]
        cx = current[1]
        # M[cy][cx] = '#'
        for i in d:
            ny = cy+i[0]
            nx = cx+i[1]
            # はみ出さないかつ通ってない
            if (0<=ny<H and 0<=nx<W) and M[ny][nx]=='.' and steps[ny][nx] < 0:
            # if (0<=ny<H and 0<=nx<W) and M[ny][nx]!='#':
                queue.append([ny, nx])
                steps[ny][nx] = steps[cy][cx] + 1
        
    return max([max(i) for i in steps])

maxStep = 0
for s in itertools.product(range(H), range(W)):
    if S[s[0]][s[1]] == '.':
        maxStep = max(solve([s[0], s[1]]), maxStep)

print(maxStep)

Buggy submission (verdict: Runtime Error):
from collections import deque
import itertools
import copy
import numpy as np

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

def solve(S, s, g):
    M = copy.deepcopy(S)
    d = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    queue = deque([s])
    steps = [[np.max for _ in range(W)] for _ in range(H)]
    steps[s[0]][s[1]] = 0

    while len(queue) > 0:
        current = queue.popleft()
        cy = current[0]
        cx = current[1]
        # if cy == g[0] and cx == g[1]:
        #     return steps[cy][cx]
        # else:
        M[cy][cx] = '#'
        for i in d:
            next = [cy+i[0], cx+i[1]]
            # はみ出さないかつ通ってない
            if (0<=next[0]<H and 0<=next[1]<W) and M[next[0]][next[1]]!='#':
                queue.append(next)
                steps[next[0]][next[1]] = steps[cy][cx] + 1
        
    return max([max(i) for i in steps])
# print(solve(S, [0, 0], [0, 1]))
# print(solve(S, [2, 0], [0, 4]))

# print(S)
maxStep = 0
for s in itertools.product(range(H), range(W)):
    for g in itertools.product(range(H), range(W)):
        if S[s[0]][s[1]] == '.' and S[g[0]][g[1]] == '.':
            # print(s, g)
            # print(solve(S, [s[0], s[1]], [g[0], g[1]]))
            maxStep = max(solve(S, [s[0], s[1]], [g[0], g[1]]), maxStep)

print(maxStep)