#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<vector<int64_t>> dp(n + 1, vector<int64_t>(k + 1));
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= k; j++) {
dp[i][j] = 1e12;
}
}
dp[0][0] = 0;
/**
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= min(k, i); j++) {
int mx = a[i];
for(int x = i - 1; x >= 0; x--) {
dp[i][j] = min(dp[i][j], dp[x][j - 1] + mx);
mx = max(mx, a[x]);
}
}
}**/
for(int c = 1; c <= k; c++) {
for(int i = c; i <= n; i++) {
/**
int mx = 0;
for(int j = i; j <= n; j++) {
mx = max(mx, a[j]);
dp[j][c] = min(dp[j][c], dp[i - 1][c - 1] + mx);
}**/
for(int l = c; l <= n; l++) {
int mx = 0;
for(int r = l; r <= n; r++) {
mx = max(mx, a[r]);
if(l <= i && i <= r && mx == a[i]) {
dp[r][c] = min(dp[r][c], dp[l - 1][c - 1] + a[i]);
}
}
}
}
}
cout << dp[n][k];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |