#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    long long n, mas[10000], c = 0;
    cin >> n;
    mas [0] = 53590549;
    for (int i = 1; i <= n; i++)
         cin >> mas[i];
    bool boo = false;
    while (boo == false)
    for (int i = 1; i <= n; i++)
    {
        int j = i;
        while(i == 1 and mas [i] > mas[i+1])    
        {
                mas[i] --;
                c++;
        }
        if (mas[i+1] >= mas[i-1])
        {
            continue;
        }
        else
        {
         while (mas [i] > mas[i-1])
         {
               mas[i]--;
               c++;
         }
        while (mas [i] > mas[i+1])
        {
           while (j >= 1)
           {
                 mas [j] --;
                 j--;
           }
                 c++;
                   j = i;
        }
        }
        if (mas [i] == 0)
           boo = true;
    }
    cout << c;       
    return 0;
}


