fn main() { input!{ n: usize, x: usize, y: usize, pc: [(i64, char); n], } // x+yのグループに分けて、abの希望人数を記録 const INF: i64 = 1 << 60; let mut cnt = vec![vec![0; x+y]; 2]; for i in 0..n { let p = i % (x+y); if pc[i].1 == 'A' { cnt[0][p] += pc[i].0;} else { cnt[1][p] += pc[i].0;} } // dp[iグループ見た][xを選んだ個数j] let xy = min(x+y, n); let mut dp = vec![vec![-INF; x+1]; xy+1]; dp[0][0] = 0; for i in 0..xy { for j in 0..=x { // xを選ぶ if j+1 <= x { dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + cnt[0][i]); } // yを選ぶ dp[i+1][j] = max(dp[i+1][j], dp[i][j] + cnt[1][i]); } } println!("{}", max(0, dp[xy][x])); } // ---------- begin input macro ---------- #[macro_export] 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_export] 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_export] 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 ---------- #[allow(unused)] use std::{ io::stderr, io::stdin, io::BufRead, io::Write, mem::swap, cmp::min, cmp::max, cmp::Reverse, collections::HashSet, collections::BTreeSet, collections::HashMap, collections::BTreeMap, collections::BinaryHeap, collections::VecDeque, };