N=int(input())
a=list(map(int,input().split()))

def zeroing(a):
    m=max(a)
    if(m==0):
        return 0
    S=0
    n=a.index(m)
    c=a.count(m)
    if(c==1):
        a[n]-=1
        S+=1
    else:
        k=1
        while(a[n+k]==m):
            k+=1
            if(n+k+1>N):
                break
        a[n:n+k]=[m-1]*k
        S+=1
    S+=zeroing(a)
    return S

print(zeroing(a))
    
    
            
    

