终于AC了(没AC找我)
2024-10-17 19:54:09
发布于:北京
4阅读
0回复
0点赞
#include <bits/stdc++.h>
#define MAX 405
using namespace std;
int n, m;
int a[MAX], f[MAX][MAX], s[MAX];
int main(){
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
s[i] = s[i-1]+a[i];
}
m++;
memset(f, 0x3f, sizeof(f));
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= min(m, i); ++j) {
int mx = a[i];
for (int k = i-1; k >= 0; --k) {
f[i][j] = min(f[i][j], f[k][j-1]+mx*(i-k)-(s[i]-s[k]));
mx = max(mx, a[k]);
}
}
}
int ans = 0x3f3f3f3f;
for (int i = 0; i <= m; ++i) {
ans = min(ans, f[n][i]);
}
cout << ans << endl;
return 0;
}
这里空空如也
有帮助,赞一个