Problem p02953 (python)

Fixed submission:
n = int(input())
h = list(map(int, input().split()))


h[0] = h[0] -1
for i in range(1,n):
    if(h[i-1] < h[i]):
        h[i] = h[i]-1

flag = True
for i in range(1,n):
    if(h[i-1]>h[i]):
        flag = False

if(flag):
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

n = len(h)
dp =[[-2] * 2 for i in range(n)]


def f(index, is_sub):
    if(dp[index][is_sub] != -2):
        return dp[index][is_sub]

    v = h[index] - is_sub
    if(index == 0):
        dp[index][is_sub] = v
        return dp[index][is_sub]

    pre_v = f(index-1, 0)
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
    pre_v = f(index-1, 1)
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
 
    if(dp[index][is_sub]!=v):
        dp[index][is_sub] = -1
    
    #print(dp)
    return dp[index][is_sub]


#print(dp)
if(f(n-1,1)!=-1 or f(n-1,0)!=-1):
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

n = len(h)
dp =[[-2] * 2 for i in range(n)]


def f(index, is_sub):
    if(dp[index][is_sub] != -2):
        return dp[index][is_sub]

    v = h[index] - is_sub
    if(index == 0):
        dp[index][is_sub] = v
        #print(dp)
        return dp[index][is_sub]

    pre_v = f(index-1, 0)
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
        #print("set value",index,is_sub,pre_v,v)
    pre_v = f(index-1, 1)
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
        #print("set value",index,is_sub,pre_v,v)

    if(dp[index][is_sub]!=v):
        #print("set value",index,is_sub,"NO")
        dp[index][is_sub] = -1
    
    #print(dp)
    return dp[index][is_sub]


#print(dp)
if(f(n-1,1)!=-1 or f(n-1,0)!=-1):
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))


dp =[[-2] * 2 for i in range(n)]


def f(index, is_sub):
    if(dp[index][is_sub] != -2):
        return dp[index][is_sub]

    v = h2[index][is_sub]
    if(index == 0):
        dp[index][is_sub] = h2[index][is_sub]
        #print(dp)
        return dp[index][is_sub]

    pre_v = f(index-1, 0)
    dp[index][is_sub] = -2
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
        #print("set value",index,is_sub,pre_v,v)
    pre_v = f(index-1, 1)
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
        #print("set value",index,is_sub,pre_v,v)

    if(dp[index][is_sub]!=v):
        #print("set value",index,is_sub,"NO")
        dp[index][is_sub] = -1
    
    #print(dp)
    return dp[index][is_sub]


h2 = [[i,i-1] for i in h]
#print(dp)
if(f(n-1,1)!=-1 or f(n-1,0)!=-1):
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))


dp =[ [-2] * 2 for i in [1]*n]


def f(index, is_sub):
    if(dp[index][is_sub] != -2):
        return dp[index][is_sub]

    v = h2[index][is_sub]
    if(index == 0):
        dp[index][is_sub] = h2[index][is_sub]
        #print(dp)
        return dp[index][is_sub]

    pre_v = f(index-1, 0)
    dp[index][is_sub] = -2
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
        #print("set value",index,is_sub,pre_v,v)
    pre_v = f(index-1, 1)
    if(pre_v != -1 and pre_v <= v):
        dp[index][is_sub] = v
        #print("set value",index,is_sub,pre_v,v)

    if(dp[index][is_sub]!=v):
        #print("set value",index,is_sub,"NO")
        dp[index][is_sub] = -1
    
    #print(dp)
    return dp[index][is_sub]


h2 = [[i,i-1] for i in h]
#print(dp)
if(f(n-1,1)!=-1 or f(n-1,0)!=-1):
    print("Yes")
else:
    print("No")