#[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 mut next_zero = std::collections::HashMap::new(); { let mut zero = None; for i in (0..n).rev() { if a[i] == 0 { zero = Some(i); } match zero { Some(j) => { next_zero.insert(i, j); } None => {} } } }; let ans = (|| { (0..n).fold(0, |acc, l| match next_zero.get(&l) { Some(j) => acc + (n - j), None => acc, }) })(); println!("{}", ans); }