#include<iostream>;
using namespace std;
int main(){
	int n,i,sum,max,p,z,k,m,pr,max1,max2;
	sum=0;
	max=0;
	p=1;
	cin>>n;
	int a[n];
	for(i=0;i<n;i++){
		cin>>a[i];
	}
	while(p>0){
	    for(i=0;i<n;i++){
		    if((a[i]>a[i+1]) and (i<(n-1))){
			    max=i;
		    } else{
		    	if((a[i]<a[i+1]) and (i<(n-1))){
		    		max=i+1;
				}
			}
	    }
//	    for(pr=0;pr<n;pr++){
//	    	cout<<a[pr]<<" ";
//		}
//	    cout<<max<<endl;
	    m=1;
	    z=a[max];
//	    cout<<z<<endl;
	    if(z==0){
	    	cout<<sum<<endl;
	    	return 0;
		}
	    a[max]=a[max]-1;
	    sum=sum+1;
	    k=1;
	    max1=max;
	    max2=max;
	    while(m>0){
	    	if((z==a[max1-1]) and (max1>0) and (z>0) and (k>0)){
	    		a[max1-1]=a[max1-1]-1;
	    		max1=max1-1;
			} else{
				k=0;
			}
			if((z==a[max2+1]) and (max2<n-1) and (z>0)){
	    		a[max2+1]=a[max2+1]-1;
	    		max2=max2+1;
			} else{
				if(k==0){
					m=0;
				}
			}
		}
    }
}
