#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector <ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector <ll> r(n);
r[0] = -1;
for (int i = 1; i < n; i++) {
r[i] = r[i - 1];
while (r[i] != -1 && a[r[i]] < a[i]) {
r[i] = r[r[i]];
}
}
vector <vector <ll>> dp(k + 1, vector <ll> (n, 1e15));
for (int i = 0; i < n; i++) if (r[i] == -1) dp[1][i] = a[i];
for (int step = 1; step <= k; step++) {
vector <ll> u(n, 1e15);
vector <int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.back()] < a[i]) {
u[i] = min(u[i], u[st.back()]);
u[i] = min(u[i], dp[step - 1][st.back()]);
st.pop_back();
}
dp[step][i] = min(dp[step][i], u[i] + a[i]);
if (!st.empty()) {
dp[step][i] = min(dp[step][i], dp[step - 1][st.back()] + a[i]);
}
st.push_back(i);
}
}
ll ans = dp[k][n - 1];
for (int i = n - 2; i >= 0; i--) {
if (a[i + 1] > a[i]) break;
ans = min(ans, dp[k][i]);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |