Problem p03160 (python)

Fixed submission:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

n = int(input())
h = [0] + list(map(int, input().split()))

INF = 10**15

dp = [INF]*(n+5)
dp[0] = 0
dp[1] = 0
dp[2] = abs(h[2]-h[1])

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

print(dp[n])

Buggy submission (verdict: Runtime Error):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

N, W = list(map(int, input().split()))
INF = 10**10

dp = [[0]*(W+1) for i in range(N+1)]

for i in range(1,N+1):
    w, v = list(map(int, input().split()))
    for j in range(1,W+1):
        if j-w < 0:
            p = -INF
        else:
            p = dp[i-1][j - w]

        dp[i][j] = max(p + v, dp[i-1][j])

print(dp[N][W])

Buggy submission (verdict: Runtime Error):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

N = int(input())
h = [0] + list(map(int, input().split()))+[0]

@functools.lru_cache(None)
def cost(n):
    if n == 1:
        return 0
    elif n == 2:
        return abs(h[2]-h[1])
    else:
        return min([cost(n-1) + abs(h[n]-h[n-1]), cost(n-2) + abs(h[n]-h[n-2])])

print(cost(N))

Buggy submission (verdict: Runtime Error):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

N = int(input())
h = [0] + list(map(int, input().split()))+[0]

@functools.lru_cache(None)
def cost(n):
    if n == 1:
        return 0
    elif n == 2:
        return(abs(h[2]-h[1]))
    else:
        ans = min([cost(n-1) + abs(h[n]-h[n-1]), cost(n-2) + abs(h[n]-h[n-2])])
        return ans

print(cost(N))

Buggy submission (verdict: Runtime Error):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

n = int(input())
h = [0] + list(map(int, input().split()))+[0]

INF = 10**15

dp = [INF]*(n+5)
dp[0] = 0
dp[1] = 0

@functools.lru_cache(None)
def cost(n):
    if n == 1:
        return 0
    elif n == 2:
        return(abs(h[2]-h[1]))
    else:
        ans = min([cost(n-1) + abs(h[n]-h[n-1]), cost(n-2) + abs(h[n]-h[n-2])])
        return ans

print(cost(n))

Buggy submission (verdict: Runtime Error):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq

n = int(input())
h = [0] + list(map(int, input().split()))+[0]

INF = 10**15

dp = [INF]*(n+5)
dp[0] = 0
dp[1] = 0

@functools.lru_cache(None)
def cost(n):
    if n == 1:
        return 0
    elif n == 2:
        return(abs(h[2]-h[1]))
    else:
        a = cost(n-1)
        ans = a + abs(h[n]-h[n-1])

        a = cost(n-2)
        ans = min([ans, a + abs(h[n]-h[n-2])])

        return ans

print(cost(n))