2025-12-31 22:01:12 +09:00

42 lines
1.0 KiB
Java

import java.util.*;
public class _2342_1 {
static int[][][] dp;
static int[] inst;
static int inst_num;
static int[][] move_energy = {
{1,2,2,2,2},
{0,1,3,4,3},
{0,3,1,3,4},
{0,4,3,1,3},
{0,3,4,3,1}
};
static int solve(int L, int R, int step) {
if(step == inst_num) return 0;
if(dp[step][L][R] == 0) {
int target = inst[step];
int moveLeft = solve(target, R, step+1) + move_energy[L][target];
int moveRight = solve(L, target, step+1) + move_energy[R][target];
dp[step][L][R] = Math.min(moveLeft, moveRight);
}
return dp[step][L][R];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
inst = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
sc.close();
inst_num = inst.length-1;
dp = new int[inst_num][5][5];
System.out.println(solve(0, 0, 0));
}
}