![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1af4a68d43832328094a2489503e14d8.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ea4972f490e272ca1d9dbd2f9bea369.png)
代码
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5e3 + 100;
int a[maxn];
int f(int start, int end) {
if (start > end) return 0;
int minh = 0x3f3f3f, minpos = 0;
for (int i = start; i <= end; i++) {
if (minh > a[i]) {
minh = a[i];
minpos = i;
}
}
for (int i = start; i <= end; i++) a[i] -= minh;
return min(end - start + 1, minh + f(start, minpos - 1) + f(minpos + 1, end));
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
cout << f(0, n - 1) << endl;
return 0;
}
未能通过所有测试点,超时了
加上
ios::sync_with_stdio(false);
就过了