29 lines
605 B
Python
29 lines
605 B
Python
import sys
|
|
sys.setrecursionlimit(10**6)
|
|
|
|
force = [
|
|
[1,2,2,2,2],
|
|
[2,1,3,4,3],
|
|
[2,3,1,3,4],
|
|
[2,4,3,1,3],
|
|
[2,3,4,3,1]
|
|
]
|
|
|
|
seq = list(map(int, input().split()))
|
|
n = len(seq)
|
|
inf = float("inf")
|
|
dp = [[[inf]*5 for _ in range(5)] for _ in range(n)]
|
|
|
|
def dfs(L, R, step) :
|
|
target = seq[step]
|
|
if target == 0 :
|
|
return 0
|
|
|
|
if dp[step][L][R] == inf :
|
|
moveLeft = dfs(target, R, step+1) + force[L][target]
|
|
moveRight = dfs(L, target, step+1) + force[R][target]
|
|
dp[step][L][R] = min([moveLeft, moveRight])
|
|
|
|
return dp[step][L][R]
|
|
|
|
print(dfs(0,0,0)) |