#![allow(dead_code, unused_imports, unused_macros, non_snake_case)] fn main() { input! { Q: usize, Y: i64, S: [String; Q], } #[derive(Clone, Copy, Debug)] struct Term { mul: i64, add: i64, min: i64, max: i64, } fn clamp(x: i64) -> i64 { x.min(INF).max(-INF) } let mut stack = vec![]; for token in S { if token == "X" { stack.push(Term { mul: 1, add: 0, min: -INF, max: INF }) } else if token == "+" { let Term { mul: mul1, add: add1, min: min1, max: max1 } = stack.pop().unwrap(); let Term { mul: mul2, add: add2, min: min2, max: max2 } = stack.pop().unwrap(); stack.push(Term { mul: mul1 + mul2, add: clamp(add1 + add2), min: clamp(min1 + min2), max: clamp(max1 + max2) }); } else if token == "min" { let Term { mul: mul1, add: add1, min: min1, max: max1 } = stack.pop().unwrap(); let Term { mul: mul2, add: add2, min: min2, max: max2 } = stack.pop().unwrap(); stack.push(Term { mul: mul1.min(mul2), add: add1.min(add2), min: min1.min(min2), max: max1.min(max2) }); } else if token == "max" { let Term { mul: mul1, add: add1, min: min1, max: max1 } = stack.pop().unwrap(); let Term { mul: mul2, add: add2, min: min2, max: max2 } = stack.pop().unwrap(); stack.push(Term { mul: mul1.max(mul2), add: add1.max(add2), min: min1.max(min2), max: max1.max(max2) }); } else { let c = token.parse::().unwrap(); stack.push(Term { mul: 0, add: c, min: -INF, max: INF }); } } let Term { mul, add, min, max } = stack.pop().unwrap(); if (min ..= max).contains(&Y) { let x = if mul == 0 { 0 } else { (Y - add) / mul }; if x * mul + add == Y { println!("{x}"); } else { println!("-1"); } } else { println!("-1"); } } type Int = i64; const MOD: Int = 998244353; const INF: Int = 1_000_000_000_000_000_000; const YESNO: [&'static str; 2] = ["Yes", "No"]; use proconio::{input, source::line::LineSource, marker::{Usize1, Chars}}; fn yes() { println!("{}", YESNO[0]); } fn no() { println!("{}", YESNO[1]); } fn yesno(c: bool) { println!("{}", if c { YESNO[0] } else { YESNO[1] }); }