#[macro_export] macro_rules! setup { { mut $input:ident: SplitWhitespace $(,)? } => { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut $input = buf.split_whitespace(); }; } #[macro_export] macro_rules! parse_next { ($str_iter:expr) => { $str_iter.next().unwrap().parse().ok().unwrap() }; } fn main() { setup! { mut input: SplitWhitespace }; let n: usize = parse_next!(input); let a: Vec = (0..n).map(|_| parse_next!(input)).collect(); let zeros = a .iter() .enumerate() .filter(|(_, x)| **x == 0) .map(|(i, _)| i) .collect::>(); let mut next_zero = std::collections::HashMap::new(); { zeros.iter().for_each(|&j| { (0..=j).for_each(|i| { if !next_zero.contains_key(&i) { next_zero.insert(i, j); } }) }); }; let ans = (|| { (0..n).fold(0, |acc, l| match next_zero.get(&l) { Some(i) => acc + (n - i), None => acc, }) })(); println!("{}", ans); }