#include <stdio.h>
#include <math.h>
using namespace std;

int a[200000];
int b[200000];

void qsort(int l, int r){
   int i = l;
   int j = r;
   int p = a[ b[ (l + r) / 2 ]];
   while ( i <= j ){
     while (a[b[i]] > p) i++;
     while (a[b[j]] < p) j--;
     if ( i <= j ){
        if (a[b[i]] != a[b[j]]){
          int t = b[i];
          b[i] = b[j];
          b[j] = t;
        }
        i++;
        j--;
     }
   }
   if ( i < r )
     qsort (i,r);
   if ( l < j )
     qsort (l,j);
}
void printarray(int n){
    for (int i = 0; i < n; i++)
     printf("%d %d\n",a[b[i]], b[i]);
}
bool step(int i, int t,int n){
     bool f = false;
     while(a[i] == t && i < n){
         a[i]--;
         f = true;
         i++;
       }
     return f;
}
int main(int argc, char *argv[]){
    int n;
    scanf("%d",&n);

    for (int i = 0; i < n; i++){
      scanf("%d",a + i);
    }
    for (int i = 0; i < n; i++)
      b[i] = i;
    int max = a[0];
    int imax = 0;
    for (int i = 0; i < n; i++)
      if (a[i] > max){
        max = a[i];
        imax = i;
      }
      int steps = 0;
    while (a[imax] != 0){
       int i = 0;
       int t = a[imax];
       while ( i < n ){
          while (a[i] != t && i < n)
            i++;
          bool f = step(i,t,n);
          if(f) steps++;
       }
    }
            /*
    
    
    while (a[b[0]] != 0){
       int y = 0;
       int t = a[b[0]];
       printf("%d",t);
       while ( y < n ){
        int i = b[y];
        
       
        bool f = step(i,t,n);
        if (f)
          steps++;
        y++;
       }
       
    }
    */
    printf("%d\n",steps);
    
    return 0;
}
