use proconio::input; use proconio::fastout; use std::cmp::{min, max}; #[fastout] #[allow(non_snake_case)] fn main() { input! { (Q, Y): (usize, isize), s: [String; Q], } assert!(1 <= Q && Q <= 200_000); assert!(0 <= Y && Y <= 100_000_000_000_000); for i in 0..Q { match s[i].parse::() { Ok(x) => {assert!(0 <= x && x <= 1_000_000_000)}, Err(_) => {assert!(["X", "+", "min", "max"].contains(&s[i].as_str()))}, }; } let mut ng = -1; let mut ok = Y; for _ in 0..100 { let mid = (ok + ng) / 2; let mut stc = Vec::new(); for i in 0..Q { if s[i] == "X" { stc.push(mid); } else if s[i] == "+" { let x = stc.pop().unwrap(); let y = stc.pop().unwrap(); stc.push(x + y); } else if s[i] == "min" { let x = stc.pop().unwrap(); let y = stc.pop().unwrap(); stc.push(min(x, y)); } else if s[i] == "max" { let x = stc.pop().unwrap(); let y = stc.pop().unwrap(); stc.push(max(x, y)); } else { stc.push(s[i].parse::().unwrap()); } } assert!(stc.len() == 1); if stc[0] >= Y { ok = mid; } else { ng = mid; } } let mut stc = Vec::new(); for i in 0..Q { if s[i] == "X" { stc.push(ok); } else if s[i] == "+" { let x = stc.pop().unwrap(); let y = stc.pop().unwrap(); stc.push(x + y); } else if s[i] == "min" { let x = stc.pop().unwrap(); let y = stc.pop().unwrap(); stc.push(min(x, y)); } else if s[i] == "max" { let x = stc.pop().unwrap(); let y = stc.pop().unwrap(); stc.push(max(x, y)); } else { stc.push(s[i].parse::().unwrap()); } } if stc[0] != Y { println!("-1"); } else { println!("{}", ok); } }