import sys from collections import deque def main(): input = sys.stdin.read().split() N = int(input[0]) A = list(map(int, input[1:N+1])) if N < 3: print(0) return dq = deque() # Identify initial peaks for j in range(1, N-1): if A[j-1] < A[j] and A[j] > A[j+1]: dq.append(j) operations = 0 while dq: j = dq.popleft() # Check if j is still a peak if j <= 0 or j >= N-1: continue if not (A[j-1] < A[j] and A[j] > A[j+1]): continue # Process the peak new_val = min(A[j-1], A[j+1]) - 1 A[j] = new_val operations += 1 # Check left and right neighbors for k in [j-1, j+1]: if 1 <= k <= N-2: if A[k-1] < A[k] and A[k] > A[k+1]: dq.append(k) print(operations) if __name__ == "__main__": main()