#include <iostream>
#include "stdlib.h"
using namespace std;

int main()
{
   int N;
   cin>> N;
   int a[N];
int counter=0;
   for(int i=0; i<N; i++)
   {
       cin>>a[i];

   }
   for(int j=1; j<N; j++)
   {
   int index=1;
   for(int i=0; i<N; i++)
   {
       if(a[index]<a[i])
        index=i;
   }
   if(index<N-1)
   while (a[index]>a[index+1] && a[index]>a[index-1])
   {
       a[index]--;
       counter++;
   }
   else
    while (a[index]>a[index-1])
   {
       a[index]--;
       counter++;
   }
   }
   if(N=3 && a[0]==a[1] && a[1]==a[2])
    counter=counter+a[1];
    else
        counter=counter+a[0];


   cout<<counter;
    return 0;
}
