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

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

if k%2 ==1:
  print(int(n//k)**3)
else:
  print(int((n//k)**3 + ((n+(k/2))//k)**3))

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())

if k%2 ==1:
  print(int(n//k)**3)
else:
  print(int((n//k)**3 + ((n+(k/2))//k)**3))
Problem p03157 (python)

Fixed submission:
import sys
sys.setrecursionlimit(500*500)

h, w = map(int, input().split())
grid = []
seen = [[False for y in range(w)] for x in range(h)]

for x in range(h):
    grid.append(input())



blackSeen = 0
whiteSeen = 0
def countFrom(a, b, lastColor):
    global blackSeen, whiteSeen
    if not (0 <= a < h and 0 <= b < w):
        return
    if seen[a][b]:
        return
    #print(a, b)
    

    thisColor = (1 if grid[a][b] == "." else -1)

    if grid[a][b] == "#" and lastColor != -1:
        blackSeen += 1
    elif grid[a][b] == "." and lastColor != 1:
        whiteSeen += 1
    else:
        return
    seen[a][b] = True

    countFrom(a+1, b, thisColor)
    countFrom(a-1, b, thisColor)
    countFrom(a, b+1, thisColor)
    countFrom(a, b-1, thisColor)

tot = 0
for x in range(h):
    for y in range(w):
        blackSeen = 0
        whiteSeen = 0
        countFrom(x, y, 0)
        #print(x, y, blackSeen, whiteSeen)
        tot += blackSeen * whiteSeen

print(tot)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
grid = []
seen = [[False for y in range(w)] for x in range(h)]

for x in range(h):
    grid.append(input())



blackSeen = 0
whiteSeen = 0
def countFrom(a, b, lastColor):
    global blackSeen, whiteSeen
    if not (0 <= a < h and 0 <= b < w):
        return
    if seen[a][b]:
        return
    #print(a, b)
    

    thisColor = (1 if grid[a][b] == "." else -1)

    if grid[a][b] == "#" and lastColor != -1:
        blackSeen += 1
    elif grid[a][b] == "." and lastColor != 1:
        whiteSeen += 1
    else:
        return
    seen[a][b] = True

    countFrom(a+1, b, thisColor)
    countFrom(a-1, b, thisColor)
    countFrom(a, b+1, thisColor)
    countFrom(a, b-1, thisColor)

tot = 0
for x in range(h):
    for y in range(w):
        blackSeen = 0
        whiteSeen = 0
        countFrom(x, y, 0)
        #print(x, y, blackSeen, whiteSeen)
        tot += blackSeen * whiteSeen

print(tot)
Problem p03285 (python)

Fixed submission:
def piyo():
    n = int(input())
    if n>=24:
        if ((n-7)%4==0 or (n-14)%4==0 or (n-21)%4==0 or n%4==0
        or (n-4)%7==0 or (n-8)%7==0 or (n-12)%7==0 or (n-16)%7==0 or (n-20)%7==0 or (n-24)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=21:
        if ((n-7)%4==0 or (n-14)%4==0 or (n-21)%4==0 or n%4==0
        or (n-4)%7==0 or (n-8)%7==0 or (n-12)%7==0 or (n-16)%7==0 or (n-20)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=20:
        if ((n-7)%4==0 or (n-14)%4==0 or n%4==0 or (n-4)%7==0 or (n-8)%7==0 or (n-12)%7==0 or (n-16)%7==0 or (n-20)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=16:
        if ((n-7)%4==0 or (n-14)%4==0 or n%4==0 or (n-4)%7==0 or (n-8)%7==0 or (n-12)%7==0 or (n-16)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=14:
        if ((n-7)%4==0 or (n-14)%4==0 or n%4==0 or (n-4)%7==0 or (n-8)%7==0 or (n-12)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=12:
        if ((n-7)%4==0 or n%4==0 or (n-4)%7==0 or (n-8)%7==0 or (n-12)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=8:
        if ((n-7)%4==0 or n%4==0 or (n-4)%7==0 or (n-8)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=7:
        if ((n-7)%4==0 or n%4==0 or (n-4)%7==0 or n%7==0):
            print("Yes")
            return
    if n>=4:
        if (n%4==0 or (n-4)%7==0 or n%7==0):
            print("Yes")
            return
    print("No")
        
if __name__ == '__main__':
    piyo()

Buggy submission (verdict: Runtime Error):
import sympy
def piyo():
    n = int(input())
    if (n-7)%4==0 or (n-14)%4==0 or (n-21)%4==0 or n%4==0:
        print("Yes")
        return
    print("No")
        
if __name__ == '__main__':
    piyo()
Problem p03284 (python)

Fixed submission:
numSenbei, numPeople = [int(i) for i in str(input()).split()]

if numSenbei % numPeople == 0:
    print(0)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
import sys

targetPrice = int(input())
cakePrice = 4
donutPrice = 7
maxDonutCount = int(targetPrice / 7)

for i in range(maxDonutCount + 1):
    targetCakePrice = targetPrice - (donutPrice * i)
    if targetCakePrice % cakePrice == 0:
        print("Yes")
        sys.exit()
print("No")

Buggy submission (verdict: Runtime Error):
(numSenbei, numPeople) = input().split()

if numSenbei % numPeople == 0:
    print(1)
else:
    print(0)
Problem p03160 (python)

Fixed submission:
n=int(input())
l=[int(x) for x in input().split()]
dp=[0]*n
for i in range(n):
    if i==0:
        dp[i]=0
    elif i==1:
        dp[i]=abs(l[i]-l[i-1])
    else:
	    dp[i]=min(dp[i-1]+abs(l[i]-l[i-1]),dp[i-2]+abs(l[i]-l[i-2]))
print(dp[n-1])

Buggy submission (verdict: Runtime Error):
def solve(n,l,dp):
    if n<=0:
        return 0
    elif n==1:
        return abs(l[n]-l[n-1])
    if dp[n]!=-1:
        return dp[n]
    dp[n]= min(abs(l[n]-l[n-1])+solve(n-1,l,dp),abs(l[n]-l[n-2])+solve(n-2,l,dp))
    return dp[n]
n=int(input())
l=[int(x) for x in input().split()]
if n==1:
    print(0)
else:
    dp=[-1]*(n+1)
    ans=solve(n-1,l,dp)
    print(ans)

Buggy submission (verdict: Runtime Error):
def solve(n,l,dp):
    if n<=0:
        return 0
    elif n==1:
        return abs(l[n]-l[n-1])
    if n in dp:
        return dp[n]
    dp[n]= min(abs(l[n]-l[n-1])+solve(n-1,l,dp),abs(l[n]-l[n-2])+solve(n-2,l,dp))
    return dp[n]
n=int(input())
l=[int(x) for x in input().split()]
if n==1:
    print(0)
else:
    ans=solve(n-1,l,{})
    print(ans)

Buggy submission (verdict: Runtime Error):
def solve(n,l,dp):
    if n==0:
        return l[0]
    elif n==1:
        return abs(l[n-1]-l[n-2])
    if n in dp:
        return dp[n]
    dp[n]= min((abs(l[n-1]-l[n-2])+solve(n-1,l,dp)),abs(l[n-1]-l[n-3])+solve(n-2,l,dp))
    return dp[n]
n=int(input())
l=[int(x) for x in input().split()]
if n==1:
    print(0)
else:
    ans=solve(n,l,{})
    print(ans)
Problem p03161 (python)

Fixed submission:
import sys
from functools import reduce
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
	if x>=n-2: return dp[x]
	if not dp[x] is -1: return dp[x]
	# t = [ for i in range(x+1,mn(n,x+k+1))]
	t = map(lambda i:ab(ar[x]-ar[i])+solve(n,k,ar,dp,i), range(x+1,mn(n,x+k+1)))
	dp[x] = reduce(lambda a,b : a if a < b else b,t)
	return dp[x]

def main():
	I = lambda:map(int,sys.stdin.readline().split())
	pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	pr(solve(n,k,ar,dp,0))

main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
    if x>=n-2: return dp[x]
	if dp[x] != -1: return dp[x]
	m = 1e9
	for i in range(x+1,mn(n,x+k+1)):
		t = ab(ar[x]-ar[i])+solve(n,k,ar,dp,i)
		m = t if t<m else m
	dp[x] = m
	# pr(f'{x} => {dp[x]}')
	return dp[x]

def main():
	I = lambda:map(int,sys.stdin.readline().split())
	pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	# pr((n,k,ar,dp))
	pr(solve(n,k,ar,dp,0))

main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
    if x>=n-2: return dp[x]
	if dp[x] != -1: return dp[x]
	m = 1e9
	for i in range(x+1,mn(n,x+k+1)):
		t = ab(ar[x]-ar[i])+solve(n,k,ar,dp,i)
		m = t if t<m else m
	dp[x] = m
	# pr(f'{x} => {dp[x]}')
	return dp[x]

def main():
	I = lambda:map(int,sys.stdin.readline().split())
	pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	# pr((n,k,ar,dp))
	pr(solve(n,k,ar,dp,0))

main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
    if x>=n-2: return dp[x]
	if dp[x] != -1: return dp[x]
	m = 1e9
	for i in range(x+1,mn(n,x+k+1)):
		t = ab(ar[x]-ar[i])+solve(n,k,ar,dp,i)
		m = t if t<m else m
	dp[x] = m
	# pr(f'{x} => {dp[x]}')
	return dp[x]

def main():
    I = lambda:map(int,sys.stdin.readline().split())
    pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	# pr((n,k,ar,dp))
	pr(solve(n,k,ar,dp,0))

main()
Problem p03171 (python)

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

mem = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
    mem[i][i] = arr[i]

for diff in range(1, n):
    for L in range(n-diff):
        H = L+diff
        mem[L][H] = max(arr[L]-mem[L+1][H], arr[H]-mem[L][H-1])

print(mem[0][n-1])

Buggy submission (verdict: Runtime Error):
dp = {}

def getAns(arr, L, H):
    # print(arr)
    if (L, H) in dp:
        return dp[(L, H)]
    if L == H:
        return arr[L]
    else:
        res =  max(
            arr[L]-getAns(arr, L+1, H), 
            arr[H]-getAns(arr, L, H-1)
            )
        
        dp[(L, H)] = res
        
        return res

n = int(input())
arr = list(map(int, input().strip().split()))
print(getAns(arr, 0, n-1))
Problem p03160 (python)

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

dp = [0] * n
dp[0] = 0
for i in range(1, n):
    if i - 2 < 0:
        dp[i] = dp[i-1] + abs(h[i] - h[i-1])
    else: 
        dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]))

print(dp[n-1])

Buggy submission (verdict: Runtime Error):
n,k = map(int, input().split())
h = list(map(int, input().split()))
INF = 999999999999999999999999999
dp = [INF] * n
dp[0] = 0
for i in range(1, n):
    for j in range(1, k+1):
        if i - j < 0:
            continue
        dp[i] = min(dp[i], dp[i-j] + abs(h[i] - h[i-j]))
    
print(dp[n-1])
Problem p03160 (python)

Fixed submission:
N = int(input())
height = list(map(int, input().split(" ")))
dp = [0]*N
 
for i in range(1,N):
  if i <= 2:
    dp[i] = abs(height[i]-height[0])
  else:
    dp[i] = min(dp[i-1] + abs(height[i]-height[i-1]), dp[i-2] + abs(height[i]-height[i-2]))

print(dp[N-1])

Buggy submission (verdict: Runtime Error):
N = input()
height = list(map(int, input().split(" ")))
dp = [0]*N
 
for i in range(1,N):
  if i <= 2:
    dp[i] = abs(height[i]-height[0])
  if dp[i-1] < dp[i-2]:
    dp[i] = dp[i-1] + abs(height[i]-height[i-1])
  else:
    dp[i] = dp[i-2] + abs(height[i]-height[i-2])
 
print(dp[N-1])

Buggy submission (verdict: Runtime Error):
N = input()
height = list(map(int, input().split(" ")))
dp = [0]*N
dp[1] = 

for i in range(1,N):
  if i <= 2:
    dp[i] = abs(height[i]-height[0])
  if dp[i-1] < dp[i-2]:
    dp[i] = dp[i-1] + abs(height[i]-height[i-1])
  else:
    dp[i] = dp[i-2] + abs(height[i]-height[i-2])

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

Fixed submission:
from sys import stdin, setrecursionlimit as srl
from threading import stack_size
srl(int(1e9)+7)
stack_size(int(1e8))

def get(i, value, vis):
    if vis[i]:
        return value[i]
    vis[i] = True
    ans = 0
    for j in adj[i]:
        ans = max(ans, 1+get(j, value, vis))
    value[i] = ans
    return ans


n, m = map(int, input().split())
adj = {}
for i in range(n):
    adj[i] = []
for i in range(m):
    x, y = map(int,input().split())
    adj[x-1].append(y-1)
vis = [False for i in range(n)]
value = [0 for i in range(n)]
for i in range(n):
    value[i] = get(i, value, vis)
x = 0
for i in value:
    x = max(x, i)
print(x)

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
graph=[[] for i in range(n+1)]
for i in range(m):
	x,y=map(int,input().split())
	graph[x].append(y)

def dfs(node):
	ans=0
	if(vis[node]):
		return dp[node]
	vis[node]=1
	for child in graph[node]:
		ans=max(ans,1+dfs(child))
	dp[node]=ans
	return ans
vis=[0 for i in range(n+1)]
dp=[0 for i in range(n+1)]
ans=0
for i in range(1,n+1):
	if(not vis[n]):
		a=dfs(i)
		ans=max(ans,a)
print(ans)
print(dp)

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
graph=[[] for i in range(n+1)]
for i in range(m):
	x,y=map(int,input().split())
	graph[x].append(y)

def dfs(node):
	ans=0
	if(vis[node]):
		return dp[node]
	vis[node]=1
	for child in graph[node]:
		ans=max(ans,1+dfs(child))
	dp[node]=ans
	return ans
vis=[0 for i in range(n+1)]
dp=[0 for i in range(n+1)]
ans=float('-inf')
for i in range(1,n+1):
    dp[i]=dfs(i)
print(max(dp))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
graph=[[] for i in range(n+1)]
for i in range(m):
	x,y=map(int,input().split())
	graph[x].append(y)

def dfs(node):
	ans=0
	if(vis[node]):
		return dp[node]
	vis[node]=1
	for child in graph[node]:
		ans=max(ans,1+dfs(child))
	dp[node]=ans
	return ans
vis=[0 for i in range(n+1)]
dp=[0 for i in range(n+1)]
ans=float('-inf')
for i in range(1,n+1):
	if(not vis[n]):
		a=dfs(i)
		ans=max(ans,a)
print(ans)
Problem p03160 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
def solve(n,l,pos):
    if(pos==n):
        return 0
    if(pos==n-1):
        return abs(l[-1]-l[-2])
    if(t[pos]!=-1):
        return t[pos]
    t[pos]=min(abs(l[pos-1]-l[pos])+solve(n,l,pos+1),abs(l[pos-1]-l[pos+1])+solve(n,l,pos+2))
    return t[pos]
n=int(input())
l=list(map(int,input().split()))
global t
t=[-1]*(n+1)
print(solve(n,l,1))

Buggy submission (verdict: Runtime Error):
def solve(n,l,pos):
    ans=0
    if(pos==n):
        return 0
    if(pos==n-1):
        return abs(l[-1]-l[-2])
    ans+=min(abs(l[pos-1]-l[pos])+solve(n,l,pos+1),abs(l[pos-1]-l[pos+1])+solve(n,l,pos+2))
    return ans
n=int(input())
l=list(map(int,input().split()))
print(solve(n,l,1))
Problem p03165 (python)

Fixed submission:

# Returns length of LCS for X[0..m-1], Y[0..n-1] 
def lcs(X, Y, m, n): 
	L = [[0 for x in range(n+1)] for x in range(m+1)] 

	# Following steps build L[m+1][n+1] in bottom up fashion. Note 
	# that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] 
	for i in range(m+1): 
		for j in range(n+1): 
			if i == 0 or j == 0: 
				L[i][j] = 0
			elif X[i-1] == Y[j-1]: 
				L[i][j] = L[i-1][j-1] + 1
			else: 
				L[i][j] = max(L[i-1][j], L[i][j-1]) 

	# Following code is used to print LCS 
	index = L[m][n] 

	# Create a character array to store the lcs string 
	lcs = [""] * (index+1) 
	lcs[index] = "" 

	# Start from the right-most-bottom-most corner and 
	# one by one store characters in lcs[] 
	i = m 
	j = n 
	while i > 0 and j > 0: 

		# If current character in X[] and Y are same, then 
		# current character is part of LCS 
		if X[i-1] == Y[j-1]: 
			lcs[index-1] = X[i-1] 
			i-=1
			j-=1
			index-=1

		# If not same, then find the larger of two and 
		# go in the direction of larger value 
		elif L[i-1][j] > L[i][j-1]: 
			i-=1
		else: 
			j-=1

	print (''.join(lcs))

# Driver program 

s=input()
t=input()
m = len(s) 
n = len(t) 
lcs(s, t, m, n)

Buggy submission (verdict: Runtime Error):

def solve(s,t,n,m,dp):
    if n<0 or m<0:
            return ''
    if dp[n][m]!=-1:
        return dp[n][m]
    else:
        ans=''
        if s[n]==t[m]:
            ans=s[n]+solve(s,t,n-1,m-1,dp)
            dp[n][m]= ans
        else:
            s1=solve(s,t,n-1,m,dp)
            s2=solve(s,t,n,m-1,dp)
            if len(s1)>len(s2):
                ans=s1
            else:
                ans=s2
            dp[n][m]= ans
        return dp[n][m]


s=input()
t=input()
dp=[[-1]*(len(t)) for i in range(len(s))] 
# print(dp)
solve(s,t,len(s)-1,len(t)-1,dp)
print(dp[-1][-1][::-1])
Problem p03160 (python)

Fixed submission:
n = int(input())
h = [int(x) for x in input().split()]
dp = [0]*n
dp[n-2] = abs(h[n-1]-h[n-2])

for i in range(n-3,-1,-1):
  dp[i] = min(dp[i+1]+abs(h[i]-h[i+1]),dp[i+2]+abs(h[i]-h[i+2]))
  
print(dp[0])

Buggy submission (verdict: Runtime Error):
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
dp = [float('inf')]*n
dp[-1] = 0
dp[-2] = abs(h[-1]-h[-2])

for i in range(n-3,-1,-1):
  for j in range(i+1,min(i+k+1,n)):
    dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
                
print(dp[0])
Problem p03161 (python)

Fixed submission:
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
dp = [float('inf')]*n
dp[-1] = 0
dp[-2] = abs(h[-1]-h[-2])

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

Buggy submission (verdict: Runtime Error):
n,k = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
dp = [float('inf')]*n
dp[-1] = 0
dp[-2] = abs(h[-1]-h[-2])

for i in range(n-3,-1,-1):
  for j in range(i+1,min(i+k,n)):
    dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j])
                
print(dp[0])
Problem p03163 (python)

Fixed submission:
N, W = map(int, input().split())
import numpy as np
dp = np.zeros(W+1, dtype=int)
for i in range(N):
    w, v = map(int, input().split())
    np.maximum(dp[:-w] + v, dp[w:], out=dp[w:])
print(dp.max())

Buggy submission (verdict: Runtime Error):
6 15
6 5
5 6
6 4
6 6
3 5
7 2

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

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

Fixed submission:
# DP practice
# https://atcoder.jp/contests/dp/tasks/dp_a

# 入力方法としてはこれが手軽
N = int(input())
h = list(map(int, input().split()))

dp = list()

# N = 0, 1 の時の処理も必要。だが省く
dp.append(0)
dp.append(dp[0] + abs(h[1] - h[0]))

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


print(dp[N-1])

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

dp = list()

for i in range (0, N):
	inp = list(map(int, input().split()))

	w.append(inp[0])
	v.append(inp[1])	

for i in range (0, N + 1):
	dp.append([0] * ( W + 1 ))

for i in range(0, N):
	for j in range(1, W+1):
		if(j >= w[i]):
			dp[i+1][j] = max(dp[i][j-w[i]] + v[i], dp[i][j])
		else:
			dp[i+1][j] = dp[i][j]

print(dp[N][W])

Buggy submission (verdict: Runtime Error):
# DP practice
# https://atcoder.jp/contests/dp/tasks/dp_b

# 入力方法としてはこれが手軽
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = list()

# N = 0, 1 の時の処理も必要。だが省く
dp.append(0)

# dp_a を改造した方法で単純にやってみる。
for i in range(1, N):

	min_cost_sum = 99999999
	for k in range(1, K+1):
		if(i-k) < 0:
			break

		tmp = dp[i-k] + abs(h[i] - h[i-k])
		if(tmp < min_cost_sum):
			min_cost_sum = tmp

	dp.append(min_cost_sum)


print(dp[N-1])

# Failure
# 1.初期値を dp[0] = h[0] の方が正しいと思ってしまった
#   → dpの意味を誤解
#
# 2.abs をつけていなかった
#   → コストの全てのパターン意識していなかった。正の場合しか考えていない
#
Problem p03160 (python)

Fixed submission:
n=int(input())
h=list(map(int,input().split()))
dp=[10**9]*n
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(n-1):
    dp[i+1]=min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
    if i+2<n:
        dp[i+2]=min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
print(dp[-1])

Buggy submission (verdict: Runtime Error):
INF=1000000000
def getans(i):
    if i>=(n-1):
        dp[n-1]= 0
        return dp[n-1]
    if dp[i]!=-1:
        return dp[i]
    if i==(n-2):
        dp[i]=abs(h[i]-h[i+1])
        return dp[i]
    else:
        dp[i]=min(h[i]-h[i+1]+getans(i+1),h[i]-h[i+2]+getans(i+2))
        return dp[i]


n=int(input())
dp=[-1]*n
h=list(map(int,input().split()))
ans=getans(0)
print(ans)

Buggy submission (verdict: Runtime Error):
INF=1000000000
def getans(i):
    if i>=(n-1):
        dp[n-1]= 0
        return dp[n-1]
    if dp[i]!=-1:
        return dp[i]
    if i==(n-2):
        dp[i]=abs(h[i]-h[i+1])
        return dp[i]
    else:
        dp[i]=min(abs(h[i]-h[i+1])+getans(i+1),abs(h[i]-h[i+2])+getans(i+2))
        return dp[i]


n=int(input())
dp=[-1]*n
h=list(map(int,input().split()))
ans=getans(0)
print(ans)
Problem p03166 (python)

Fixed submission:
import collections
import sys
from functools import lru_cache
sys.setrecursionlimit(int(10**9))
n, m  = [int(i) for i in input().strip().split(' ')] 
edges = collections.defaultdict(list)
for i in range(m):
    s,d = [int(i) for i in input().strip().split(' ')]
    edges[s-1].append(d-1) 
# dp = [-1]*n
maxPath = 0 
@lru_cache(None)
def dfs(node): 
    ans = 0 
    for i in edges[node]:
        ans = max(ans,dfs(i)+1)
    return ans
 
for i in range(n):
    maxPath = max(dfs(i), maxPath) 
print(maxPath)

Buggy submission (verdict: Runtime Error):
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit = 1000000

input  = stdin.readline

n,m = list(map(int,input().split()))
par = [[] for _ in range(n+1)]
dis = [0]*(n+1)
vis = dis[:]

def dfs(n):
	for i in par[n]:
		dis[i] = max(dis[i], dis[n]+1)
		dfs(i)

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

for i in range(1,n+1):
	if not vis[i]:
		dfs(i)

print(max(dis))
Problem p03160 (python)

Fixed submission:



def findmin(arr,n):
  dp = [0]*n
  dp[1] = abs(arr[0]-arr[1])
  for i in range(2,n):
    dp[i] = min(dp[i-1]+abs(arr[i-1]-arr[i]), dp[i-2]+abs(arr[i-2]-arr[i]))
  return dp[-1]

n = int(input())
arr = list(map(int,input().split()))
print(findmin(arr,n))

Buggy submission (verdict: Runtime Error):
def findmin(arr,n):
  dp = [0]*n
  dp[1] = abs(arr[0]-arr[1])
  for i in range(2,n):
    dp[i] = min(dp[i-1]+abs(arr[i-1]-arr[i]), dp[i-2]+abs(arr[i-2]-arr[i]))
  return dp[-1]

n = int(input())
arr = list(map(int,input().split())
print(findmin(arr,n))