Buggy python 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))




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 python 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))



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 python 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))



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 python 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))


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())