import sys from collections import deque def main(): N, *rest = map(int, sys.stdin.read().split()) A = rest[:N] is_peak = [False] * N q = deque() for j in range(1, N-1): if A[j-1] < A[j] and A[j] > A[j+1]: is_peak[j] = True q.append(j) count = 0 while q: j = q.popleft() if not is_peak[j]: continue # Perform the operation m = min(A[j-1], A[j+1]) - 1 A[j] = m count += 1 is_peak[j] = False # This peak is now processed # Check j-1, j, j+1 for possible new peaks for k in [j-1, j, j+1]: if 1 <= k <= N-2: # Check if k is a peak now is_p = (A[k-1] < A[k] and A[k] > A[k+1]) if is_p: if not is_peak[k]: is_peak[k] = True q.append(k) else: if is_peak[k]: is_peak[k] = False # No longer a peak print(count) if __name__ == "__main__": main()