import sys from collections import deque def main(): n = int(sys.stdin.readline()) A = list(map(int, sys.stdin.readline().split())) if n < 3: print(0) return is_peak = [False] * n q = deque() for i in range(1, n-1): if A[i-1] < A[i] and A[i] > A[i+1]: is_peak[i] = True q.append(i) count = 0 while q: i = q.popleft() if not is_peak[i]: continue # Perform the operation min_val = min(A[i-1], A[i+1]) A[i] = min_val - 1 count += 1 is_peak[i] = False # Current position is no longer a peak # Check surrounding positions for new peaks for j in [i-1, i, i+1]: if j < 1 or j >= n-1: continue # Check if j is a peak now if A[j-1] < A[j] and A[j] > A[j+1]: if not is_peak[j]: is_peak[j] = True q.append(j) else: if is_peak[j]: is_peak[j] = False # In case it was marked as peak before print(count) if __name__ == "__main__": main()