#include <bits/stdc++.h>
using namespace std;
int n, a[5005];
int dp[5005][5005], ans;
int cnt, b[5005];
int Hash(int x) { return lower_bound(b + 1, b + 1 + cnt, x) - b; }
int main() {
  while (~scanf("%d", &n)) {
    for (long long i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    cnt = unique(b + 1, b + 1 + n) - b - 1;
    memset(dp, 0x3f, sizeof(dp));
    ans = 0x3f3f3f3f;
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      long long loc = Hash(a[i + 1]);
      for (int j = 0; j <= cnt; j++) {
        if (dp[i][j] != 0x3f3f3f3f) {
          if (j >= loc) {
            dp[i + 1][loc] = min(dp[i + 1][loc], dp[i][j]);
          } else {
            dp[i + 1][loc] = min(dp[i + 1][loc], dp[i][j] + (a[i + 1] - b[j]));
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
          }
        }
      }
    }
    for (int j = 0; j <= cnt; j++) {
      if (dp[n][j] != 0x3f3f3f3f) {
        ans = min(ans, dp[n][j]);
      }
    }
    printf("%d\n", ans);
  }
}
