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

Fixed submission:
import math
import os
import random
import re
import sys
def dpfindpath(a):
	dpath=[int(0) for _ in range(len(a))]
	dpath[0]=0
	dpath[1]=abs(a[1]-a[0])
	for i in range(2,len(a)):
		dpath[i]=min(abs(a[i]-a[i-1])+dpath[i-1],abs(a[i]-a[i-2])+dpath[i-2])
	return dpath[len(a)-1]
n=int(input())
a=input().split()
for i in range(n):
  a[i]=int(a[i])
print (dpfindpath(a))

Buggy submission (verdict: Runtime Error):
import math
import os
import random
import re
import sys
def dpkfindpath(a,k):
	dpath=[int(0) for _ in range(len(a))]
	for i in range(k):
		dpath[i]=abs(a[i]-a[0])
	
	for i in range(k,len(a)):
		li=[abs(a[i]-a[ik])+dpath[ik] for ik in range(i-1,i-k-1,-1) ]
		dpath[i]=min(li)
		
	return dpath[len(a)-1]
		
n,k=input().split()
n=int(n)
k=int(k)
a=input().split()
for i in range(n):
  a[i]=int(a[i])
if n>=k:
	print (dpkfindpath(a,k))

Buggy submission (verdict: Runtime Error):
import math
import os
import random
import re
import sys
def dpkfindpath(a,k):
	dpath=[int(0) for _ in range(len(a))]
	for i in range(k):
		dpath[i]=abs(a[i]-a[0])
	
	for i in range(k,len(a)):
		m=abs(a[i]-a[i-1])+dpath[i-1]
		for j in range(1,k+1):
			if (abs(a[i]-a[i-j])+dpath[i-j])<m:
				m=abs(a[i]-a[i-j])+dpath[i-j]
		dpath[i]=m
	return dpath[len(a)-1]
		
n,k=input().split()
n=int(n)
k=int(k)
a=input().split()
for i in range(n):
  a[i]=int(a[i])
print (dpkfindpath(a,k))

Buggy submission (verdict: Runtime Error):
import math
import os
import random
import re
import sys
def findpath(a):
  if len(a)==2:
     return abs(a[1]-a[0])    
  if len(a)==3:
    return min(abs(a[2]-a[1])+abs(a[1]-a[0]),abs(a[2]-a[0]))
  return min(abs(a[2]-a[0])+findpath(a[2::]),abs(a[1]-a[0])+findpath(a[1::]))
n=int(input())
a=input().split()
for i in range(n):
  a[i]=int(a[i])
print (findpath(a))

Buggy submission (verdict: Runtime Error):
import math
import os
import random
import re
import sys
def findpath(a):
  if len(a)==3:
    return min(abs(a[2]-a[1])+abs(a[1]-a[0]),abs(a[2]-a[0]))
  return min(abs(a[2]-a[0])+findpath(a[2::]),abs(a[1]-a[0])+findpath(a[1::]))
n=int(input())
a=input().split()
for i in range(n):
  a[i]=int(a[i])
print findpath(a)

Buggy submission (verdict: Runtime Error):
import math
import os
import random
import re
import sys
def findpath(a):
  if len(a)==3:
    return min(abs(a[2]-a[1])+abs(a[1]-a[0]),abs(a[2]-a[0]))
  return min(abs(a[2]-a[0])+findpath(a[2::]),abs(a[1]-a[0])+findpath(a[1::]))
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
print findpath(a)
Problem p03160 (python)

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

Buggy submission (verdict: Runtime Error):
x=int(input())
n=int(input())
arr=list(map(int,input().split()))
dp=[0]*len(arr)
dp[0]=0
dp[1]=abs(arr[1]-arr[0])
for i in range(2,len(arr)):
    dp[i]=min((dp[i-1]+abs(arr[i-1]-arr[i])),(dp[i-2]+abs(arr[i-2]-arr[i])))
print(dp[len(arr)-1])
Problem p03166 (python)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 14:22:54 2020

@author: Maruthi Srinivas
"""
import sys
sys.setrecursionlimit(1000000)
def dfs(node,graph,dp):
    temp=0
    if dp[node]!=-1:
        return dp[node]
    for i in graph[node]:
        temp=max(temp,dfs(i,graph,dp)+1)
    dp[node]=temp
    return temp
            
v,e=map(int,input().split())
adj={x:[] for x in range(v+1)}
for i in range(e):
    x,y=map(int,input().split())
    adj[x].append(y)
m=-1
dp=[-1]*(v+1)
for i in range(1,v+1):
    x=dfs(i,adj,dp)
    m=max(m,x)
print(m)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 26 14:22:54 2020

@author: Maruthi Srinivas
"""
import sys
sys.setrecursionlimit(10000000000)
def dfs(node,graph):
    temp=0
    for i in graph[node]:
        temp=max(temp,dfs(i,graph)+1)
    return temp
            
v,e=map(int,input().split())
adj={x:[] for x in range(v+1)}
for i in range(e):
    x,y=map(int,input().split())
    adj[x].append(y)
m=-1
for i in range(1,v+1):
    visited=[False]*(v+1)
    x=dfs(i,adj)
    #print(i,x)
    m=max(m,x)
print(m)
Problem p03169 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))
one=two=three=0
for i in arr:
    if i==1:
        one+=1
    elif i==2:
        two+=1
    elif i==3:
        three+=1
max_one=one+two+three
max_two=two+three
max_three=three
dp=[[[0.0]*(n+1) for i in range(n+1)] for j in range(n+1)]
for k in range(max_three+1):
    for j in range(max_two+1-k):
        for i in range(max_one+1-j-k):
            if i+j+k==0:
                continue
            else:
                dp[i][j][k]=n/(i+j+k)
            if i>0:
                dp[i][j][k] += (dp[i-1][j][k]*i)/(i+j+k)
            if j>0:
                dp[i][j][k] += (dp[i+1][j-1][k]*j)/(i+j+k)
            if k>0:
                dp[i][j][k] += (dp[i][j+1][k-1]*k)/(i+j+k)
print(dp[i][j][k])

Buggy submission (verdict: Runtime Error):
n=int(input())
arr=list(map(int,input().split()))
one=two=three=0
for i in arr:
    if i==1:
        one+=1
    elif i==2:
        two+=1
    elif i==3:
        three+=1
max_one=one+two+three
max_two=two+three
max_three=three
dp=[[[0.0]*(n+1) for i in range(n)] for j in range(n+1)]
for k in range(max_three+1):
    for j in range(max_two+1-k):
        for i in range(max_one+1-j-k):
            if i+j+k==0:
                continue
            else:
                dp[i][j][k]=n/(i+j+k)
            if i>0:
                dp[i][j][k] += (dp[i-1][j][k]*i)/(i+j+k)
            if j>0:
                dp[i][j][k] += (dp[i+1][j-1][k]*j)/(i+j+k)
            if k>0:
                dp[i][j][k] += (dp[i][j+1][k-1]*k)/(i+j+k)
print(dp[i][j][k])
Problem p03160 (python)

Fixed submission:
def fun(a):
    if len(a)==1:
        return 0
    dp = [0]*len(a)
    dp[0]=a[0]
    dp[1]=dp[0]+abs(a[0]-a[1])
    for i in range(2,len(a)):
        dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]) ,dp[i-2]+abs(a[i]-a[i-2]))
    return dp[-1]-a[0]
  
n = int(input())
a = list(map(int , input().split()))
print(fun(a))

Buggy submission (verdict: Runtime Error):
def fun(a,k,n): ## k different steps can be taken
    if n==1:
        return 0
    dp = [0]*n
    dp[1]=dp[0]+abs(a[0]-a[1])
    for i in range(2,len(a)):
        t = 0
        min_ = float('inf')
        for j in range(i-1,-1,-1):
            if t==k:
                break
            else:
                t+=1
            if dp[j]+abs(a[i]-a[j]) < min_ :
                   min_ = dp[j]+abs(a[i]-a[j])
        dp[i]=min_
    return dp[-1]
from sys import stdin , stdout
n,k = map(int , stdin.readline().split())
a = list(map(int , stdin.readline().split()))
print(fun(a,k,n))
Problem p03161 (python)

Fixed submission:
def fun(a,k,n): ## k different steps can be taken
    if n==1:
        return 0
    dp = [0]*n
    dp[1]=dp[0]+abs(a[0]-a[1])
    for i in range(2,len(a)):
        t = 0
        min_ = float('inf')
        for j in range(i-1,-1,-1):
            if t==k:
                break
            else:
                t+=1
            if dp[j]+abs(a[i]-a[j]) < min_ :
                   min_ = dp[j]+abs(a[i]-a[j])
        dp[i]=min_
    return dp[-1]
from sys import stdin , stdout
n,k = map(int , stdin.readline().split())
a = list(map(int , stdin.readline().split()))
print(fun(a,k,n))

Buggy submission (verdict: Runtime Error):
import numba as nb
@nb.njit
def fun(a,k,n): ## k different steps can be taken
    if n==1:
        return 0
    dp = [0]*n
    dp[1]=dp[0]+abs(a[0]-a[1])
    for i in range(2,len(a)):
        t = 0
        min_ = float('inf')
        for j in range(i-1,-1,-1):
            if t==k:
                break
            else:
                t+=1
            if dp[j]+abs(a[i]-a[j]) < min_ :
                   min_ = dp[j]+abs(a[i]-a[j])
        dp[i]=min_
    return dp[-1]
from sys import stdin , stdout
n,k = map(int , stdin.readline().split())
a = list(map(int , stdin.readline().split()))
print(fun(a,k,n))
Problem p03163 (python)

Fixed submission:
import numba as nb
import sys
import numpy as np
@nb.njit
def solve(w,weight,value):
    total=w
    dp = np.zeros(shape=(n + 1, w + 1), dtype=np.int64)
    for i in range(l):
        for w in range(total+1):
            if weight[i]<=w:
                dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
            else:
                dp[i][w] = dp[i-1][w]
    print(dp[l-1][total])
n,w=map(int,sys.stdin.readline().split())
weight=[0]*n
value=[0]*n
total=w
for i in range(n):
    x,y=map(int,sys.stdin.readline().split())
    weight[i]=x
    value[i]=y
l=len(weight)
solve(w,weight,value)

Buggy submission (verdict: Runtime Error):
import sys
n,w=map(int,sys.stdin.read().split())
weight=[]
value=[]
total=w
for i in range(n):
    x,y=map(int,sys.stdin.read().split())
    weight.append(x)
    value.append(y)
l=len(weight)
dp=[[0]*(w+1) for j in range(l)]
for i in range(l):
    for w in range(total+1):
        if weight[i]<=w:
            dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(dp[l-1][total])

Buggy submission (verdict: Runtime Error):
n,w=map(int,input().split())
weight=[]
value=[]
total=w
for i in range(n):
    x,y=map(int,input().split())
    weight.append(x)
    value.append(y)
l=len(weight)
dp=[[0]*w for j in range(l)]
for i in range(l):
    for w in range(total+1):
        if weight[i]<=w:
            dp[i][w]=max(value[i]+dp[i-1][w-weight[i]],dp[i-1][w])
        else:
            dp[i][w] = dp[i-1][w]
print(dp[l-1][total])
Problem p03169 (python)

Fixed submission:
n=int(input())
l=list(map(int,input().split()))
ones,twos,threes=0,0,0
for i in l:
    if i==1:
        ones+=1
    elif i==2:
        twos+=1
    else:
        threes+=1
max_ones=ones+twos+threes
max_twos=twos+threes
max_threes=threes
dp=[[[0.0]*(n+1)for i in range(n+1)]for j in range(n+1)]
for k in range(max_threes+1):
    for j in range(max_twos+1-k):
        for i in range(max_ones+1-j-k):
            if (i+j+k)==0:
                continue
            else:
                dp[i][j][k]=n/(i+j+k)
                if i>0:
                    dp[i][j][k]+=(dp[i-1][j][k]*i)/(i+j+k)
                if j>0:
                    dp[i][j][k]+=(dp[i+1][j-1][k]*j)/(i+j+k)
                if k>0:
                    dp[i][j][k]+=(dp[i][j+1][k-1]*k)/(i+j+k)
print(dp[i][j][k])

Buggy submission (verdict: Runtime Error):
n=int(input())
l=list(map(int,input().split()))
ones,twos,threes=0,0,0
for i in l:
    if i==1:
        ones+=1
    elif i==2:
        twos+=2
    else:
        threes+=3
max_ones=ones+twos+threes
max_twos=twos+threes
max_threes=threes
dp=[[[0.0]*(n+1)for i in range(n+1)]for j in range(n+1)]
for k in range(max_threes+1):
    for j in range(max_twos+1-k):
        for i in range(max_ones+1-j-k):
            if i+j+k==0:
                continue
            else:
                dp[i][j][k]=n/(i+j+k)
                if i>0:
                    dp[i][j][k]+=(dp[i-1][j][k]*i)/(i+j+k)
                if j>0:
                    dp[i][j][k]+=(dp[i+1][j-1][k]*j)/(i+j+k)
                if k>0:
                    dp[i][j][k]+=(dp[i][j+1][k-1]*k)/(i+j+k)
print(dp[i][j][k])

Buggy submission (verdict: Runtime Error):
n=int(input())
l=list(map(int,input().split()))
ones,twos,threes=0,0,0
for i in l:
    if i==1:
        ones+=1
    elif i==2:
        twos+=2
    else:
        threes+=3
max_ones=ones+twos+threes
max_twos=twos+threes
max_threes=threes
dp=[[[0.0]*(n+1)for i in range(n+1)]for j in range(n+1)]
for k in range(max_threes+1):
    for j in range(max_twos+1-k):
        for i in range(max_ones+1-j-k):
            if i+j+k==0:
                continue
            else:
                dp[i][j][k]=n/(i+j+k)
                if i>0:
                    dp[i][j][k]+=(dp[i-1][j][k]*i)/(i+j+k)
                if j>0:
                    dp[i][j][k]+=(dp[i+1][j-1][k]*j)/(i+j+k)
                if k>0:
                    dp[i][j][k]+=(dp[i][j+1][k-1]*k)/(i+j+k)
print(dp[i][j][k])
Problem p03160 (python)

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

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

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

print(res[-1])

Buggy submission (verdict: Runtime Error):
r,c=map(int,input().split())
mod=10**9+7
g=[]
for _ in range(r):
  g.append(input())
dp=[[0 for j in range(c)] for i in range(r)]
dp[0][0]=1
for i in range(r):
  for j in range(c):
    if g[i][j]=='.':
        if i>0:
            dp[i][j]+=dp[i-1][j]%mod
        if j>0:
            dp[i][j]+=dp[i][j-1]%mod


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

Fixed submission:
import sys
n,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j],dp[i]+abs(a[i]-a[j]))
print(dp[n-1])

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
h=list(map(int,input().split()))
res=[-1]*n
res[0]=0
for i in range(1,k):
    
    res[i]=abs(h[i-1]-h[i])
for i in range(k,n):
    mini=999999999
    for j in range(1,k+1):
        mini=min(abs(h[i-j]-h[i])+res[i-j],mini)
    res[i]=mini

print(res[-1])
Problem p03162 (python)

Fixed submission:
n=int(input())
a=[]
for _ in range(n):
    a.append(list(map(int,input().split())))
for i in range(1,n):
    a[i][0]+=max(a[i-1][1],a[i-1][2])
    a[i][1]+=max(a[i-1][0],a[i-1][2])
    a[i][2]+=max(a[i-1][1],a[i-1][0])
print(max(a[n-1]))

Buggy submission (verdict: Runtime Error):
n=int(input())
a=[]
for _ in range(n):
    a.append(list(map(int,input().split())))
dp=[0]*n
for i in range(3):
  dp[i]=a[0][i]
for k in range(1,n):
    m=[0]*3
    for i in range(3):
        for j in range(3):
            if i!=j:
                m[i]=max(m[i],dp[j]+a[k][i])
    dp=m
print(max(m))

Buggy submission (verdict: Runtime Error):
n=int(input())
a=[]
for _ in range(n):
    a.append(list(map(int,input().split()))) 
dp=[0]*n
for i in range(3):
  dp[i]=a[0][i]
for k in range(1,n):
    m=[0,0,0]
    for i in range(3):
        for j in range(3):
            if i!=j:
                m[i]=max(m[i],dp[j]+a[k][i])
    dp=m
print(max(m))

Buggy submission (verdict: Runtime Error):
n=int(input())
a=[]
for _ in range(n):
    a.append(list(map(int,input().split())))
dp=a[0]

for k in range(1,n):
    m=[0,0,0]
    for i in range(3):
        for j in range(3):
            if i!=j:
                m[i]=max(m[i],dp[j]+a[k][i])
    dp=m
print(max(m))
Problem p03165 (python)

Fixed submission:
a=input()
b=input()
dp=[[0 for i in range(len(a)+1)] for j in range(len(b)+1)]
for i in range(1,len(b)+1):
  for j in range(1,len(a)+1):
    if a[j-1]==b[i-1]:
      dp[i][j]=dp[i-1][j-1]+1
    else:
      dp[i][j]=max(dp[i-1][j],dp[i][j-1])

res=''

j=len(a)
i=len(b)
while j>0 and i>0:
 
    if dp[i][j]==dp[i-1][j]:
        i-=1
  
    elif dp[i][j]==dp[i][j-1]:
        j-=1
    elif dp[i][j]==dp[i-1][j-1]+1:

        res+=a[j-1]
        i-=1
        j-=1
    
print(res[::-1])

Buggy submission (verdict: Runtime Error):
a=input()
b=input()
dp=[[0 for i in range(len(a+1))] for j in range(len(b+1))]
for i in range(1,en(a+1)):
  for j in range(1,len(b+1)):
    if a[j-1]==b[i-1]:
      dp[i][j]=dp[i-1][j-1]
    else:
      dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1
print(dp[-1][-1])
Problem p03171 (python)

Fixed submission:
def f(a,i,j,dp):
    if dp[i][j]==-1:
        if i>j:
            return 0

        dp[i][j]=max(a[i]+min(f(a,i+2,j,dp),f(a,i+1,j-1,dp)),
                                a[j]+min(f(a,i+1,j-1,dp),f(a,i,j-2,dp)))
    return dp[i][j]
        
n=int(input())
dp=[[-1 for i in range(n)] for j in range(n)]
a=list(map(int,input().split()))
for i in range(n):
    dp[i][i]=a[i]

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

s1=f(a,0,n-1,dp)

print(s1-sum(a)+s1)

Buggy submission (verdict: Runtime Error):
def f(a,p1,p2):
    if p1>p2:
        return 0
   
    return max(a[p1]+min(f(a,p1+2,p2),f(a,p1+1,p2-1))
                      ,a[p2]+min(f(a,p1+1,p2-1),f(a,p1,p2-2)))
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
p1=0
p2=n-1
first=f(a,n,0,p1,p2,2)
print(first-(s-first))

Buggy submission (verdict: Runtime Error):
def f(a,n,ans1,p1,p2,flag):
    if p1>p2:
        return ans1
    if flag&1==0:
        return max(f(a,n,ans1+a[p1],p1+1,p2,flag+1),
                   f(a,n,ans1+a[p2],p1,p2-1,flag+1))
    return min(f(a,n,ans1,p1+1,p2,flag+1),f(a,n,ans1,p1,p2-1,flag+1))
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
p1=0
p2=n-1
first=f(a,n,0,p1,p2,2)
print(first-(s-first))

Buggy submission (verdict: Runtime Error):
def f(a,n,ans1,p1,p2,flag):
    if p1>p2:
        return ans1
    if flag&1==0:
        return max(f(a,n,ans1+a[p1],p1+1,p2,flag+1),
                   f(a,n,ans1+a[p2],p1,p2-1,flag+1))
    return min(f(a,n,ans1,p1+1,p2,flag+1),f(a,n,ans1,p1,p2-1,flag+1))
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
p1=0
p2=n-1
first=f(a,n,0,p1,p2,2)
print(first-(s-first))
Problem p03160 (python)

Fixed submission:
N = int(input())
jumps = [int(n) for n in input().split()]

dp = [float("inf")] * N
dp[0] = 0

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

print(dp[-1])

Buggy submission (verdict: Runtime Error):
return True
Problem p03167 (python)

Fixed submission:
MOD = 1000000007
h,w=map(int,input().split())
graph=[]
for i in range(h):
  graph.append(list(input()))
#print(graph)
dp=[[0]*(w) for _ in range(h)]
dp[0][0]=1
for i in range(1,w):
  if graph[0][i]=='#':
    break
  dp[0][i]=1
for i in range(1,h):
  if graph[i][0]=='#':
    break
  dp[i][0]=1
for i in range(1,h):
  for j in range(1,w):
    #print(i-1,j-1)
    if(graph[i][j]=='#'):
      dp[i][j]=0
    else:
      dp[i][j]=(dp[i-1][j]+dp[i][j-1])%MOD

print(dp[-1][-1])

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
graph=[]
for i in range(h):
  graph.append(list(input()))
#print(graph)
dp=[[0]*(w) for _ in range(h)]
dp[0][0]=1
for i in range(1,w):
  dp[0][i]=dp[0][i-1] if graph[0][i]=='.' else 0
for i in range(1,h):
  dp[i][0]=dp[i-1][0] if graph[0][i]=='.' else 0
for i in range(1,h):
  for j in range(1,w):
    #print(i-1,j-1)
    if(graph[i-1][j-1]=='#'):
      dp[i][j]=0
    else:
      dp[i][j]=(dp[i-1][j]+dp[i][j-1])%(1e9 + 7)

print(int(dp[-1][-1]))
Problem p03160 (python)

Fixed submission:
N = int(input())
h = [*map(lambda x: int(x), input().split())]

#bottom-up

def iterative_method(final):
    i = 0
    min_table = {0: 0}

    while (i < final):
        if i+1 in min_table:
            min_table[i+1] =  min(min_table[i+1], min_table[i] + abs(h[i+1] - h[i]))
        else:
            min_table[i+1] = min_table[i] + abs(h[i+1] - h[i])
        try:
            if i+2 in min_table:
                min_table[i+2] = min(min_table[i+2], min_table[i] + abs(h[i+2] - h[i]))
            else:
                min_table[i+2] = min_table[i] + abs(h[i+2] - h[i])
        except:
            break
        i += 1


    print(min_table[final])

iterative_method(N-1)

Buggy submission (verdict: Runtime Error):
N = int(input())
h = [*map(lambda x: int(x), input().split())]

#bottom-up
def iterative_method(final):
    i = 0
    min_table = {0: 0}

    while (i < final):
        if i+1 in min_table:
            min_table[i+1] =  min(min_table[i+1], min_table[i] + abs(h[i+1] - h[i]))
        else:
            min_table[i+1] = min_table[i] + abs(h[i+1] - h[i])
        try:
            if i+2 in min_table:
                min_table[i+2] = min(min_table[i+2], min_table[i] + abs(h[i+2] - h[i]))
            else:
                min_table[i+2] = min_table[i] + abs(h[i+2] - h[i])
        except:
            break
        i += 1


    print(min_table[final])

#topdown
def recursive_method(n):
    memo = [-1]*n
    memo[0] = 0
    memo[1] = abs(h[1] - h[0])
    def recurse(i):
        if memo[i] != -1:
            return memo[i]
        else:
            memo[i] = min( recurse(i-1) + abs(h[i] - h[i-1]), recurse(i-2) + abs(h[i] - h[i-2]))
            return memo[i]
    return recurse(n-1)

    

print(recursive_method(N))
Problem p03162 (python)

Fixed submission:
import numpy as np

N = int(input())
a = np.zeros((N,3))
for i in range(N):
    a[i][0],a[i][1],a[i][2] = map(int,input().split())

memo = np.zeros((N,3))

memo[0][0], memo[0][1], memo[0][2] = a[0][0],a[0][1],a[0][2]
day = 1

while (day < N):
    memo[day] = [max(memo[day-1][(x+1)%3],memo[day-1][(x+2)%3]) + a[day][x] \
                   for x in [0,1,2]]
    day += 1

print(int(max(memo[day - 1])))

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

N, W = [int(x) for x in input().split()]
weight = np.zeros((N,2))
for x in range(N):
    weight[x] = [int(y) for y in input().split()]
#topdown
memo = np.zeros((N+1,W+1))
for x in range(N+1):
    memo[x] = [-1]*(W+1)

def dp(n,w):
    if w < 0:
        return -float('inf')
    elif n == 0 or w == 0:
        return 0
    elif memo[n][w] != -1:
        return memo[n][w]
    else:
        memo[n][w] = max(dp(n-1,w), dp(n-1,w - int(weight[n-1][0])) + weight[n-1][1])
        return memo[n][w]

print(int(dp(N,W)))
Problem p03163 (python)

Fixed submission:
import numpy as np

N, W = [int(x) for x in input().split()]
items = [tuple(map(int, input().split())) for _ in range(N)]

#topdown
memo = np.zeros(shape = W+1, dtype = np.int64)

for w,v in items:
    memo[w:] = np.maximum( memo[w:], memo[:-w] + v)

print(memo[-1])

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

N, W = [int(x) for x in input().split()]
weight = np.zeros((N,2))
for x in range(N):
    weight[x] = [int(y) for y in input().split()]
#topdown
memo = np.zeros((N+1,W+1))
for x in range(N+1):
    memo[x] = [-1]*(W+1)

def dp(n,w):
    if w < 0:
        return -float('inf')
    elif n == 0 or w == 0:
        return 0
    elif memo[n][w] != -1:
        return memo[n][w]
    else:
        memo[n][w] = max(dp(n-1,w), dp(n-1,w - int(weight[n-1][0])) + weight[n-1][1])
        return memo[n][w]

print(int(dp(N,W)))

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

N, W = [int(x) for x in input().split()]
weight = np.zeros((N,2))
for x in range(N):
    weight[x] = [int(y) for y in input().split()]
#topdown
memo = np.zeros((N+1,W+1))
for x in range(N+1):
    memo[x] = [-1]*(W+1)

def dp(n,w):
    if w < 0:
        return -float('inf')
    elif n == 0 or w == 0:
        return 0
    elif memo[n][w] != -1:
        return memo[n][w]
    else:
        memo[n][w] = max(dp(n-1,w), dp(n-1,w - int(weight[n-1][0])) + weight[n-1][1])
        return memo[n][w]

print(int(dp(N,W)))
Problem p03164 (python)

Fixed submission:

N, W = [int(x) for x in input().split()]
items = [tuple(map(int, input().split())) for _ in range(N)]

#bottomup
memo = [10**10]*(10**5+1)
memo[0] = 0

for weight, value in items:
    for i in reversed(range(len(memo)-value)):
            memo[i+value] = min(memo[i+value],memo[i] + weight)

ans = 0
for i in range(len(memo)):
    if memo[i] != 10**10 and memo[i] <= W:
        ans = i

print(ans)

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

N, W = [int(x) for x in input().split()]
items = [tuple(map(int, input().split())) for _ in range(N)]

#topdown
memo = np.zeros(shape = W+1, dtype = np.int64)

for w,v in items:
    memo[w:] = np.maximum( memo[w:], memo[:-w] + v)

print(memo[-1])