import sys from collections import deque def main(): N, *rest = map(int, sys.stdin.read().split()) A = rest[:N] if N < 3: print(0) return q = deque() in_queue = set() for i in range(N - 2): if A[i] < A[i + 1] and A[i + 1] > A[i + 2]: q.append(i) in_queue.add(i) count = 0 while q: i = q.popleft() in_queue.discard(i) if i < 0 or i >= N - 2: continue if A[i] < A[i + 1] and A[i + 1] > A[i + 2]: new_val = min(A[i], A[i + 2]) - 1 A[i + 1] = new_val count += 1 if i - 1 >= 0 and (i - 1) not in in_queue: if A[i - 1] < A[i] and A[i] > A[i + 1]: q.append(i - 1) in_queue.add(i - 1) if i + 1 <= N - 3 and (i + 1) not in in_queue: if A[i + 1] < A[i + 2] and A[i + 2] > A[i + 3]: q.append(i + 1) in_queue.add(i + 1) print(count) if __name__ == "__main__": main()