from heapq import heappush,heappop n = int(input()) A = list(map(int,input().split())) num = A[:] done = [0]*n h = [] for i,a in enumerate(A): heappush(h,[-a,i]) ans = 0 while h: c,ind = heappop(h) # print(c,ind) c = -c if ind == 0 or ind == n-1: done[ind] = 1 num[ind] = c continue if done[ind-1] == done[ind+1] == 0: ans += 1 c = min(num[ind-1],num[ind+1])-1 num[ind] = c heappush(h,[-c,ind]) else: done[ind] = 1 print(ans)