#![allow(unused_macros, unused_imports)] use std::collections::BTreeSet; macro_rules! dbg { ($($xs:expr),+) => { if cfg!(debug_assertions) { std::dbg!($($xs),+) } else { ($($xs),+) } } } fn main() { input!{ n: usize, p: [Usize1; n], } let mut s = (0..n).collect::>(); let mut ans = 0; for i in (0..n).rev() { if let Some(&x) = s.range(p[i]+1..).next() { s.remove(&x); ans += i as i64 + 1; } else if s.contains(&p[i]) { s.remove(&p[i]); } else { let x = *s.iter().next().unwrap(); s.remove(&x); ans -= i as i64 +1; } } println!("{}", ans); } pub mod proconio { #[macro_export] macro_rules! input{ (sc=$sc:expr,$($r:tt)*)=>{ input_inner!{$sc,$($r)*} }; ($($r:tt)*)=>{ let mut sc=proconio::Scanner::new(std::io::stdin().lock()); input_inner!{sc,$($r)*} }; } #[macro_export] macro_rules! input_inner{ ($sc:expr)=>{}; ($sc:expr,)=>{}; ($sc:expr,$var:ident:$t:tt$($r:tt)*)=>{ let $var=read_value!($sc,$t); input_inner!{$sc $($r)*} }; } #[macro_export] macro_rules! read_value{ ($sc:expr,($($t:tt),*))=>{ ($(read_value!($sc,$t)),*) }; ($sc:expr,[$t:tt;$len:expr])=>{ (0..$len).map(|_|read_value!($sc,$t)).collect::>() }; ($sc:expr,Chars)=>{read_value!($sc,String).chars().collect::>()}; ($sc:expr,Usize1)=>{read_value!($sc,usize)-1}; ($sc:expr,$t:ty)=>{$sc.next::<$t>()}; } pub struct Scanner { s: Box, input: std::iter::Peekable>, } impl Scanner { pub fn new(mut reader: R) -> Self { let s = { let mut s = String::new(); reader.read_to_string(&mut s).unwrap(); s.into_boxed_str() }; let mut sc = Scanner { s, input: "".split_ascii_whitespace().peekable(), }; use std::mem; let s: &'static str = unsafe { mem::transmute(&*sc.s) }; sc.input = s.split_ascii_whitespace().peekable(); sc } #[inline] pub fn next(&mut self) -> T where T::Err: std::fmt::Debug, { self.input .next() .unwrap() .parse::() .expect("Parse error") } } }