#![allow(unused_imports)] #![allow(unused_macros)] #![allow(non_snake_case)] use proconio::marker::{Bytes, Chars, Isize1, Usize1}; use proconio::{fastout, input, input_interactive}; #[fastout] fn main() { input! { N: usize, Q: usize, A: [i64; N], mut B: [i64; Q], } B.dedup(); let B = B; let mut dp = vec![usize::MAX; N]; dp[0] = 0; let pos_per_b = { let mut ret = vec![vec![]; 1_001]; for i in 0..N { ret[A[i] as usize].push(i); } ret }; for b in B { let mut new_dp = vec![usize::MAX; N]; for from in 0..N { if dp[from] == usize::MAX { continue; } for to in pos_per_b[b as usize].iter() { debug!(from, to, dp[from]); chmin!(new_dp[*to], dp[from] + to.abs_diff(from)); } } dp = new_dp; } let ans = dp.iter().min().unwrap(); println!("{}", ans); } #[macro_export] macro_rules! mat { ($($e:expr),*) => { vec![$($e),*] }; ($($e:expr,)*) => { vec![$($e),*] }; ($e:expr; $d:expr) => { vec![$e; $d] }; ($e:expr; $d:expr $(; $ds:expr)+) => { vec![mat![$e $(; $ds)*]; $d] }; } #[macro_export] macro_rules! chmin { ($a:expr, $b:expr) => { if $a > $b { $a = $b; true } else { false } }; } #[macro_export] macro_rules! chmax { ($a:expr, $b:expr) => { if $a < $b { $a = $b; true } else { false } }; } /// https://maguro.dev/debug-macro/ #[macro_export] macro_rules! debug { ($($a:expr),* $(,)*) => { #[cfg(debug_assertions)] eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*); }; }