#include <iostream>

using namespace std;

int n, a[200009];

long long this_segment(int l, int r){
    if( l == r-1)
        return a[l];
    if( l == r )
        return 0;
    int mn = a[l], b[r-l+5], k = 1;
    b[0] = l;
    for(int i = l+1; i < r; i++){
        if( a[i] == mn){
            b[k] = i;
            k++;
        }
        if( a[i] < mn){
            k = 1;
            b[0] = i;
            mn = a[i];
        }
    }
    for( int i = l; i < r; i++){
        a[i] -= mn;
    }
    long long res = mn;
    if( a[l] != 0)
        res += this_segment(l, b[0]);
    for( int i = 0; i < k-1; i++){
        res += this_segment(b[i] +1, b[i+1]);
    }
    res += this_segment(b[k-1] + 1, r);
    return res;
}


int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    long long res = this_segment(0, n);
    cout << res;
    return 0;
}
