//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1;
int big = 1e9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int dp[n + 1][k + 1], a[n], greater[n];
for (int &x : a)
cin >> x;
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.top()] < a[i])
greater[st.top()] = i, st.pop();
st.push(i);
}
while (!st.empty())
greater[st.top()] = n, st.pop();
for (int i = 0; i < k; i++)
dp[n][i] = big;
for (int i = 0; i < n; i++)
dp[i][k] = big;
dp[n][k] = 0;
for (int i = n - 1; i >= 0; i--)
for (int j = k - 1; j >= 0; j--)
dp[i][j] = min(dp[greater[i]][j], a[i] + dp[i + 1][j + 1]);
cout << dp[0][0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |