use std::cmp::{min, max}; use proconio::input; fn rev_polish(s: &Vec, x: i64) -> i64 { let mut stack = Vec::::new(); for token in s.iter() { match token.as_str() { "+" => { let a = stack.pop().unwrap(); let b = stack.pop().unwrap(); stack.push(a + b); }, "min" => { let a = stack.pop().unwrap(); let b = stack.pop().unwrap(); stack.push(min(a, b)); }, "max" => { let a = stack.pop().unwrap(); let b = stack.pop().unwrap(); stack.push(max(a, b)); }, "X" => { stack.push(x); }, _ => { stack.push(token.parse::().unwrap()); } } } stack.pop().unwrap() } fn main() { input! { n: usize, y: i64, s: [String; n], } let mut ok: i64 = y; let mut ng: i64 = -1; while ok - ng > 1 { let mid = (ok + ng) / 2; if rev_polish(&s, mid) >= y { ok = mid; } else { ng = mid; } } if rev_polish(&s, ok) == y { println!("{}", ok); } else { println!("-1"); } }