Problem p03160 (python)

Fixed submission:
n = int(input())
arr = [int(i) for i in list(input().split())]
def helper(arr):
	if len(arr) <= 1:
		return 0
	if len(arr) == 2:
		return abs(arr[1] - arr[0])
	way1 = abs(arr[-1] - arr[-2]) + helper(arr[:-1])
	way2 = abs(arr[-1] - arr[-3]) + helper(arr[:-2])
	return min(way1, way2 )

if n == 1:
	print(0)
elif n == 2:
	print(abs(arr[1] - arr[0]))
else:
	dp = [0]*n
	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] - arr[i-1]), dp[i-2] + abs(arr[i] - arr[i-2]))
	print(dp[n-1])
# print(helper(arr))

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = [int(i) for i in list(input().split())]
dp = [-1]*n
def helper(arr, end):
	global dp
	if end <= 0:
		return 0 
	if end == 1:
		return abs(arr[1] - arr[0])
	if dp[end] != -1:
		return dp[end]
	way1 = abs(arr[end] - arr[end-1]) + helper(arr, end - 1)
	way2 = abs(arr[end] - arr[end-2]) + helper(arr, end - 2)
	dp[end] = min(way1, way2)
	return dp[end]

print(helper(arr, n - 1 ))

# def helper(arr):
# 	if len(arr) <= 1:
# 		return 0
# 	if len(arr) == 2:
# 		return abs(arr[1] - arr[0])
# 	way1 = abs(arr[-1] - arr[-2]) + helper(arr[:-1])
# 	way2 = abs(arr[-1] - arr[-3]) + helper(arr[:-2])
# 	return min(way1, way2 )



# if n == 1:
# 	print(0)
# elif n == 2:
# 	print(abs(arr[1] - arr[0]))
# else:
# 	dp = [0]*n
# 	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] - arr[i-1]), dp[i-2] + abs(arr[i] - arr[i-2]))
# 	print(dp[n-1])
# print(helper(arr))

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = [int(i) for i in list(input().split())]
def helper(arr, end):
	if end <= 0:
		return 0 
	if end == 1:
		return abs(arr[1] - arr[0])
	way1 = abs(arr[end] - arr[end-1]) + helper(arr, end - 1)
	way2 = abs(arr[end] - arr[end-2]) + helper(arr, end - 2)
	return min(way1, way2)

print(helper(arr, n - 1 ))

# def helper(arr):
# 	if len(arr) <= 1:
# 		return 0
# 	if len(arr) == 2:
# 		return abs(arr[1] - arr[0])
# 	way1 = abs(arr[-1] - arr[-2]) + helper(arr[:-1])
# 	way2 = abs(arr[-1] - arr[-3]) + helper(arr[:-2])
# 	return min(way1, way2 )



# if n == 1:
# 	print(0)
# elif n == 2:
# 	print(abs(arr[1] - arr[0]))
# else:
# 	dp = [0]*n
# 	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] - arr[i-1]), dp[i-2] + abs(arr[i] - arr[i-2]))
# 	print(dp[n-1])
# print(helper(arr))

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = [int(i) for i in list(input().split())]
def helper(arr):
	if len(arr) <= 1:
		return 0
	if len(arr) == 2:
		return abs(arr[1] - arr[0])
	way1 = abs(arr[-1] - arr[-2]) + helper(arr[:-1])
	way2 = abs(arr[-1] - arr[-3]) + helper(arr[:-2])
	return min(way1, way2 )

if n == 1:
	print(0)
elif n == 2:
	print(abs(arr[1] - arr[0]))
else:
	dp[0] = 0
	dp[1] = abs(arr[1] - arr[0])
	dp = [0]*n
	for i in range(2,len(arr)):
		dp[i] = min(dp[i-1] + abs(arr[i] - arr[i-1]), dp[i-2] + abs(arr[i] - arr[i-2]))
	print(dp[n-1])
# print(helper(arr))

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = [int(i) for i in list(input().split())]
def helper(arr):
	if len(arr) <= 1:
		return 0
	if len(arr) == 2:
		return abs(arr[1] - arr[0])
	way1 = abs(arr[-1] - arr[-2]) + helper(arr[:-1])
	way2 = abs(arr[-1] - arr[-3]) + helper(arr[:-2])
	return min(way1, way2 )

# if n == 1:
# 	print(0)
# elif n == 2:
# 	print(abs(arr[1] - arr[0]))
# else:
# 	dp = [0]*n
# 	for i in range(2,len(arr)):
# 		dp[i] = min(dp[i-1] + abs(arr[i] - arr[i-1]), dp[i-2] + abs(arr[i] - arr[i-2]))

# 	print(dp[n-1])
print(helper(arr))

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = [int(i) for i in list(input().split())]
def helper(arr):
	if len(arr) <= 1:
		return 0
	if len(arr) == 2:
		return abs(arr[1] - arr[0])
	way1 = abs(arr[-1] - arr[-2]) + helper(arr[:-1])
	way2 = abs(arr[-1] - arr[-3]) + helper(arr[:-2])
	return min(way1, way2 )
print(helper(arr))