fn main() { input!{ n: usize, p: u128, k: usize, tb: [(usize, u128); n], } const INF : u128 = 1_000_000_000_000_000_000 + 1; // dp[i番目まで見た][k個取った] = 最大値 let mut dp = vec![vec![0u128; k+1]; n+1]; dp[0][0] = p; for i in 0..n { for j in 0..=k { if j == 0 { dp[i+1][j] = dp[i][j]; } else{ if tb[i].0 == 1 { dp[i+1][j] = max(dp[i][j], dp[i][j-1] + tb[i].1); } else{ dp[i+1][j] = max(dp[i][j], dp[i][j-1] * 2); } } dp[i+1][j] = min(dp[i+1][j], INF); } } if dp[n][k] == INF { println!("-1"); } else { println!("{}", dp[n][k]); } } // ---------- 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, };