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

Fixed submission:
n, x = map(int, input().split())
a = sorted(list(map(int, input().split())))
ans, i = 0, 0
while i < (n - 1):
    if x - a[i] >= 0:
        x -= a[i]
        ans += 1
    else:
        break
    i += 1
if x == a[i]:
	ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n, x = map(int, input().split())
a = sorted([*map(int, input().split())])
ans, i = 0, 0
while i < (n - 1):
    if x - a[i] >= 0:
        x -= a[i]
        ans += 1
    else:
        break
    i += 1
if x == a[i]:
	ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n, x = map(int, input().split())
a = sorted([*map(int, input().split())])
ans = 0
for i in a:
	if i <= x:
		ans += 1
		x -= i
	else:
		break
if not x == 0 and ans > 0: ans -= 1
print(ans)
Problem p03160 (python)

Fixed submission:
N = int(input())
heights = list(map(int, input().strip().split()))

dp = [float("inf") for _ in range(N)]

dp[0] = 0
if N > 1:
    dp[1] = abs(heights[1] - heights[0])


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

print(dp[N - 1])

Buggy submission (verdict: Runtime Error):
import math

N = int(input())
heights = list(map(int, input().strip().split()))

dp = [math.inf for _ in range(N)]

dp[0] = 0
if len > 1:
    dp[1] = abs(heights[1] - heights[0])


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

print(dp[N - 1])

Buggy submission (verdict: Runtime Error):
import math

N = int(input())
heights = list(map(int, input().strip().split()))

dp = [math.inf for _ in range(N)]

dp[0] = 0
dp[1] = abs(heights[1] - heights[0])


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

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

Fixed submission:
ips = []
ips.append(input())
while True:
    try:
        ips.append(input())
    except (EOFError):
        break
abc = ips[0][0]+ips[1][1]+ips[2][2]
print(abc)

Buggy submission (verdict: Runtime Error):
b, c = map(int, input().split())
def caibun(b,c):
    count = 0
    for i in range(b, c+1, 1):
        a = str(i)
        if a == a[::-1]:
            count +=1
    print(count)
caibun(b,c)
Problem p03423 (python)

Fixed submission:
a = int(input())
def more_than_three(a):
    if a < 3:
        return 0
    else:
        return a//3
print(more_than_three(a))

Buggy submission (verdict: Runtime Error):
ips = []
ips.append(input())
while True:
    try:
        ips.append(input())
    except (EOFError):
        break
num = int(ips[0])
hina = ips[1]
v = []
for i in range(num):
    if hina[i] not in v:
        v.append(hina[i])
if len(v) == 3:
    print("three")
elif len(v) == 4:
    print("four")
Problem p03160 (python)

Fixed submission:
import sys
import math
import functools
from collections import deque
sin = sys.stdin

n = int(sin.readline())
stones = [int(i) for i in sin.readline().split()]
minCost = [float('inf') for i in range(n)]
minCost[0] = 0
minCost[1] = abs(stones[0] - stones[1])

for i in range(2, len(stones)):
    minCost[i] = min(minCost[i-1] + abs(stones[i-1] - stones[i]),
                     minCost[i-2] + abs(stones[i-2] - stones[i]))

print(minCost[n-1])

Buggy submission (verdict: Runtime Error):
n = int(sin.readline())
stones = [int(i) for i in sin.readline().split()]
minCost = [float('inf') for i in range(n)]
minCost[0] = 0
minCost[1] = abs(stones[0] - stones[1])

for i in range(2, len(stones)):
    minCost[i] = min(minCost[i-1] + abs(stones[i-1] - stones[i]),
                     minCost[i-2] + abs(stones[i-2] - stones[i]))

print(minCost)
print(minCost[n-1])
Problem p03160 (python)

Fixed submission:
inf = float('inf')
def main():
    n = int(input().rstrip())
    h = list(map(int, input().rstrip().split()))
    dp = [inf for i in range(n)]
    dp[0]=0
    for i in range(n):
        for j in [i+1, i+2]:
            if j<n:
                dp[j] = min(dp[j], dp[i]+ abs(h[i] - h[j]))
    print(dp[n-1])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
inf = float('inf')
def main():
    n, k = map(int, raw_input().rstrip().split())
    h = list(map(int, raw_input().rstrip().split()))
    dp = [inf for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(i+1, i+k+1):
            if j<n:
                cost = abs(h[i] - h[j])
                dp[j] = min(dp[j], dp[i] + cost)
    print(dp[n-1])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
inf = float('inf')
def main():
    n, k = map(int, input().rstrip().split())
    h = list(map(int, input().rstrip().split()))
    dp = [inf for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(i+1, i+k+1):
            if j<n:
                cost = abs(h[i] - h[j])
                dp[j] = min(dp[j], dp[i] + cost)
    print(dp[n-1])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import math
def main():
    n = int(input().rstrip())
    h = list(map(int, input().rstrip().split()))
    dp = [math.inf for i in range(n)]
    dp[0]=0
    for i in range(n):
        for j in [i+1, i+2]:
            if j<n:
                dp[j] = min(dp[j], dp[i]+ abs(h[i] - h[j]))
    print(dp[n-1])

if __name__ == "__main__":
    main()
Problem p03162 (python)

Fixed submission:
def main():
    n = int(input().rstrip())
    old_dp = [0 for i in range(3)]
    for i in range(n):
        new_dp = [0 for i in range(3)]
        c = list(map(int, input().rstrip().split()))
        for i in range(3):
            for j in range(3):
                if i!=j:
                    new_dp[j] = max(new_dp[j], old_dp[i] + c[j])

        old_dp = new_dp
    print(max(old_dp[0], old_dp[1], old_dp[2]))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def holiday(n):
    old_dp = [0 for i in range(3)]
    for i in range(n):
        new_dp = [0 for i in range(n)]
        c = list(map(int, input().rstrip().split()))
        for i in range(3):
            for j in range(3):
                if i!=j:
                    new_dp[j] = max(new_dp[j], old_dp[i] + c[j])

        old_dp = new_dp
    return max(old_dp[0], old_dp[1], old_dp[2])

n = int(input().rstrip())

print(holiday(n))
Problem p03160 (python)

Fixed submission:
n = int(input())

h = list(map(int,input().split()))


dp=[float('inf')]*n
dp[0]=0


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

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

dp=[float('inf')]*n
dp[0]=0

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

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

dp=[float('inf')]*n
dp[0]=0

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

Fixed submission:
n = int(input())

t_l = input().split()
l = []
for i in t_l:
    l.append(int(i))

h = [-1 for i in l]

h[0] = 0
h[1] = abs(l[0] - l[1])

i = 2
while i < len(l):
    h[i] = min(h[i-1] + abs(l[i-1] - l[i]), 
    h[i-2] + abs(l[i-2] -l[i]))
    i+=1

print(h[len(l)-1])

Buggy submission (verdict: Runtime Error):
def rec(h, place):
    if h[place] != -1:
        return h[place]

    h[place] = min(rec(h,place-1) + abs(l[place-1] - l[place]) 
    ,rec(h,place-2) + abs(l[place-2] - l[place]))

    return h[place]
n = int(input())

t_l = input().split()
l = []
for i in t_l:
    l.append(int(i))

h = [-1 for i in l]

h[0] = 0
h[1] = abs(l[0] - l[1])

rec(h,len(l)-1)

print(h[len(l)-1])
Problem p03160 (python)

Fixed submission:
N = int(input())
h_arr = list(map(int, input().split(" ")))
dp = [9999999 for _ in range(N)]
dp[0] = 0
dp[1] = abs(h_arr[1]-h_arr[0])

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

Buggy submission (verdict: Runtime Error):
N = int(input())
h_arr = list(map(int, input().split(" ")))
dp = [9999999 for _ in range(N)]
dp[0] = 0

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

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

Fixed submission:
n=int(input())
h=list(map(int,input().split()))
dp=[]
dp.append(0)
dp.append(abs(h[1]-h[0]))
for i in range(2,n):
  dp.append(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=int(input())
h=list(map(int,input().split()))
dp=[]
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
  dp.append(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=int(input())
h=list(map(int,input().split()))
dp=[]
dp[0]=0
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
  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])
Problem p03161 (python)

Fixed submission:
def main():
	N, K = map(int, input().split())
	h = list(map(int, input().split()))
	dp=[0]*N
	for i in range(1,N):
		dp[i] = min(dp[g] + abs(h[i] - h[g]) for g in range(max(i-K,0), i))
	print(dp[N-1])
main()

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

Fixed submission:
from sys import stdin,stdout


n = int(stdin.readline())
dp = [0 for i in range(n)]
a  = list(map(int,stdin.readline().split()))
dp[1] = abs(a[1] - a[0])
if(n>1):
  for i in range(2,n):
  	dp[i] = min(dp[i-1] + abs(a[i]-a[i-1]) , dp[i-2] + abs(a[i]-a[i-2]))
print(dp[-1])

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


n = int(stdin.readline())
dp = [0 for i in range(n)]
a  = list(map(int,stdin.readline().split()))
dp[1] = abs(a[1] - a[0])
if(n>1):
  for i in range(2,n):
  	dp[i] = min(dp[i-1] + abs(a[i]-a[i-1]) + dp[i-2] + abs(a[i]-a[i-2]))
print(dp[-1])
Problem p03160 (python)

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

Buggy submission (verdict: Runtime Error):
def main():
    inf = float('inf')
    n,k=map(int,input().split())
    h = list(map(int,input().split()))
    dp=[0] + [inf] *(n-1)#初期化
    for i in range(1,n):
        for j in range(1,k+1):#iからj離れた場所から来る場合と比較
            if i >=j:
                dp[i] = min(dp[i],abs(h[i-j]-h[i])+dp[i-j])
            else:break
    print(dp[n-1])
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    inf = float('inf')
    n,k=map(int,input().split())
    h = list(map(int,input().split()))
    dp=[0] + [inf] *(n-1)#初期化
    for i in range(1,n):
        for j in range(1,k+1):#iからj離れた場所から来る場合と比較
            if i >=j:
                dp[i] = min(dp[i],abs(h[i-j]-h[i])+dp[i-j])
            else:break
    print(dp[n-1])
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
inf = float('inf')
n,k=map(int,input().split())
h = list(map(int,input().split()))
dp=[0] + [inf] *(n-1)#初期化
for i in range(1,n):
    for j in range(1,k+1):#iからj離れた場所から来る場合と比較
        if i >=j:
            dp[i] = min(dp[i],abs(h[i-j]-h[i])+dp[i-j])
        else:break
print(dp[n-1])
Problem p03163 (python)

Fixed submission:
n,w=map(int,input().split())
weight,value=[0]*110,[0]*110
dp = [[0 for _ in range(100100)] for _ in range(110)]
for i in range(n):
    weight[i],value[i] = map(int,input().split())
for i in range(n):
    sum_w =0
    while sum_w <= w:
        #i番目の品物を選ぶ
        if(sum_w - weight[i] >=0):
            dp[i+1][sum_w] =max(dp[i+1][sum_w], dp[i][sum_w - weight[i]]+ value[i])
        #選ばない場合
        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
        sum_w +=1
print(dp[n][w])

Buggy submission (verdict: Runtime Error):
N,W=map(int,input().split())
w,v=[],[]
for i in range(N):
    weight, value = map(int,input().split())
    w.append(weight)
    v.append(value)
dp = [[0 for _ in range(10*9)] for _ in range(N+1)]
for i in range(N):
    for sum_w in range(W+1):
        if sum_w -w[i] >=0:
            dp[i+1][sum_w] = max(dp[i+1][sum_w],dp[i][sum_w-w[i]] + v[i])
        dp[i+1][sum_w]=max(dp[i+1][sum_w],dp[i][sum_w])
print(dp[N][W])

Buggy submission (verdict: Runtime Error):
N,W=map(int,input().split())
w,v=[],[]
for i in range(N):
    weight, value = map(int,input().split())
    w.append(weight)
    v.append(value)
dp = [[0 for _ in range(10*5*100)] for _ in range(N+1)]
for i in range(N):
    for sum_w in range(W+1):
        if sum_w -w[i] >=0:
            dp[i+1][sum_w] = max(dp[i+1][sum_w],dp[i][sum_w-w[i]] + v[i])
        dp[i+1][sum_w]=max(dp[i+1][sum_w],dp[i][sum_w])
print(dp[N][W])
Problem p04029 (python)

Fixed submission:
N = int(input())
sumof = 0

for i in range(1, N+1):
    sumof += i
print(sumof)

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int,input().split()))
 
ans = []
for i in range(-100, 101):
    total = 0
    for ai in a:
        total += (ai - i)**2
    ans.append(total)
 
print(min(ans))
Problem p04031 (python)

Fixed submission:
N = map(int, input().split())
a = list(map(int, input().split()))

ans = 9999999999

for i in range(-100, 101):
    sumof = 0
    for j in a:
        sumof += (i - j)*(i - j)
    ans = min(ans, sumof)
    
print(ans)

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

ans = 9999

for i in range(-100, 101):
    sumof = 0
    for j in a:
        sumof += (i - j)*(i - j)
    ans = min(ans, sumof)
    
print(ans)
Problem p03161 (python)

Fixed submission:
def fragOptimalJump(h,n,k):
    dp=[0]*(n+1)
    dp[2]=abs(h[1]-h[2])
    for i in range(3,n+1):
        l=i-k
        if l<0:
            l=1
        min=10000000000
        for j in range(l,i):
            d=dp[j]+abs(h[i]-h[j])
            if d<min:
                min=d
            
        dp[i]=min
    return dp[n]



N,k=input().split()
N,k=int(N),int(k)
H=[0]*(N+1)
read = [int(n) for n in input().split()]
H=[0]+read
print(fragOptimalJump(H,N,k))

Buggy submission (verdict: Runtime Error):
def fragOptimalJump(h,n):
    dp=[0]*(n+1)
    dp[2]=abs(h[1]-h[2])
    for i in range(3,n+1):
        min=10000
        p=1
        j=1
        while j<i:
            d=dp[j]+abs(h[i]-h[j])
            if d<min:
                min=d
            p=p*2
            j=p
            
        dp[i]=min
    return dp[n]

N=int(input())
H=[0]*(N+1)
read = [int(n) for n in input().split()]
H=[0]+read
print(H)
H=[0,1,2,3,4]
print(fragOptimalJump(H,4))
Problem p03162 (python)

Fixed submission:
def LIST():
    return list(map(int, input().split()))
N = int(input())
arr = [LIST() for _ in range(N)]
dp = [[arr[0][0],arr[0][1],arr[0][2]],[0,0,0]]
l=1
p=0
for i in range(1, N):
    for j in range(3):
        for k in range(3):
            if j == k: continue
            a1=dp[p][j]
            a2=arr[i][k]
            a3=a1+a2
            dp[l][k] = max(dp[l][k], dp[p][j] + arr[i][k])
    l=l^1
    p=p^1
            
print(max([dp[p][j] for j in range(3)]))

Buggy submission (verdict: Runtime Error):
def MAXI(a):
    m=max(a)
    for i in range(3):
        if a[i]==m:
            id=i
    return m,id



tot=0
N=int(input())
i=0
while i<N:
    
    if i==0:
        x=[int(a) for a in input().split()]
        m,id=MAXI(x)
        tot+=m
        i=i+1
    else:
        x=[int(a) for a in input().split()]
        y=[int(a) for a in input().split()]
        i=i+2
        MAX=0
        for j in range(3):
            if j!=id:
                for k in range(3):
                    if k!=j:
                        s=x[j]+y[k]
                        if s>MAX:
                            MAX=s
                            id1=k
                        
        tot+=MAX
        id=id1
                            
        
print(tot)
Problem p03163 (python)

Fixed submission:
def A(w, v, n,V):
    mem=[0 for i in range(V+1)]
    if n == 0 or V == 0:
        return 0
 
    for i in range(n):
        for j in range(V,w[i]-1,-1):
            m=v[i]+ mem[j - w[i]]
            if m>mem[j]:
                mem[j]=m
    return mem[V]
n,V=input().split()
n,V=int(n),int(V)
 
w=[0]*n
v=[0]*n
for i in range(n):
  x,y=input().split()
  w[i],v[i]=int(x),int(y)
 
print(A(w,v,n,V))

Buggy submission (verdict: Runtime Error):
def A(w, v, n,V):
    mem=[0 for i in range(V+1)]
    if n == 0 or V == 0:
        return 0
 
    for i in range(n):
        for j in range(V,w[i]-1,-1):
            m=v[i]+ mem[j - w[i]]
          if m>mem[j]:
            mem[j]=m
    return mem[V]
n,V=input().split()
n,V=int(n),int(V)
 
w=[0]*n
v=[0]*n
for i in range(n):
  x,y=input().split()
  w[i],v[i]=int(x),int(y)
 
print(A(w,v,n,V))

Buggy submission (verdict: Runtime Error):
def A(w, v, n,V):
    mem=[0 for i in range(V+1)]
    if n == 0 or V == 0:
        return 0

    for i in range(n):
        for j in range(V,W[i]-1,-1):
            mem[j] = max(mem[j], v[i]+ mem[j - w[i]])
    return mem[V]
n,V=input().split()
n,V=int(n),int(V)

w=[0]*n
v=[0]*n
for i in range(n):
  x,y=input().split()
  w[i],v[i]=int(x),int(y)

print(A(W,V,4,4))
Problem p03160 (python)

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

dp = [0]*n
dp[1] = abs(cost[0] - cost[1])

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

print(dp[n-1])

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

for i in range(1,N):
    dp[i] = min(abs(stones[i] - stones[j]) + dp[j] for j in range(max(0, i - K), i))

print(dp[-1])

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

for i in range(1,N):
    dp[i] = min(abs(stones[i] - stones[j]) + dp[j] for j in range(max(0, i - K), i))

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

Fixed submission:

size = int(input())
stones = [int(val) for val in input().split()]
memo = [-1] * (size + 5)

def ans(n):
	if memo[n] != -1:
		return memo[n]
	else:
		memo[n] = min(
			ans(n - 1) + abs(stones[n] - stones[n - 1]),
			ans(n - 2) + abs(stones[n] - stones[n - 2])
		)
		return memo[n]


def ans_iterative(n):
	if memo[n] != -1:
		return memo[n]
	else:
		for idx in range(2, n):
			memo[idx] = min(
				memo[idx - 1] + abs(stones[idx] - stones[idx - 1]),
				memo[idx - 2] + abs(stones[idx] - stones[idx - 2])
			)
		return memo[n - 1]


if size == 1:
	print(0)
else:
	memo[0] = 0
	memo[1] = abs(stones[0] - stones[1])
	print(ans_iterative(size))

Buggy submission (verdict: Runtime Error):

size = int(input())
stones = [int(val) for val in input().split()]
memo = [-1] * (size + 5)

def ans(n):
	if memo[n] != -1:
		return memo[n]
	else:
		memo[n] = min(
			ans(n - 1) + abs(stones[n] - stones[n - 1]),
			ans(n - 2) + abs(stones[n] - stones[n - 2])
		)
		return memo[n]

if size == 1:
	print(0)
else:
	memo[0] = 0
	memo[1] = abs(stones[0] - stones[1])
	print(ans(size - 1))

Buggy submission (verdict: Runtime Error):

size = int(input())
stones = [int(val) for val in input().split()]
memo = [-1] * size

def ans(n):
	if memo[n] != -1:
		return memo[n]
	else:
		memo[n] = min(
			ans(n - 1) + abs(stones[n] - stones[n - 1]),
			ans(n - 2) + abs(stones[n] - stones[n - 2])
		)
		return memo[n]

if size == 1:
	print(0)
else:
	memo[0] = 0
	memo[1] = abs(stones[0] - stones[1])
	print(ans(size - 1))

Buggy submission (verdict: Runtime Error):

size = int(input())
stones = [int(val) for val in input().split()]

memo = [-1] * size

memo[0] = 0
memo[1] = abs(stones[0] - stones[1])

def ans(n):
	if memo[n] != -1:
		return memo[n]
	else:
		memo[n] = min(
			ans(n - 1) + abs(stones[n] - stones[n - 1]),
			ans(n - 2) + abs(stones[n] - stones[n - 2])
		)
		return memo[n]

print(ans(size - 1))
Problem p03160 (python)

Fixed submission:
n=0
stones=[0]*n
memo=[-1]*n

def dp(pos):
  if pos==n-1:
    return 0
  if pos==n-2:
    return abs(stones[pos+1]-stones[pos])
  if memo[pos] is not -1:
    return memo[pos];
  memo[pos]=min(abs(stones[pos+1]-stones[pos])+dp(pos+1),abs(stones[pos+2]-stones[pos])+dp(pos+2))
  return memo[pos]

def dp_iterative():
  memo[n-1]=0
  if n>=2:
    memo[n-2]=abs(stones[n-1]-stones[n-2])
  for index in range(n-3,-1,-1):
    memo[index]=min(memo[index+1]+abs(stones[index+1]-stones[index]),memo[index+2]+abs(stones[index+2]-stones[index]))
  return memo[0]

if __name__=='__main__':
  n=int(input())
  stones=[0]*n
  stones=[int(val) for val in input().split()]
  memo=[-1]*n
  print(dp_iterative())

Buggy submission (verdict: Runtime Error):
n=0
stones=[0]*n
memo=[-1]*n

def dp(pos):
  if pos==n-1:
    return 0
  if pos==n-2:
    return abs(stones[pos+1]-stones[pos])
  if(memo[pos]!=-1):
    return memo[pos];
  memo[pos]=min(abs(stones[pos+1]-stones[pos])+dp(pos+1),abs(stones[pos+2]-stones[pos])+dp(pos+2))
  return memo[pos]

if __name__=='__main__':
  n=int(input())
  stones=[0]*n
  stones=[int(val) for val in input().split()]
  memo=[-1]*n
 
  print(dp(0))
Problem p03160 (python)

Fixed submission:
def solve():
  N=int(input())
  a=list(map(int,input().split()))
  dp=[float('inf') for i in range(N)]
  dp[0]=0
  for i in range(N):
  	for j in range(i+1,min(i+3,N)):
  		dp[j]=min(dp[j],dp[i]+abs(a[i]-a[j]))
  print(dp[N-1])
solve()
      
# your code goes here

Buggy submission (verdict: Runtime Error):
def solve():
  N=int(input())
  a=list(map(int,input().split()))
  dp=[float('inf') for i in range(N)]
  dp[0]=0
  for i in range(N):
    for j in range(1,3):
      if i+j<N:
	  	dp[i+j]=min(dp[i+j],dp[i]+abs(a[i]-a[i+j]))
        
  print(dp[N-1])
solve()
Problem p03437 (python)

Fixed submission:
from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
def id_generator(size=20, chars=string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
 
def mp():
    return map(int,str(raw_input()).split())
 
 
a,b=mp()
if a>=b:
    if a%b==0:
    	print -1
    else:
    	print a
else:
	if 0:
		print -1
	else:
		print a

Buggy submission (verdict: Runtime Error):
from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
def id_generator(size=20, chars=string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
 
def mp():
    return map(int,str(raw_input()).split())

a,b=mp()
if a>b:
    if gcd(a,b)==1:
    	print a
	elif a%b==0:
		print -1
	else:
		print a
else:
	if gcd(a,b)==1:
    	print b
	elif b%a==0:
		print -1
	else:
		print b
Problem p03160 (python)

Fixed submission:

n = int(input())

h_list = [int(h) for h in input().split()]

dp = [0] * n
dp[0], dp[1] = 0, abs(h_list[1] - h_list[0])

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

print(dp[n-1])

Buggy submission (verdict: Runtime Error):

n, k = map(int, input().split())
h_list = list(map(int, input().split()))

dp = [1000000] * n
dp[0] = 0

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

print(dp[n-1])