Problem p03160 (python)

Fixed submission:

n = int(input())
temp = input()
h = temp.split()
for i,item in enumerate(h):
    h[i] = int(item)
dp = [0]
for i,height in enumerate(h):
    if i >=2:
        step1 = dp[i - 1] + abs(height - h[i-1])
        step2 = dp[i-2] + abs(height - h[i-2])
        dp.append(step1 if step1 <= step2 else step2)
    elif i == 1:
        dp.append(abs(height - h[i-1]))

print(dp.pop())

Buggy submission (verdict: Runtime Error):

n = int(input())
temp = input()
h = temp.split()
for i,item in enumerate(h):
    h[i] = int(item)
dp = [0]

def rec_flog(i):
    if len(dp) > i:
        return dp[i]
    elif i >= 2:
        step1 = abs(h[i] - h[i - 1]) + rec_flog(i-1)
        step2 = abs(h[i] - h[i -2]) + rec_flog(i-2)
        dp.append(step1 if step1 <= step2 else step2)
        return dp[i]
    else :
        dp.append(h[1] - h[0])
        return dp[1]


print(rec_flog(n-1))

Buggy submission (verdict: Runtime Error):

n = int(input())
temp = input()
h = temp.split()
for i,item in enumerate(h):
    h[i] = int(item)
dp = [0]

def rec_flog(i):
    if len(dp) > i:
        return dp[i]
    elif i >= 2:
        step1 = abs(h[i] - h[i - 1]) + rec_flog(i-1)
        step2 = abs(h[i] - h[i -2]) + rec_flog(i-2)
        dp.append(step1 if step1 <= step2 else step2)
        return dp[i]
    else :
        dp.append(h[1] - h[0])
        return h[1] - h[0]


print(rec_flog(n-1))

Buggy submission (verdict: Runtime Error):

n = int(input())
temp = input()
h = temp.split()

for i,item in enumerate(h):
    h[i] = int(item)

dp = []
def rec_flog(i):
    print(dp)
    if len(dp) > i:
        return dp[i]
    elif i >= 2:
        step1 = abs(h[i] - h[i - 1]) + rec_flog(i-1)
        step2 = abs(h[i] - h[i -2]) + rec_flog(i-2)
        dp.append(step1 if step1 <= step2 else step2)
        return dp[i]
    else :
        dp.append(h[1])
        return h[1]


print(rec_flog(6))

Buggy submission (verdict: Runtime Error):

n,l = input().split()
n = int(n)
l = int(l)
temp = input()
h = temp.split()
for i,item in enumerate(h):
    h[i] = int(item)

dp = [0]
for i,height in enumerate(h):
    temp = max(h)
    start = i-l if i-l >= 0 else 0
    for k in range(start,i):
        temp_step = abs(height - h[k])
        step = temp if temp < temp_step else temp_step + dp[k]
        temp = temp_step

    if i != 0:
        dp.append(step)


print(dp.pop())

Buggy submission (verdict: Runtime Error):
n = int(input())
temp = input()
h = temp.split()
for i,item in enumerate(h):
    h[i] = int(item)
dp = [0]
print(dp)
for i,height in enumerate(h):
    if i >=2:
        step1 = dp[i - 1] + abs(height - h[i-1])
        step2 = dp[i-2] + abs(height - h[i-2])
        dp.append(step1 if step1 <= step2 else step2)
    elif i == 1:
        dp.append(abs(height - h[i-1]))

    print(dp)

print(dp)

Buggy submission (verdict: Runtime Error):

n = int(input())
temp = input()
h = temp.split()
for i,item in enumerate(h):
    h[i] = int(item)
dp = [0]
print(dp)
for i,height in enumerate(h):
    if i >=2:
        step1 = dp[i - 1] + abs(height - h[i-1])
        step2 = dp[i-2] + abs(height - h[i-2])
        dp.append(step1 if step1 <= step2 else step2)
    elif i == 1:
        dp.append(abs(height - h[i-1]))

    print(dp)

print(dp)