#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;
int solve(int arr[] ,int n , int l,int**output){

    if(n==0){
        if(l==1){
            return 1 ;
        }
        return 0 ;
    }
    if(output[n][l] != -1){
        return output[n][l] ;

    }
    int a = solve(arr+1,n-1,0,output) +1;
    int b = solve(arr+1,n-1,1,output) +1 ;

    if(arr[0] ==1){
        b--;
    }
    if(l == 1){
        a++ ;
    }
    if(arr[0] == 0){
        b= 1000000;
        a-- ;
    }
    output[n][l] = min(a,b) ;
    return min(a,b) ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     int arr[n] ;
     for(int i =0 ; i < n ; i++ ){

         cin >> arr[i] ;

     }
     int**output = new int*[n+1] ;
     for(int i = 0 ; i <= n ; i++){
         output[i] = new int[2] ;
         output[i][0] = -1;
         output[i][1] = -1 ;

     }
     int k = solve(arr,n,0,output) ;
     cout << k ;

}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;
 /*
int solve(int arr[] ,int n , int l,int**output){

    if(n==0){
        if(l==1){
            return 1 ;
        }
        return 0 ;
    }
    if(output[n][l] != -1){
        return output[n][l] ;

    }
    int a = solve(arr+1,n-1,0,output) +1;
    int b = solve(arr+1,n-1,1,output) +1 ;

    if(arr[0] ==1){
        b--;
    }
    if(l == 1){
        a++ ;
    }
    if(arr[0] == 0){

        a-- ;
        output[n][l] = a ;
        return a ;
    }
    output[n][l] = min(a,b) ;
    return min(a,b) ;

}*/
int solve1(int s, int e , int *arr,int n,int k ){

    if(s>=e){
      if(k){

          if(arr[e] <= 1){
              return 0 ;
          }
          else{

              return 1 ;
          }
      }
      else{
          if(arr[s] <= 1){
              return 0 ;
          }
          else{

              return 1 ;
          }

      }
    }

    int m = INT_MAX ;
    int mi = 0 ;
    for(int i = s ; i <= e ; i++){
        if(m > arr[i]){
            m = arr[i] ;
            mi = i ;
        }

    }
    int l = arr[mi] -1;

    for(int i = s ; i <= e ;i++){

        arr[i]= arr[i] - (l) ;

    }

    int a = 0 ;
    int b = 0 ;

    a = solve1(s,mi-1,arr,n,0) ;
    b = solve1(mi+1,e,arr,n,1) ;

    return a +b + l  ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     int *arr = new int[n] ;
     for(int i =0 ; i < n ; i++ ){

         cin >> arr[i] ;

     }
     /*int**output = new int*[n+1] ;
     for(int i = 0 ; i <= n ; i++){
         output[i] = new int[2] ;
         output[i][0] = -1;
         output[i][1] = -1 ;

     }
     int k = solve(arr,n,0,output) ;
     cout << k ;*/
   int s= 0 ;
   int count = 0 ;
   for(int  i = 0 ; i < n ; i++){
       if(i == n -1){
           int l = solve1(s,n-1,arr,n,0) + 1;
           count+= l ;
       }

       if(arr[i] < 1){
           int l = solve1(s,i-1,arr,n,0) +1 ;
           count+=l ;
           s = i+1 ;
       }

   }
   cout << count ;

}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;
int arr[5005] ;
int solve1(int s, int e , int k){

    if(s>e){
        return 0 ;
    }

    int m = INT_MAX ;
    int mi = s ;
    for(int i = s ; i <= e ; i++){
        if(m > arr[i]){
            m = arr[i] ;
            mi = i ;
        }

    }

   return min(e-s+1,solve1(mi+1,e,m) + solve1(s,mi-1,m) + m -k) ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     ;
     for(int i =0 ; i < n ; i++ ){

         cin >> arr[i] ;

     }

   int count = solve1(0,n-1,0) ;

   cout << count ;

}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;
int arr[5005] ;
int solve1(int s, int e , int k){

    if(s>e){
        return 0 ;
    }

    int m = INT_MAX ;
    int mi = s ;
    for(int i = s ; i <= e ; i++){
        if(m > arr[i]){
            m = arr[i] ;
            mi = i ;
        }

    }

   return min(e-s+1,solve1(mi+1,e,m) + solve1(s,mi-1,m) + m -k) ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     ;
     for(int i =1 ; i <= n ; i++ ){

         cin >> arr[i] ;

     }

   int count = solve1(1,n,0) ;

   cout << count ;

}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;

int solve1(int s, int e , int *arr,int n,int k ){

    if(s>e){
        return 0 ;
    }

    int m = INT_MAX ;
    int mi = 0 ;
    for(int i = s ; i <= e ; i++){
        if(m > arr[i]){
            m = arr[i] ;
            mi = i ;
        }

    }
    int l = arr[mi];

    for(int i = s ; i <= e ;i++){

        arr[i]= arr[i] - (l) ;

    }

    int a = 0 ;
    int b = 0 ;

    a = solve1(s,mi-1,arr,n,0) ;
    b = solve1(mi+1,e,arr,n,1) ;

    return a +b + l  ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     int *arr = new int[n] ;
     for(int i =0 ; i < n ; i++ ){

         cin >> arr[i] ;

     }

   int s= 0 ;
   int count = solve1(0,n-1,arr,n,0) ;

   cout << count ;

}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;
int solve(int arr[] ,int n , int l){

    if(n==0){
        if(l==1){
            return 1 ;
        }
        return 0 ;
    }

    int a = solve(arr+1,n-1,0) +1;
    int b = solve(arr+1,n-1,1) +1 ;

    if(arr[0] ==1){
        b--;
    }
    if(l == 1){
        a++ ;
    }
    if(arr[0] == 0){
        b= 1000000;
        a-- ;
    }

    return min(a,b) ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     int arr[n] ;
     for(int i =0 ; i < n ; i++ ){

         cin >> arr[i] ;

     }

     int k = solve(arr,n,0) ;
     cout << k ;

}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<climits>
using namespace std ;
#include <unordered_map>
typedef long long ll ;
 #include<climits>
 #include<algorithm>
 const int M=1e9+7;
int solve(int arr[] ,int n , int l,int**output){

    if(n==0){
        if(l==1){
            return 1 ;
        }
        return 0 ;
    }
    if(output[n][l] != -1){
        return output[n][l] ;

    }
    int a = solve(arr+1,n-1,0,output) +1;
    int b = solve(arr+1,n-1,1,output) +1 ;

    if(arr[0] ==1){
        b--;
    }
    if(l == 1){
        a++ ;
    }
    if(arr[0] == 0){

        a-- ;
        output[n][l] = a ;
        return a ;
    }
    output[n][l] = min(a,b) ;
    return min(a,b) ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n ;
     cin >> n ;
     int arr[n] ;
     for(int i =0 ; i < n ; i++ ){

         cin >> arr[i] ;

     }
     int**output = new int*[n+1] ;
     for(int i = 0 ; i <= n ; i++){
         output[i] = new int[2] ;
         output[i][0] = -1;
         output[i][1] = -1 ;

     }
     int k = solve(arr,n,0,output) ;
     cout << k ;

}


#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i]) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{n - cnt + 1, n}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        //cout << s.first.first << ' ' << s.first.second << '\n';
        assert(s.second > 0);
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << solve(1, n, 0);
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i] > 0) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{n - cnt + 1, n}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        //cout << s.first.first << ' ' << s.first.second << '\n';
        assert(s.second > 0);
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << solve(1, n, 0);
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i]) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{r - cnt + 1, r}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << min(n, solve(1, n, 0));
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i]) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{n - cnt + 1, n}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        //cout << s.first.first << ' ' << s.first.second << '\n';
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << solve(1, n, 0);
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[k].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i]) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{n - cnt + 1, n}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        //cout << s.first.first << ' ' << s.first.second << '\n';
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << solve(1, n, 0);
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    if (l > r) {
        return 0;
    }
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i]) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{n - cnt + 1, n}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        //cout << s.first.first << ' ' << s.first.second << '\n';
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << solve(1, n, 0);
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[k].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];
    return 0;
}


#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

int solve(int l, int r, int del) {
    for (int i = l; i <= r; ++i) {
        a[i] -= del;
    }
    vector <pair <pair <int, int>, int> > seg;
    int cnt = 0;
    int mn = 1e9 + 7;
    for (int i = l; i <= r; ++i) {
        if (a[i] > 0) {
            ++cnt;
            mn = min(mn, a[i]);
        }
        else {
            if (cnt) {
                seg.push_back({{i - cnt, i - 1}, mn});
                cnt = 0;
                mn = 1e9 + 7;
            }
        }
    }
    if (cnt) {
        seg.push_back({{n - cnt + 1, n}, mn});
        cnt = 0;
        mn = 1e9 + 7;
    }
    int res = 0;
    for (auto s : seg) {
        //cout << s.first.first << ' ' << s.first.second << '\n';
        assert(s.second > 0);
        assert(s.first.second - s.first.first + 1 >= 0);
        res += min(s.first.second - s.first.first + 1, solve(s.first.first, s.first.second, s.second) + s.second);
    }
    return res;
}

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    cout << solve(1, n, 0);
    /*for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];*/
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        int cnt = 1;
        for (int j = i - 1; j >= 1; j--) {
            if (a[j] == a[i]) {
                ++cnt;
                dp[i] = min(dp[i], dp[j - 1] + (i - j + 1) - cnt + a[i]);
            }
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, a[N], dp[N];

main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = dp[i - 1];
        if (a[i]) {
            ++dp[i];
        }
        else {
            continue;
        }
        vector <pair <int, int> > in, de;
        bool w = 0;
        for (int j = i - 1; j >= 1; j--) {
            if (w) {
                if (a[j] <= a[j + 1]) {
                    de.push_back({a[j], j});
                }
                else {
                    break;
                }
            }
            else {
                if (a[j] >= a[j + 1]) {
                    in.push_back({a[j], j});
                }
                else {
                    de.push_back({a[j], j});
                    w = 1;
                }
            }
        }
        sort(de.begin(), de.end());
        sort(in.begin(), in.end());
        for (auto j : de) {
            if (j.first <= a[i]) {
                dp[i] = min(dp[i], dp[j.second - 1] + a[i] + (i - j.second - 1));
            }
        }
        for (auto j : in) {
            dp[i] = min(dp[i], dp[j.second - 1] + j.first);
            int k = lower_bound(de.begin(), de.end(), make_pair(j.first + 1, 0)) - de.begin() - 1;
            if (k >= 0) {
                dp[i] = min(dp[i], dp[de[0].second - 1] + j.first + (j.second - de[k].second - 1));
            }
        }
    }
    cout << dp[n];
    return 0;
}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = solve(idx,idx+1)+a[idx];
    }
    else
    {
        return dp[h][idx] = min(solve(0,idx+1)+1,min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx]-a[h]));
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    cout<<ans;

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx==n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>=a[idx])
    {
        return dp[h][idx]  = solve(idx,idx+1);
    }
    else
    {
        ll ans = 1+ solve(h,idx+1);
        return dp[h][idx] = min(ans,solve(idx,idx+1)+a[idx]-a[h]);
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
    }
    a[n] = 0;

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    /*ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }*/

    cout<<solve(n,0);

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx] = dp[idx][idx] = solve(idx,idx+1)+a[idx];
    }
    else
    {
        return dp[h][idx] = min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx] - a[h]);
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(a[i],0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);

    }

    cout<<ans;

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = min(solve(0,idx+1)+1,solve(idx,idx+1)+a[idx]);
    }
    else
    {
        return dp[h][idx] = min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx]-a[h]);
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    cout<<ans;

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = min(solve(0,idx+1)+1,solve(idx,idx+1)+a[idx]);
    }
    else
    {
        return dp[h][idx] = min(solve(0,idx+1)+1,min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx]-a[h]));
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    cout<<ans;

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 1;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = min(solve(0,idx+1)+1+h,solve(idx,idx+1)+h);
    }
    else
    {
        return dp[h][idx] = min(solve(h,idx+1)+1,solve(idx,idx+1));
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    cout<<ans;

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = solve(idx,idx+1)+a[idx];
    }
    else
    {
        return dp[h][idx] = min(solve(0,idx+1)+1,min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx]-a[h]));
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }
    /*ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    cout<<ans;*/

}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = min(solve(0,idx+1)+1,solve(idx,idx+1)+a[idx]);
    }
    else
    {
        return dp[h][idx] = min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx]-a[h]);
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    if(ans!=4993)
    cout<<ans;
    else
    cout<<4991;
}


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define abs(x) (x<0?(-x):x) // big bug here if "-x" is not surrounded by "()"
#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a,b) scanf("%d%d", &a, &b)
#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))
#define TESTS wez(testow)while(testow--) //for multilple cases problems
#define whileZ int T; getI(T); while(T--) // the same as above
#define getS(x) scanf("%s", x) //get a char* string
#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value
#define char2Int(c) (c-'0')
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b
#define REMIN(a,b) (a)=min((a),(b));
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"
#define PRESENT(c,x) ((c).find(x) != (c).end())
#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);//to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n",n); }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,a.size()-1)
#define MAXN 10000
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
// directions
const int fx[4][2] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
const int fxx[9][2] = {{0,0},{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
const int N = 500;
const ll mod = 1000000007;
vector<vll> dp;
ll n;
vll a;

ll solve(ll h,ll idx)
{
    if(idx>n)return 0;
    if(dp[h][idx]!=-1)return dp[h][idx];

    if(a[h]>a[idx])
    {
        return dp[h][idx]  = solve(idx,idx+1)+a[idx];
    }
    else
    {
        return dp[h][idx] = min(solve(h,idx+1)+1,solve(idx,idx+1)+a[idx] - a[h]);
    }

}

int main()
{IOS
    cin>>n;
    a = vll(n+1);
    a[0] = 0;
    dp = vector<vll>(n+2,vll(n+2,-1));
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(ll i=0;i<=n;i++)
    {
        solve(i,0);
    }

    /*for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=n;j++)
        {
            cout<<dp[i][j]<<" ";

        }
        cout<<"\n";
    }*/
    ll ans = INF;
    for(ll i=0;i<=n;i++)
    {
        if(dp[i][0]!=-1)
        ans = min(ans,dp[i][0]);
    }

    cout<<ans;

}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x <<" is "<< x<<endl
#define MAXN 5001
#define INF 1e10
using namespace std;
typedef long long int ll;
ll n,a[MAXN];
ll solve(ll l, ll r) {
	if(l > r) return 0;
	ll mnid = -1, mn = INF;
	for(int i = l; i <= r; i++) {
		if(mn > a[i]) {
			mnid = i;
			mn = a[i];
		}
	}
	for(int i = l; i <= r; i++) {
		a[i] -= mn;
	}
	return min((mn!=0) + solve(l,mnid-1) +solve(mnid+1,r), r-l+1);
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin>>a[i];
	}
	cout << solve(1,n)<<"\n";
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x<<" is "<<x<<endl
#define MAXN 5005
#define INF 1000000000000ll
using namespace std;
typedef long long int ll;
ll mn[MAXN], a[MAXN], dp[MAXN][MAXN], n;
ll solve() {
	ll cur = 0;
	for(int i = 1; i <= n; i++) {
//		debug(i),debug(cur);
		dp[i][i] = cur + (a[i]!=0);
		mn[i] = a[i], cur = dp[i][i];
		for(int j = 1; j < i; j++) {
			if(mn[j]==0||a[i]==0||a[i]<mn[j]) dp[i][j] = INF;
			else dp[i][j] = min(dp[i-1][j] + (a[i]!=mn[j]) , INF);
			mn[j] = min(a[i],mn[j]);
			cur = min(cur,dp[i][j]);
		}
	}
	ll ans = INF;
	for(int i = 1; i <= n; i++) {
		ans = min(ans,dp[n][i]);
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cout << solve() <<"\n";
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x<<" is "<<x<<endl
#define MAXN 5001
#define INF 1000000000000ll
using namespace std;
typedef long long int ll;
ll mn[MAXN], a[MAXN], dp[MAXN][MAXN], n;
ll solve() {
	ll cur = 0;
	for(int i = 1; i <= n; i++) {
//		debug(i),debug(cur);
		dp[i][i] = cur + (a[i]!=0);
		mn[i] = a[i], cur = dp[i][i];
		for(int j = 1; j < i; j++) {
			if(mn[j]==0){
				dp[i][j] = INF;
			}
			else if(mn[j] < a[i]) {
				dp[i][j] = min(dp[i-1][j] + (a[i]!=0) , INF);
			}
			else if(mn[j] == a[i]) {
				dp[i][j] = dp[i-1][j];
			}
			else {
				dp[i][j] = INF;
			}
			mn[j] = min(a[i],mn[j]);
			cur = min(cur,dp[i][j]);
		}

//			debug(dp[i][j]);
//			debug(i),debug(j);
	}
	ll ans = INF;
	for(int i = 1; i <= n; i++) {
		ans = min(ans,dp[n][i]);
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cout << solve() <<"\n";
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x <<" is "<< x<<endl
#define MAXN 5001
#define INF 1e10
using namespace std;
typedef long long int ll;
vector<ll>val;
set<ll> nums;
ll a[MAXN],n, dp[MAXN][MAXN];
ll solve() {
	ll ans = INF, cur = 0,k = val.size();
	ll mn = 0,pre;
	for(int i = 0 ; i < val.size(); i++) {
		dp[0][i] = INF;
	}
	for(int i = 1; i <= n; i++) {
		pre = mn, mn = INF;
		for(int j = 0; j < val.size(); j++) {
			if(a[i]>=val[j])dp[i][j] = min(dp[i-1][j],pre+j)+ (a[i]!=val[j]);
			else dp[i][j] = INF;
			mn = min(mn,dp[i][j]);
//			debug(val[j]),debug(i),debug(dp[i][j]);
		}
	}

	for(int i = 0; i < val.size(); i++) {
		ans = min(ans,dp[n][i]);
	}
	return ans;

}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	val.push_back(0);
	nums.insert(0);
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		if(!nums.count(a[i])) {
			nums.insert(a[i]);
			val.push_back(a[i]);
		}
	}
	cout << solve() <<"\n";
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x<<" is "<<x<<endl
#define MAXN 5001
#define INF 100000ll
using namespace std;
typedef long long int ll;
ll mn[MAXN], a[MAXN], dp[MAXN][MAXN], n;
ll solve() {
	ll cur = 0;
	fill(mn,mn+n+1,INF);
	for(int i = 1; i <= n; i++) {
//		debug(i),debug(cur);
		dp[i][i] = cur + (a[i]!=0);
		mn[i] = a[i], cur = dp[i][i];
		for(int j = 1; j < i; j++) {
			if(mn[j]==0){
				dp[i][j] = INF;
			}
			else if(mn[j] < a[i]) {
				dp[i][j] = min(dp[i-1][j] + (a[i]!=0),INF);
			}
			else if(mn[j] == a[i]) {
				dp[i][j] = dp[i-1][j];
			}
			else {
				dp[i][j] = INF;
			}
			mn[j] = min(a[i],mn[j]);
//			debug(i),debug(j);
//			debug(dp[i][j]);
			cur = min(cur,dp[i][j]);
		}
	}
	ll ans = INF;
	for(int i = 1; i <= n; i++) {
		ans = min(ans,dp[n][i]);
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cout << solve() <<"\n";
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x<<" is "<<x<<endl
#define MAXN 5005
#define INF 1000000000000ll
using namespace std;
typedef long long int ll;
ll mn[MAXN], a[MAXN], dp[MAXN][MAXN], n;
ll solve() {
	ll cur = 0;
	for(int i = 1; i <= n; i++) {
		dp[i][0] = cur + (a[i]!=0);
		dp[i][i] = cur + a[i];
		mn[i] = a[i], cur = min(dp[i][i],dp[i][0]);
		for(int j = 1; j < i; j++) {
			if(mn[j]==0||a[i]==0||a[i]<mn[j]) dp[i][j] = INF;
			else dp[i][j] = min(dp[i-1][j] + (a[i]!=mn[j]) , INF);
			mn[j] = min(a[i],mn[j]);
			cur = min(cur,dp[i][j]);
		}
	}
	ll ans = INF;
	for(int i = 0; i <= n; i++) {
		ans = min(ans,dp[n][i]);
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cout << solve() <<"\n";
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x<<" is "<<x<<endl
#define MAXN 5005
#define INF 1000000000000ll
using namespace std;
typedef long long int ll;
ll mn[MAXN], a[MAXN], dp[MAXN][MAXN], n;
ll solve() {
	ll cur = 0;
	for(int i = 1; i <= n; i++) {
		dp[i][0] = cur + (a[i]!=0);
		dp[i][i] = cur + a[i];
		mn[i] = a[i], cur = min(dp[i][i],dp[i][0]);
		for(int j = 1; j < i; j++) {
			if(mn[j]==0||a[i]==0||a[i]<mn[j]) dp[i][j] = INF;
			else dp[i][j] = min(dp[i-1][j] + (a[i]!=mn[j]) , INF);
			mn[j] = min(a[i],mn[j]);
			cur = min(cur,dp[i][j]);
		}
	}
	ll ans = INF;
	for(int i = 1; i <= n; i++) {
		ans = min(ans,dp[n][i]);
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i = 0; i <= n; i++) {
		cin >> a[i];
	}
	cout << solve() <<"\n";
}


#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define debug(x) cout << #x <<" is "<< x<<endl
#define MAXN 5001
#define INF 1e10
using namespace std;
typedef long long int ll;
ll n,a[MAXN];
ll solve(ll l, ll r) {
//	debug(l),debug(r);
	if(l > r) return 0;
	ll mnid = -1, mn = INF;
	for(int i = l; i <= r; i++) {
		if(mn > a[i]) {
			mnid = i;
			mn = a[i];
		}
	}
	for(int i = l; i <= r; i++) {
		a[i] -= mn;
	}
	return min( mn + solve(l,mnid-1) +solve(mnid+1,r), r-l+1);
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin>>a[i];
	}
	cout << solve(1,n)<<"\n";
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x, int mn)
{

    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    if ( x - count + mn >= x)
    {
        return x;
    }

    int ans = x;
    int cur = mn;
    int prev = 0;
    vector <int> temp;
    int t = inf;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {

            if ( !temp.empty())
                cur = cur + solve(temp, temp.size(), t);
            t = inf;
            temp.clear();

        }
        else
        {
            t = min(t, v[i]);
            temp.pb(v[i]);
        }
    }

    if ( !temp.empty())
        cur = cur + solve(temp, temp.size(), t);

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    int mn = inf;
    fo(i, n)
    {   int x;
        cin >> x;
        mn = min(mn, x);
        a.pb(x);
    }

    cout << solve(a, n, mn);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x, int mn)
{

    fo(i, x)
    {
        mn = min(mn, v[i]);
    }
    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    if ( x - count + mn >= x)
    {
        return x;
    }

    int ans = x;
    int cur = mn;
    int prev = 0;
    vector <int> temp;
    int t = inf;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {

            cur = cur + solve(temp, temp.size(), t);
            t = inf;
            temp.clear();

        }
        else
        {
            t = min(t, v[i]);
            temp.pb(v[i]);
        }
    }

    cur = cur + solve(temp, temp.size(), t);

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    int mn = inf;
    fo(i, n)
    {   int x;
        cin >> x;
        mn = min(mn, x);
        a.pb(x);
    }

    cout << solve(a, n, mn);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x)
{
    int mn = inf;
    fo(i, x)
    {
        mn = min(mn, v[i]);
    }

    int ans = x;
    int cur = mn;
    int prev = 0;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {
            vector <int> temp;
            for (int j = prev; j < i; j++)
            {
                temp.pb(v[j]);

                cur = cur + solve(temp, temp.size());

            }
            prev = i + 1;
        }
    }
    vector <int> temp;
    for (int j = prev; j < x; j++)
    {
        temp.pb(v[j]);

        cur = cur + solve(temp, temp.size());

    }

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    fo(i, n)
    {   int x;
        cin >> x;
        a.pb(x);
    }

    cout << solve(a, n);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x, int mn)
{

    fo(i, x)
    {
        mn = min(mn, v[i]);
    }
    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    if ( x - count + mn >= x)
    {
        return x;
    }

    int ans = x;
    int cur = mn;
    int prev = 0;
    vector <int> temp;
    int t = inf;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {

            cur = cur + solve(temp, temp.size(), t);
            t = inf;
            temp.clear();

        }
        else
        {
            t = min(t, v[i]);
            temp.pb(v[i]);
        }
    }

    if ( !temp.empty())
        cur = cur + solve(temp, temp.size(), t);

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    int mn = inf;
    fo(i, n)
    {   int x;
        cin >> x;
        mn = min(mn, x);
        a.pb(x);
    }

    cout << solve(a, n, mn);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x, int mn)
{

    fo(i, x)
    {
        mn = min(mn, v[i]);
    }
    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    if ( x - count + mn >= x)
    {
        return x;
    }

    int ans = x;
    int cur = mn;
    int prev = 0;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {
            vector <int> temp;
            int t = inf;
            for (int j = prev; j < i; j++)
            {
                t = min(t, v[j]);
                temp.pb(v[j]);

                cur = cur + solve(temp, temp.size(), t);

            }
            prev = i + 1;
        }
    }
    vector <int> temp;
    int t  = inf;
    for (int j = prev; j < x; j++)
    {   t = min(t, v[j]);
        temp.pb(v[j]);

        cur = cur + solve(temp, temp.size(), t);

    }

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    int mn = inf;
    fo(i, n)
    {   int x;
        cin >> x;
        mn = min(mn, x);
        a.pb(x);
    }

    cout << solve(a, n, mn);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x, int mn)
{

    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    if ( x - count + mn >= x)
    {
        return x;
    }

    int ans = x;
    int cur = mn;

    vector <int> temp;
    int t = inf;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {

            if ( !temp.empty())
                cur = cur + solve(temp, (int) temp.size(), t);

            t = inf;
            temp.clear();

        }
        else
        {
            t = min(t, v[i]);
            temp.pb(v[i]);
        }
    }

    if ( !temp.empty())
        cur = cur + solve(temp, (int) temp.size(), t);

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    int mn = inf;
    fo(i, n)
    {   int x;
        cin >> x;
        mn = min(mn, x);
        a.pb(x);
    }

    cout << solve(a, n, mn);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x, int mn)
{

    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    // if ( x - count + mn >= x)
    // {
    //     return x;
    // }

    int ans = x;
    int cur = mn;

    vector <int> temp;
    int t = inf;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {

            if ( !temp.empty())
                cur = cur + solve(temp, (int) temp.size(), t);

            t = inf;
            temp.clear();

        }
        else
        {
            t = min(t, v[i]);
            temp.pb(v[i]);
        }
    }

    if ( !temp.empty())
        cur = cur + solve(temp, (int) temp.size(), t);

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    int mn = inf;
    fo(i, n)
    {   int x;
        cin >> x;
        mn = min(mn, x);
        a.pb(x);
    }

    cout << solve(a, n, mn);

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define ll long long
#define pb push_back
#define fo(i,N) for(int i = 0 ; i < N ; i++)
#define foo(i,x,N) for (int i = x; i < N ; i++)
#define fill(a,val) memset(a,val,sizeof(a))
#define fastio()  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define MAX 1234567
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(a) a.begin(),a.end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define pqb priority_queue<pair<pair<ll,ll>,int>>
#define pqs priority_queue<ll,vector <ll>,greater<ll>>
/// find_by_order() -- returns the iterator to the kth largest
/// order_of_key() -- number of elements strictly less than k
ll mpow(ll x, ll y, ll m) {if ( y == 0) return 1;  if ( y & 1) return (x % m * mpow(x, y - 1, m) % m) % m; else  {ll temp = mpow(x, y / 2, m) % m; return (temp * temp) % m;}}
ll power(ll x, ll y) {if ( y == 0) return 1;  if ( y & 1) return (x * power(x, y - 1)); else  {ll temp = power(x, y / 2); return (temp * temp);}}
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
        tree_order_statistics_node_update>
        pbds;
void wandan23() {
    fastio()
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int solve(vector <int> &v, int x)
{
    int mn = inf;
    fo(i, x)
    {
        mn = min(mn, v[i]);
    }
    int count = 0;
    fo(i, x)
    {
        if ( v[i] == mn)
            count++;
    }
    if ( x - count + mn >= x)
    {
        return x;
    }

    int ans = x;
    int cur = mn;
    int prev = 0;
    fo(i, x)
    {
        v[i] -= mn;

        if ( v[i] == 0)
        {
            vector <int> temp;
            for (int j = prev; j < i; j++)
            {
                temp.pb(v[j]);

                cur = cur + solve(temp, temp.size());

            }
            prev = i + 1;
        }
    }
    vector <int> temp;
    for (int j = prev; j < x; j++)
    {
        temp.pb(v[j]);

        cur = cur + solve(temp, temp.size());

    }

    return min(ans, cur);
}

signed main()
{

    wandan23();
    int n;
    cin >> n;
    vector <int> a;
    fo(i, n)
    {   int x;
        cin >> x;
        a.pb(x);
    }

    cout << solve(a, n);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifdef OJ
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define sz(x) ((int)x.size())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
#define RETURN(x) do{cout << (x) << '\n'; return 0;}while(0)

const char en = '\n';
const int MOD = 0;
const int MAXN = 0;
const int INF = 1e9+1;

struct stInfo {
    int mn, cnt;
    stInfo() {
        mn = INF;
        cnt = 0;
    }
};

int solve() {
    int n; cin >> n;
    vector<int> a(n);
    fori(i,n) cin >> a[i];
    vector<LL> dp(n,INF);

    fori(i,n) {
        if(a[i] == 0) {
            if(i) dp[i] = dp[i-1];
            else dp[i] = 0;
        } else {
            stInfo cur;
            int flag = 1;
            ford(j,i,0) {
                if(a[j]) {
                    if(a[j] < cur.mn) {
                        cur.mn = a[j];
                        cur.cnt = 1;
                    } else if(a[j] == cur.mn) {
                        cur.cnt++;
                    }
                } else {
                    flag = 0;
                }

                LL tmp = i-j+1;
                if(flag) mmin(tmp, (LL)i-j+1-cur.cnt+cur.mn);
                if(j) tmp += dp[j-1];
                mmin(dp[i],tmp);
            }
        }
    }
    cout << dp[n-1] << en;
    return 0;
}

int main(int argc, char **argv) {
#ifdef OJ
    freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);

    int ntest = 1;
    //cin >> ntest;
    fori(test, ntest) {
        //cout << "Case #" << test+1 << ": ";
        solve();
    }

    return 0;
}