Task
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 3e3 + 10; int n,a[maxn]; int solve(int l,int r){ int minn = 1e9 + 10; for(int i = l; i <= r; i++) minn = min(minn,a[i]); int ans = minn; int pre = l; for(int i = l; i <= r; i++){ a[i] -= minn; if(!a[i]){ ans += solve(pre,i - 1); pre = i + 1; } } if(pre != r + 1) ans += solve(pre,r); return min(ans,r - l + 1); } signed main(){ // freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; cout << solve(1,n); return 0; }