#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 = c + 1; i <= n; i++) {
for(int l = 1; l <= n; l++) {
for(int r = l; r <= n; r++) {
int x = *max_element(a.begin() + l, a.begin() + r + 1);
if(x != a[i]) {
continue;
}
dp[r][c + 1] = min(dp[r][c + 1], dp[l - 1][c] + a[i]);
}
}
/**
int sc = 1;
int64_t mn = 1e12;
for(int j = i; j >= 1; j--) {
if(a[j] > a[i]) {
break;
}
mn = min(mn, dp[j][c]);
sc = j;
}
for(int j = i; j <= n && a[j] <= a[i]; j++) {
dp[j][c + 1] = min(dp[j][c + 1], mn + a[i]);
mn = min(mn, dp[j][c]);
}**/
}
}
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... |