use std::collections::VecDeque; fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let mut a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut result = 0usize; let mut deque = VecDeque::new(); for i in 1..n-1 { if a[i-1] < a[i] && a[i] > a[i+1] { deque.push_back(i); } } while let Some(pos) = deque.pop_front() { result += 1; a[pos] = a[pos-1].min(a[pos+1]) - 1; if pos > 1 && a[pos-1] > a[pos-2] && a[pos-1] > a[pos] { deque.push_back(pos-1); } if pos < n-2 && a[pos+1] > a[pos+2] && a[pos+1] > a[pos] { deque.push_back(pos+1); } } println!("{}", result); }