Problem p03160 (python)

Fixed submission:
inf = float("inf") 
n=int(input())
a=list(map(int ,input().split()))
dp=[inf]*n
k=2
dp[0]=0
for i in range(1,n):
   for j in range(i-1,max(0,i-k)-1,-1):
       dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]))
print(dp[n-1])

Buggy submission (verdict: Runtime Error):
def dp(idx,f,a,cache):
    if idx==len(a):
        return 0
    if cache[idx][f]!=-1:
        return cache[idx][f]
    ans=0
    for i in range(3):
        if i==f:
            continue
        ans=max(ans,dp(idx+1,i,a,cache))
    ans+=a[idx][f]
    cache[idx][f]=ans
    return ans
a=[]
n=int(input())
for i in range(n):
    a.append(list(map(int,input().split())))
cache=[[-1,-1,-1] for i in range(n)]
print(max(dp(0,0,a,cache),dp(0,1,a,cache),dp(0,2,a,cache)))

Buggy submission (verdict: Runtime Error):
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
    Date:11/06/2020

'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

def solve():
    
    # for _ in range(ii()):
        
    n=ii()
    a=li()
    dp=[inf]*n
    k=2
    dp[0]=0
    for i in range(1,min(n,k)):
        dp[i]=abs(a[i]-a[0])
    for i in range(1,n):
        for j in range(i-1,max(0,i-k)-1,-1):
            dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]))
    print(dp[n-1])
    
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
        
        
        
if __name__ =="__main__":
    solve()

Buggy submission (verdict: Runtime Error):
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
    Date:11/06/2020

'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

def solve():
    
    # for _ in range(ii()):
        
    n=ii()
    global a
    a=li()
    inf=1000000007
    dp=[inf]*n
    k=2
    dp[0]=0
    for i in range(1,min(n,k)):
        dp[i]=abs(a[i]-a[0])
    for i in range(1,n):
        for j in range(i-1,max(0,i-k)-1,-1):
            dp[i]=min(dp[i],dp[j]+abs(a[i]-a[j]))
    print(dp[n-1])
    
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
        
        
        
if __name__ =="__main__":
    solve()

Buggy submission (verdict: Runtime Error):
'''
    Auther: ghoshashis545 Ashis Ghosh
    college: jalpaiguri Govt Enggineering College
    Date:12/03/2020
'''

s=input()
t=input()
Mxn=3005
dp=[["" for i in range(Mxn)] for j in range(Mxn)] 
n=len(s)
m=len(t)
for i in range(n+1):
    for j in range(m+1):
        if(i==0 or j==0):
            dp[i][j]=""
        elif(s[i-1]==t[j-1]):
            dp[i][j]=dp[i-1][j-1]+s[i-1]
        else:
            if(len(dp[i][j-1])<len(dp[i-1][j])):
                dp[i][j]=dp[i-1][j-1]
            else:
                dp[i][j]=dp[i][j-1]
print(dp[n][m])