#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;
}
}
for(int i = 0; i <= n; i++) {
dp[i][0] = 0;
}
for(int c = 0; c < k; c++) {
for(int i = 1; i <= n; i++) {
int first = n + 1;
for(int j = i + 1; j <= n; j++) {
if(a[j] > a[i]) {
first = j;
break;
}
}
first -= 1;
for(int x = i; x <= first; x++) {
dp[x][c + 1] = min(dp[x][c + 1], dp[i - 1][c] + a[i]);
}
for(int x = i - 1; x >= 1; x--) {
if(a[x] > a[i]) {
break;
}
dp[i][c + 1] = min(dp[i][c + 1], dp[x - 1][c] + 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... |