Problem p03160 (python)

Fixed submission:
# N = 4
# HRR = [10, 30, 40, 20]
# N = 2
# HRR = [10, 10]

N = int(input())
HRR = list(map(int,input().split()))
result = []
for i in range(N):
    if i == 0:
        result.append(0)
    elif i == 1:
        s1 = result[i - 1] + abs(HRR[i] - HRR[i - 1])
        result.append(s1)
    else:
        s1 = result[i - 1] + abs(HRR[i] - HRR[i - 1])
        s2 = result[i - 2] + abs(HRR[i] - HRR[i - 2])
        result.append(min(s1, s2))

print(result[-1])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
# N = 4
# ARR = [10, 30, 40, 20]

N = int(input())
ARR = list(map(int, input().split()))

def calculate(n, arr):
    result = aaa(0, n - 1, arr)
    print(result)


def aaa(currentPosition, endPosition, arr, cost=0):
    if currentPosition == endPosition:
        return cost
    if currentPosition == (endPosition - 1):
        return aaa(endPosition, endPosition, arr, cost + abs(arr[endPosition] - arr[currentPosition]))

    a1 = abs(arr[currentPosition + 1] - arr[currentPosition])
    a2 = abs(arr[currentPosition + 2] - arr[currentPosition])

    s1 = aaa(currentPosition + 1, endPosition, arr, cost + a1)
    s2 = aaa(currentPosition + 2, endPosition, arr, cost + a2)
    return min(s1, s2)





calculate(N, ARR)

Buggy submission (verdict: Runtime Error):
# N = 4
# ARR = [10, 30, 40, 20]

N = int(input())
ARR = list(map(int, input().split()))

def calculate(n, arr):
    result = aaa(0, n - 1, arr)
    print(result)


def aaa(currentPosition, endPosition, arr, cost=0):
    if currentPosition == endPosition:
        return cost
    if currentPosition == (endPosition - 1):
        return aaa(endPosition, endPosition, arr, cost + abs(arr[endPosition] - arr[currentPosition]))

    a1 = abs(arr[currentPosition + 1] - arr[currentPosition])
    a2 = abs(arr[currentPosition + 2] - arr[currentPosition])

    s1 = aaa(currentPosition + 1, endPosition, arr, cost + a1)
    s2 = aaa(currentPosition + 2, endPosition, arr, cost + a2)
    return min(s1, s2)





calculate(N, ARR)

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

# N = 4
# HRR = [10, 30, 40, 20]
# # N = 2
# HRR = [10, 10]

N = int(input())
HRR = list(map(int,input().split()))
result = []


def calculate(pos, n, hrr, cost):
    if pos > n - 1:
        return
    if pos == n-1:
        result.append(int(cost))
        return

    if pos + 1 <= n - 1:
        calculate(pos + 1, n, hrr, cost + math.fabs(hrr[pos + 1] - hrr[pos]))

    if pos + 2 <= n - 1:
        calculate(pos + 2, n, hrr, cost + math.fabs(hrr[pos + 2] - hrr[pos]))


calculate(0, N, HRR, 0)
print(min(result))

Buggy submission (verdict: Runtime Error):
import math

# N = 4
# HRR = [10, 30, 40, 20]
# N = 2
# HRR = [10, 10]

N = int(input())
HRR = list(map(int,input().split()))
result = []


def calculate(pos, n, hrr, cost):
    if pos == n-1:
        result.append(int(cost))

    if pos + 1 <= n-1:
        calculate(pos + 1, n, hrr, cost + math.fabs(hrr[pos + 1] - hrr[pos]))

    if pos + 2 <= n-1:
        calculate(pos + 2, n, hrr, cost + math.fabs(hrr[pos + 2] - hrr[pos]))


calculate(0, N, HRR, 0)
print(min(result))