Problem p03160 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
dp = [0 for _ in range(len(A))]
dp[0] = 0 
for i in range(1,N):
    if i - 2>=0:
        dp[i] = min(dp[i-1] + abs(A[i] - A[i-1]),dp[i-2] + abs(A[i] - A[i-2]))
    else:
        dp[i] =dp[i-1] + abs(A[i] - A[i-1])

print(dp[len(A)-1])

Buggy submission (verdict: Runtime Error):
from functools import lru_cache
import sys 
sys.setrecursionlimit(2147483647)

N = int(input())
A = []
for i in range(0,N):
    A.append(list(map(int,input().split())))


#@lru_cache(None)
def dfs(d,pre):
    if (d,pre) in dic: return dic[(d,pre)]
    if d>=N-1: return 0
    ma = 0 
    for idx,a in enumerate(A[d]):
        if idx != pre:
            res = dfs(d + 1,idx) + a 
            ma = max(res,ma)
    dic[(d,pre)] = ma
    return ma
dic = {}
res = dfs(-1,-1)
print(res)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
from functools import lru_cache
import sys
sys.setrecursionlimit(10**5+10) 

@lru_cache(None)
def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
from functools import lru_cache
import sys
sys.setrecursionlimit(100000) 

@lru_cache(None)
def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
from functools import lru_cache
import sys
sys.setrecursionlimit(10**5+10) 

@lru_cache(None)
def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
from functools import lru_cache
import sys
sys.setrecursionlimit(10**5+10) 

@lru_cache(None)
def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
from functools import lru_cache

@lru_cache(None)
import sys
sys.setrecursionlimit(10**5+10) 
def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
 import sys
sys.setrecursionlimit(10**5+10) 
def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

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

def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1: 
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

if N > 2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

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

def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    res1 = up(i + 1,i)
    if i + 2 <=len(A) -1:
        res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1,abs(A[pre] - A[i]) + res2)

print(min(up(1,0),up(2,0)))

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

def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = float("inf")
    if i + 2 <=len(A) -1: 
        res1 = up(i + 1,i)
        res2 = up(i + 2,i)
        return min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2)
    elif i + 1 <=len(A) -1: 
        res1 = up(i + 1,i)
        return abs(A[pre] - A[i]) + res1
  
if N>2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

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

def up(i,pre):
    if i == len(A) - 1:
        return abs(A[pre] - A[i])
    res1 = res2 = 0 
    if i + 2 <=len(A) -1: 
        res1 = up(i + 1,i)
        res2 = up(i + 2,i)
        return min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2)
    elif  i + 1 <=len(A) -1: 
        res1 = up(i + 1,i)
        return abs(A[pre] - A[i]) + res1
  
if N>2:
    print(min(up(1,0),up(2,0)))
else:
    print(up(1,0))

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

def up(i,pre):
  if i == len(A) - 1:
    return abs(A[pre] - A[i])
  res1 = res2 = 0
  if i + 2 <=len(A) -1:
    res1 = up(i + 1,i)
    res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2)
  elif  i + 1 <=len(A) -1:
    res1 = up(i + 1,i)
    return abs(A[pre] - A[i]) + res1
  else: return 0
  
if N>2:
 print(min(up(1,0),up(2,0)))
else:
  print(up(1,0))

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

def up(i,pre):
  if i == len(A) - 1:
    return abs(A[pre] - A[i])
  res1 = res2 = 0
  if i + 2 <=len(A) -1:
    res1 = up(i + 1,i)
    res2 = up(i + 2,i)
    return min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2)
  elif  i + 1 <=len(A) -1:
    res1 = up(i + 1,i)
    return abs(A[pre] - A[i]) + res1
  
if N>2:
 print(min(up(1,0),up(2,0)))
else:
  print(up(1,0))

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

def up(i,pre):
  if i == len(A) - 1:
    return abs(A[pre] - A[i])
  res1 = res2 = 0
  if i + 2 <=len(A) -1:
    res1 = up(i + 1,i)
    res2 = up(i + 2,i)
   # print(min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2))
    return min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2)
  elif  i + 1 <=len(A) -1:
    res1 = up(i + 1,i)
    #print(abs(A[pre] - A[i]) + res1)
    return abs(A[pre] - A[i]) + res1
  
if N>2:
 print(min(up(1,0),up(2,0)))
else:
  print(up(1,0))

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

def up(i,pre):
  if i == len(A) - 1:
    return abs(A[pre] - A[i])
  res1 = res2 = 0
  if i + 2 <=len(A) -1:
    res1 = up(i + 1,i)
    res2 = up(i + 2,i)
   # print(min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2))
    return min(abs(A[pre] - A[i]) + res1, abs(A[pre] - A[i]) + res2)
  elif  i + 1 <=len(A) -1:
    res1 = up(i + 1,i)
    #print(abs(A[pre] - A[i]) + res1)
    return abs(A[pre] - A[i]) + res1
  
if N>=2:
 print(min(up(1,0),up(2,0)))
else:
  print(up(1,0))