import sys from collections import deque def main(): n = int(sys.stdin.readline()) arr = list(map(int, sys.stdin.readline().split())) if n < 3: print(0) return queue = deque() in_queue = [False] * n # Initialize queue with initial peaks for j in range(1, n-1): if arr[j-1] < arr[j] and arr[j] > arr[j+1]: queue.append(j) in_queue[j] = True count = 0 while queue: j = queue.popleft() in_queue[j] = False # Check if current j is still a peak if j < 1 or j >= n-1: continue if not (arr[j-1] < arr[j] and arr[j] > arr[j+1]): continue # Perform the operation count += 1 new_val = min(arr[j-1], arr[j+1]) - 1 arr[j] = new_val # Check neighboring positions and add them to the queue if necessary for k in [j-1, j, j+1]: if 1 <= k < n-1: if not in_queue[k]: queue.append(k) in_queue[k] = True print(count) if __name__ == "__main__": main()