#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, K = 1e2 + 10;
int n, k;
int a[N];
int dp[N][K];
int pref[N];
int lst[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
stack<int> stk;
for(int i = 1; i <= n; i++) {
while(!stk.empty() && a[stk.top()] <= a[i]) stk.pop();
if(stk.empty()) lst[i] = 0;
else lst[i] = stk.top();
stk.push(i);
}
int mx = 0;
for(int i = 1; i <= n; i++) {
mx = max(mx, a[i]);
dp[i][1] = mx;
}
for(int i = 0; i <= n; i++) for(int j = 2; j <= k; j++) dp[i][j] = 1e9;
pref[0] = 1e9;
for(int j = 2; j <= k; j++) {
for(int i = j; i <= n; i++) {
pref[i] = min(pref[lst[i]], dp[lst[i] + 1][j - 1] + a[i]);
dp[i][j] = pref[i];
}
}
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= k; j++) cout << dp[i][j] << " ";
// cout << "\n";
// }
cout << dp[n][k];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Incorrect |
0 ms |
2476 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |