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 queue = deque() for j in range(1, N-1): if A[j-1] < A[j] and A[j] > A[j+1]: is_peak[j] = True queue.append(j) count = 0 while queue: j = queue.popleft() if not is_peak[j]: continue left_val = A[j-1] right_val = A[j+1] new_val = min(left_val, right_val) - 1 A[j] = new_val count += 1 is_peak[j] = False for neighbor in [j-1, j+1]: if neighbor < 1 or neighbor >= N-1: continue if A[neighbor-1] < A[neighbor] and A[neighbor] > A[neighbor+1]: if not is_peak[neighbor]: is_peak[neighbor] = True queue.append(neighbor) else: if is_peak[neighbor]: is_peak[neighbor] = False print(count) if __name__ == "__main__": main()