// 最短路反復法 // src -> dst へflowだけ流せた時コストを返す // 負閉路はないと仮定 // todo: SPFAじゃなくてポテンシャル付ダイクストラにする const INF: i64 = 1_000_000_000_000_000_000i64 + 1; struct Graph { size: usize, edge: Vec<(usize, usize, i64, i64)>, } impl Graph { fn new(size: usize) -> Self { Graph { size: size, edge: vec![], } } fn add_edge(&mut self, src: usize, dst: usize, capa: i64, cost: i64) { assert!(src < self.size && dst < self.size && src != dst); self.edge.push((src, dst, capa, cost)); } fn solve(&self, src: usize, dst: usize) -> i64 { if src == dst { return 0; } let size = self.size; let edge = &self.edge; let mut deg = vec![0; size]; for &(a, b, _, _) in edge.iter() { deg[a] += 1; deg[b] += 1; } let mut graph: Vec<_> = deg.into_iter().map(|d| Vec::with_capacity(d)).collect(); for &(a, b, capa, cost) in edge.iter() { let x = graph[a].len(); let y = graph[b].len(); graph[a].push((b, capa, cost, y)); graph[b].push((a, 0, -cost, x)); } let mut ans = 0; let mut dp = Vec::with_capacity(size); let mut elem = Vec::with_capacity(size); let mut que = std::collections::VecDeque::new(); loop { dp.clear(); dp.resize(size, (INF, src, 0)); // コスト、親、親からの番号 dp[src] = (0, src, 0); elem.clear(); elem.resize(size, false); elem[src] = true; que.push_back(src); while let Some(v) = que.pop_front() { elem[v] = false; let (c, _, _) = dp[v]; for (i, &(u, capa, cost, _)) in graph[v].iter().enumerate() { if capa == 0 { continue; } let c = c + cost; if c < dp[u].0 { dp[u] = (c, v, i); if !elem[u] { elem[u] = true; que.push_back(u); } } } } if dp[dst].0 == INF { break; } ans += dp[dst].0; let mut pos = dst; while pos != src { let (_, parent, k) = dp[pos]; let inv = graph[parent][k].3; graph[parent][k].1 -= 1; graph[pos][inv].1 += 1; pos = parent; } } ans } } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- fn run() { input! { n: usize, s: bytes, v: [i64; n], } let mut g = Graph::new(n + n + 2 + 10); let src = n; let dst = n + 1; let mut id = n + 6; let mut gen = || { let res = id; id += 1; res }; let mut sum = ((n + 2)..(n + 2 + 4)).collect::>(); for (i, (&c, &v)) in s.iter().zip(v.iter()).enumerate().rev() { let k = match c { b'y' => 0, b'u' => 1, b'k' => 2, b'i' => 3, _ => unreachable!(), }; if k == 0 { g.add_edge(src, i, 1, -v); } else if k == 3 { g.add_edge(i, dst, 1, 0); } if k > 0 { let p = gen(); g.add_edge(p, sum[k], 2000, 0); g.add_edge(p, i, 1, -v); sum[k] = p; } if k + 1 < 4 { g.add_edge(i, sum[k + 1], 1, 0); } } let ans = -g.solve(src, dst); println!("{}", ans); } fn main() { run(); }