#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 c = 1; c <= k; c++) {
vector<pair<int, pair<int,int>>> operation;
for(int i = c; i <= n; i++) {
int64_t mn = 1e9;
for(int l = i; l >= c && a[i] >= a[l]; l--) {
mn = dp[l - 1][c - 1];
}
for(int r = i; r <= n && a[i] >= a[r]; r++) {
dp[r][c] = min(dp[r][c], mn + a[i]);
}
/**
dp[i][j] <= dp[i + 1][j]
**/
}
}
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... |