Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
[[0]*W for _ in range(H)]
dist = 10**9
def dfs(x, y,visit, n):
    if x < 0 or x >= H or y < 0 or y >= W or g[x][y] == '#':
        return 0
    elif x == H-1and y == W-1:
        global dist
        if n < dist:
            dist = n
        return 0
    elif visit[x][y]:
        return 0
    else:
        visit[x][y] = 1
        dfs(x+1, y, n+1)
        dfs(x, y+1, n+1)
        dfs(x-1, y, n+1)
        dfs(x, y-1, n+1)
dfs(0, 0,[[0]*W for _ in range(H)] ,1)
if dist == 10**9:
    print(-1)
else:
    print(white - dist)

Fixed submission:
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
start = (0, 0)
goal = (H-1, W-1)
S = [start]
T = {start: 1}
while S and goal not in T:
    S2 = []
    for x, y in S:
        for pos in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
            x2, y2 = pos
            if 0<= x2 <= H-1 and 0<= y2 <= W-1:
                if g[x2][y2] != '#' and pos not in T:
                    T[pos] = T[(x, y)] + 1
                    S2.append(pos)
    S = S2
if goal not in T:
    print(-1)
else:
    print(white - T[goal])

Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
visit =[[0]*W for _ in range(H)]
dist = 10**9
def dfs(x, y, n):
    if x < 0 or x >= H or y < 0 or y >= W or g[x][y] == '#':
        return 0
    elif x == H-1and y == W-1:
        visit[x][y] = 1
        global dist
        if n < dist:
            dist = n
    elif visit[x][y]:
        return 0
    else:
        visit[x][y] = 1
        dfs(x+1, y, n+1)
        dfs(x-1, y, n+1)
        dfs(x, y+1, n+1)
        dfs(x, y-1, n+1)
dfs(0, 0, 1)
if not visit[H-1][W-1]:
    print(-1)
else:
    print(white - dist)

Fixed submission:
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
start = (0, 0)
goal = (H-1, W-1)
S = [start]
T = {start: 1}
while S and goal not in T:
    S2 = []
    for x, y in S:
        for pos in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
            x2, y2 = pos
            if 0<= x2 <= H-1 and 0<= y2 <= W-1:
                if g[x2][y2] != '#' and pos not in T:
                    T[pos] = T[(x, y)] + 1
                    S2.append(pos)
    S = S2
if goal not in T:
    print(-1)
else:
    print(white - T[goal])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
N, M =map(int, input().split())
G=[[]for i in range(N+1)]
for i in range(M):
    a, b, c = map(int,input().split())
    G[a] += (b,c),
    G[b] += (a,-c),

d = [10**9]*(N+1)
flag = True
def f(v, u):
    global flag
    if not flag:
        d[v] = 0
        return 0
    else:
        d[v] = u
        for t, c in G[v] :
            if d[t] == 10**9:
                f(t, u+c)
            else:
                if d[t] != u+c:
                    flag = False
                    
for i in range(1, N+1):
    if d[i] == 10**9:
        f(i, 0)
    else:
        f(i, d[i])
if flag:
    print('Yes')
else:
    print('No')

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
N, M =map(int, input().split())
G=[[]for i in range(N+1)]
for i in range(M):
    a, b, c = map(int,input().split())
    G[a] += (b,c),
    G[b] += (a,-c),

d = [10**9]*(N+1)
flag = True
def f(v, u):
    global flag
    if not flag:
        d[v] = 0
        return 0
    else:
        d[v] = u
        for t, c in G[v] :
            if d[t] == 10**9:
                f(t, u+c)
            else:
                if d[t] != u+c:
                    flag = False
                    
for i in range(1, N+1):
    if d[i] == 10**9:
        f(i, 0)
if flag:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
n, y = map(int, input().split())
ans = []
for i in range(n+1):
    for j in range(n+1-i):
        k = n-i-j
        if 10000*i + 5000*j + 1000*k == y and k >=0:
            ans.append([i,j,k])
            break
                
if len(ans) >=1:
    str_ans = map(str, ans[0])
    print(' '.join(str_ans))
if len(ans) == 0:
    print('-1 -1 -1')

Fixed submission:
S = input()
print('2018'+S[4:])
Buggy python submission (verdict: Time Limit Exceeded):
n, y = map(int, input().split())
ans = []
for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            if 10000*i + 5000*j + 1000*k == y and i+j+k ==n:
                ans.append([i,j,k])
                break
                
if len(ans) >=1:
    str_ans = map(str, ans[0])
    print(' '.join(str_ans))
if len(ans) == 0:
    print('-1 -1 -1')

Fixed submission:
n, y = map(int, input().split())
ans = []
for i in range(n+1):
    for j in range(n+1-i):
        k = n-i-j
        if 10000*i + 5000*j + 1000*k == y and k >=0:
            ans.append([i,j,k])
            break
                
if len(ans) >=1:
    str_ans = map(str, ans[0])
    print(' '.join(str_ans))
if len(ans) == 0:
    print('-1 -1 -1')
Buggy python submission (verdict: Runtime Error):
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    if i != t:
        for s in S[-i+t:]:
            if s == '?':
                ans += 'a'
            else:
                ans += s
    print(ans)
else:
    print('UNRESTORABLE')

Fixed submission:
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
if t > n:
    flag = False
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    if i != t:
        for s in S[-i+t:]:
            if s == '?':
                ans += 'a'
            else:
                ans += s
    print(ans)
else:
    print('UNRESTORABLE')

Buggy python submission (verdict: Runtime Error):
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    print(ans)
else:
    print('UNRESTORABLE')
         

Fixed submission:
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
if t > n:
    flag = False
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    if i != t:
        for s in S[-i+t:]:
            if s == '?':
                ans += 'a'
            else:
                ans += s
    print(ans)
else:
    print('UNRESTORABLE')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As =[list(map(int, input().split())) for _ in range(N)]
flag1 = False
ans = 0
for i in range(N):
    for j in range(i+1, N):
        flag2 = True
        for k in range(N):
            if k == i or k == j:
                continue
            if As[i][k]+As[k][j] < As[i][j]:
                flag1 = True
                break
            elif As[i][k]+As[k][j] == As[i][j]:
                flag2 = False
        if flag2:
            ans += As[i][j]
        if flag1:
            break
    if flag1:
        break
if not flag1:
    print(ans)
else:
    print(-1)

Fixed submission:
N = int(input())
As =[list(map(int, input().split())) for _ in range(N)]
flag1 = False
ans = 0
for i in range(N):
    for j in range(i+1, N):
        flag2 = True
        for k in range(N):
            if k == i or k == j:
                continue
            if As[i][k]+As[k][j] < As[i][j]:
                flag1 = True
                break
            elif As[i][k]+As[k][j] == As[i][j]:
                flag2 = False
        if flag2:
            ans += As[i][j]
        if flag1:
            break
    if flag1:
        break
if not flag1:
    print(ans)
else:
    print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(1, N+1):
        for i in range(1, N+1):
            for j in range(1, N+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

N, M, R = map(int, input().split()) 
rs = list(map(int, input().split()))

inf = 10**9
d = [[inf for i in range(N+1)] for i in range(N+1)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(M):
    a, b, c = map(int, input().split())
    d[a][b] = c
    d[b][a] = c
for i in range(1, N+1):
    d[i][i] = 0 #自身のところに行くコストは０
warshall_floyd(d)
ans = inf
for v in itertools.permutations(rs, R):
    dist = 0
    for i in range(R-1):
        dist += d[v[i]][v[i+1]]
    if dist <= ans:
        ans = dist
print(ans)

Fixed submission:
import itertools
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(1, N+1):
        for i in range(1, N+1):
            for j in range(1, N+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

N, M, R = map(int, input().split()) 
rs = list(map(int, input().split()))

inf = 10**9
d = [[inf for i in range(N+1)] for i in range(N+1)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(M):
    a, b, c = map(int, input().split())
    d[a][b] = c
    d[b][a] = c
for i in range(1, N+1):
    d[i][i] = 0 #自身のところに行くコストは０
warshall_floyd(d)
ans = inf
for v in itertools.permutations(rs, R):
    dist = 0
    for i in range(R-1):
        dist += d[v[i]][v[i+1]]
    if dist <= ans:
        ans = dist
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
ps = list(map(int, input().split()))
ans = 0
for i in range(N):
    if ps[i] == i+1:
        ps[i] = ps[i+1]
        ps[i+1] = i+1
        ans += 1
print(ans)

Fixed submission:
N = int(input())
ps = list(map(int, input().split()))
state = 0
ans = 0
for i in range(N):
    if state:
        state = 0
        continue
    if ps[i] == i+1:
        ans += 1
        state = 1
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**5)
n=int(input())
G=[[]for i in range(n+1)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    G[a] += (b,c),
    G[b] += (a,c),
q, k = map(int,input().split())
d = [0]*(n+1) #kから各点への距離
def f(v,p,u): #v:今見ているとこ p:一つ前に見たとこ u:kからの距離
    d[v] = u
    for t, c in G[v]:
        if p != t:
            f(t,v,u+c)
f(k,0,0)
for i in range(q):
    x, y = map(int,input().split())
    print(d[x]+d[y])

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
G=[[]for i in range(n+1)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    G[a] += (b,c),
    G[b] += (a,c),
q, k = map(int,input().split())
d = [0]*(n+1) #kから各点への距離
def f(v,p,u): #v:今見ているとこ p:一つ前に見たとこ u:kからの距離
    d[v] = u
    for t, c in G[v]:
        if p != t:
            f(t,v,u+c)
f(k,0,0)
for i in range(q):
    x, y = map(int,input().split())
    print(d[x]+d[y])

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
n=int(input())
G=[[]for i in range(n+1)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    G[a] += (b,c)
    G[b] += (a,c)
q, k = map(int,input().split())
d = [0]*(n+1) #kから各点への距離
def f(v,p,u): #v:今見ているとこ p:一つ前に見たとこ u:kからの距離
    d[v] = u
    for t, c in G[v]:
        if p != t:
            f(t,v,u+c)
f(k,0,0)
for i in range(q):
    x, y = map(int,input().split())
    print(d[x]+d[y])

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
G=[[]for i in range(n+1)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    G[a] += (b,c),
    G[b] += (a,c),
q, k = map(int,input().split())
d = [0]*(n+1) #kから各点への距離
def f(v,p,u): #v:今見ているとこ p:一つ前に見たとこ u:kからの距離
    d[v] = u
    for t, c in G[v]:
        if p != t:
            f(t,v,u+c)
f(k,0,0)
for i in range(q):
    x, y = map(int,input().split())
    print(d[x]+d[y])
Buggy python submission (verdict: Time Limit Exceeded):
X = int(input())
for i in range(10**5):
    s = sum(range(i))
    if s < X <= s + i:
        break
print(i)

Fixed submission:
X = int(input())
s = 0
for i in range(10**5):
    if s < X <= s + i:
        break
    s += i
print(i)
Buggy python submission (verdict: Runtime Error):
N , M = map(int, input().split())
As =[input() for _ in range(N)]
Bs =[input() for _ in range(M)]
m = N-M
for i in range(m):
    for j in range(m):
        flag = True
        for k in range(M):
            for l in range(M):
                if As[k+i][l+j] != Bs[k][l]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            break
    if flag:
        break
if flag:
    print('Yes')
else:
    print('No')
            

Fixed submission:
N , M = map(int, input().split())
As =[input() for _ in range(N)]
Bs =[input() for _ in range(M)]
m = N-M
for i in range(m+1):
    for j in range(m+1):
        flag = True
        for k in range(M):
            for l in range(M):
                if As[k+i][l+j] != Bs[k][l]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            break
    if flag:
        break
if flag:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
N , M = map(int, input().split())
As =[input() for _ in range(N)]
Bs =[input() for _ in range(M)]
m = N-M
for i in range(m):
    for j in range(m):
        flag = True
        for k in range(i, M+i):
            for l in range(j, M+j):
                if As[k][l] != Bs[k-i][l-j]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            break
    if flag:
        break
if flag:
    print('Yes')
else:
    print('No')
            
        

Fixed submission:
N , M = map(int, input().split())
As =[input() for _ in range(N)]
Bs =[input() for _ in range(M)]
m = N-M
for i in range(m+1):
    for j in range(m+1):
        flag = True
        for k in range(M):
            for l in range(M):
                if As[k+i][l+j] != Bs[k][l]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            break
    if flag:
        break
if flag:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
ans = []
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        h = s- i- j
        if 0<= h <= k:
            ans.append([i, j, h])
print(len(ans))

Fixed submission:
k, s = map(int, input().split())
cnt = 0
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        h = s- i- j
        if 0<= h <= k:
            cnt += 1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
ans = []
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        for h in range(min(k+1,s-i-j+1)):
            if i + j + h == s:
                ans.append([i, j, h])
                break
print(len(ans))

Fixed submission:
k, s = map(int, input().split())
cnt = 0
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        h = s- i- j
        if 0<= h <= k:
            cnt += 1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
ans = []
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        for h in range(min(k+1,s-i-j+1)):
            if i + j + h == s:
                ans.append([i, j, h])
                
print(len(ans))

Fixed submission:
k, s = map(int, input().split())
cnt = 0
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        h = s- i- j
        if 0<= h <= k:
            cnt += 1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
ans = []
for i in range(k+1):
    for j in range(k+1):
        for h in range(k+1):
            if i + j + h == s:
                ans.append([i, j, h])
                break
print(len(ans))
                

Fixed submission:
k, s = map(int, input().split())
cnt = 0
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        h = s- i- j
        if 0<= h <= k:
            cnt += 1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
ans = []
for i in range(k+1):
    for j in range(k+1):
        for h in range(k+1):
            if i + j + h == s:
                ans.append([i, j, h])
print(len(ans))
                

Fixed submission:
k, s = map(int, input().split())
cnt = 0
for i in range(k+1):
    for j in range(min(k+1,s-i+1)):
        h = s- i- j
        if 0<= h <= k:
            cnt += 1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
X = input()
while 'ST' in X:
    X.replace('ST', '')
print(len(X))

Fixed submission:
X = input()
t = 0
s = 0
for i in X:
    if i == 'T':
        if s > 0:
            s -= 1
        else:
            t += 1
    if i == 'S':
        s += 1
print(s + t)
Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())
S=[input() for i in range(H)]
A=[[0]*W for i in range(H)]
B=[[0]*W for i in range(H)]
for i in range(H):
    s=0
    t=0
    num=0
    while t<W:
        #print(s,t,num)
        if S[i][t]=='#':
            for k in range(s,t):
                A[i][k]=num
            num=0
            t+=1
            s=t
        else:
            t+=1
            num+=1
    for k in range(s,t):
        A[i][k] = num
for j in range(W):
    s=0
    t=0
    num=0
    while t<H:
        #print(s,t,num)
        if S[t][j]=='#':
            for k in range(s,t):
                B[k][j]=num
            num=0
            t+=1
            s=t
        else:
            t += 1
            num += 1
    for k in range(s,t):
        B[k][j] = num
#print(A)
#print(B)
ans=0
for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            ans=max(ans,A[i][j]+B[i][j]-1)
print(ans)



Fixed submission:
H,W=map(int,input().split())
S=[input() for i in range(H)]
A=[[0]*W for i in range(H)]
B=[[0]*W for i in range(H)]
for i in range(H):
    s=0
    t=0
    num=0
    while t<W:
        #print(s,t,num)
        if S[i][t]=='#':
            for k in range(s,t):
                A[i][k]=num
            num=0
            t+=1
            s=t
        else:
            t+=1
            num+=1
    for k in range(s,t):
        A[i][k] = num
for j in range(W):
    s=0
    t=0
    num=0
    while t<H:
        #print(s,t,num)
        if S[t][j]=='#':
            for k in range(s,t):
                B[k][j]=num
            num=0
            t+=1
            s=t
        else:
            t += 1
            num += 1
    for k in range(s,t):
        B[k][j] = num
#print(A)
#print(B)
ans=0
for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            ans=max(ans,A[i][j]+B[i][j]-1)
print(ans)
Buggy python submission (verdict: Runtime Error):
r,g,b,n=map(int,input().split())
num=0
fot i in range(g+1):
  for j in range (b+1):
    if i+j>N:
      break
    if 0<=N-i-j<=r:
      num+=1
print(num)


Fixed submission:
r,g,b,N=map(int,input().split())
num=0
for i in range(N+1):
  for j in range (N+1):
    s=(N-i*r-j*g)
    if s>=0 and s%b==0:
      num+=1
print(num)
Buggy python submission (verdict: Time Limit Exceeded):
N,A,B=map(int,input().split())
P=[int(i) for i in input().split()]
dp=[[10**19]*(N+1) for i in range(N+1)]
dp[0][0]=0
for i in range(1,N+1):
    for j in range(N+1):
        #if dp[i-1][j]==10**19:
            #continue
        if j < P[i-1]:
            dp[i][P[i-1]]=min(dp[i][P[i-1]],dp[i-1][j])
            dp[i][j]=min(dp[i][j],dp[i-1][j]+A)
        else:
            dp[i][j]=min(dp[i][j],dp[i-1][j]+B)
#print(dp)
print(min(dp[-1]))

Fixed submission:
N,A,B=map(int,input().split())
P=[int(i) for i in input().split()]
dp=[10**19]*(N+1)
dp[0]=0
for i in range(1,N+1):
    ndp=[10**19]*(N+1)
    for j in range(N+1):
        if j < P[i-1]:
            ndp[P[i-1]]=min(ndp[P[i-1]],dp[j])
            ndp[j]=min(ndp[j],dp[j]+A)
        else:
            ndp[j]=min(ndp[j],dp[j]+B)
    dp=ndp
print(min(dp))
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)v

Fixed submission:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)
Buggy python submission (verdict: Runtime Error):
import bisect
import sys
from collections import defaultdict
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
for b in B:
    dd[b]+=1
for d,n in dd.items():
    if n>=3:
        print(0)
        sys.exit()
A=sorted(A)
B=sorted(B)
ans=1
mod=10**9+7
for i in range(N*M,0,-1):
    x=bisect.bisect_left(A,i)
    y=bisect.bisect_left(B,i)
    if A[x]==i and B[y]==i:
        #print(i)
        continue
    elif A[x]==i:
        ans*=(M-y)
        ans%=mod
    elif B[y]==i:
        ans*=(N-x)
        ans%=mod
    else:
        ans*=((N-x)*(M-y)-(N*M-i))
        ans%=mod
    #print(x,y,i,ans)
print(ans)

Fixed submission:
import bisect
import sys
from collections import defaultdict
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
for b in B:
    dd[b]+=1
for d,n in dd.items():
    if n>=3:
        print(0)
        sys.exit()
A=sorted(A)
B=sorted(B)
ans=1
mod=10**9+7
for i in range(N*M,0,-1):
    x=bisect.bisect_left(A,i)
    y=bisect.bisect_left(B,i)
    if x>=N or y>=M:
        print(0)
        sys.exit()
    if A[x]==i and B[y]==i:
        #print(i)
        continue
    elif A[x]==i:
        ans*=(M-y)
        ans%=mod
    elif B[y]==i:
        ans*=(N-x)
        ans%=mod
    else:
        ans*=((N-x)*(M-y)-(N*M-i))
        ans%=mod
    #print(x,y,i,ans)
print(ans)