use std::io::{self, BufRead, Write}; use std::str::FromStr; use std::fmt; struct Io1(T); impl FromStr for Io1 { type Err = String; fn from_str(s: &str) -> Result { Ok(Io1(s.trim_end().parse().ok().unwrap())) } } struct Io2(T1, T2); impl FromStr for Io2 { type Err = String; fn from_str(s: &str) -> Result { let mut iter = s.split_whitespace(); Ok(Io2( iter.next().unwrap().parse().ok().unwrap(), iter.next().unwrap().parse().ok().unwrap(), )) } } struct Io3(T1, T2, T3); impl FromStr for Io3 where T1: FromStr, T2: FromStr, T3: FromStr { type Err = String; fn from_str(s: &str) -> Result { let mut iter = s.split_whitespace(); Ok(Io3( iter.next().unwrap().parse().ok().unwrap(), iter.next().unwrap().parse().ok().unwrap(), iter.next().unwrap().parse().ok().unwrap(), )) } } struct Ios(Vec); impl std::str::FromStr for Ios { type Err = String; fn from_str(s: &str) -> Result { Ok(Ios(s.chars().collect())) } } impl fmt::Display for Ios { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0.iter().collect::()) } } struct Iov(Vec); impl FromStr for Iov { type Err = String; fn from_str(s: &str) -> Result { let iter = s.split_whitespace(); Ok(Iov(iter.map(|x| x.parse().ok().unwrap()).collect())) } } impl fmt::Display for Iov { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0.iter().map(|x| x.to_string()) .collect::>().join(" ") ) } } struct Solver<'a> { reader: io::BufReader>, writer: io::BufWriter>, } impl Solver<'_> { fn read(&mut self) -> T { let mut input = String::new(); self.reader.read_line(&mut input).unwrap(); input.parse().ok().unwrap() } fn writeln(&mut self, ans: T) { writeln!(self.writer, "{}", ans).unwrap(); } // 1 2 3 5 8 13 21 34 55 89 144 fn solve(&mut self) { use std::collections::BTreeMap; use std::iter::FromIterator; let Io1(n): Io1 = self.read(); let Iov(a): Iov = self.read(); let mut ans: i64 = n as i64; let mut prev = 0; let mut map = BTreeMap::from_iter(a.into_iter().map(|x| (x, 1))); while map.len() > 1 { let (&k1, &v1) = map.iter().next().unwrap(); map.remove(&k1); let (&k2, &v2) = map.iter().next().unwrap(); if prev > 0 { *map.entry(k1 + 1).or_insert(0) += prev; } if k2 - k1 == 1 { let v = v1 * v2; ans += v; prev = v; } else { prev = 0; } } self.writeln(ans); } fn run() { let (stdin, stdout) = (io::stdin(), io::stdout()); let reader = io::BufReader::new(stdin.lock()); let writer = io::BufWriter::new(stdout.lock()); let mut solver = Solver { reader, writer }; solver.solve(); } } fn main() { Solver::run(); }