全部评论 1

  • #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;

    2024-08-13 来自 浙江

    0
首页