import sys input = sys.stdin.readline from heapq import heappop,heappush from operator import itemgetter N=int(input()) A=list(map(int,input().split())) H=[] for i in range(N): H.append((-A[i],i)) H.sort(key=itemgetter(0)) ANS=0 while H: w,ind=heappop(H) if 1<=indA[ind-1] and A[ind]>A[ind+1]: A[ind]=min(A[ind-1],A[ind+1])-1 ANS+=1 heappush(H,(-A[ind],ind)) print(ANS)