#include "bits/stdc++.h"
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
vector<vector<long long>> dp(k, vector<long long> (n + 1));
for (int i = 1; i <= n; ++i) {
cin >> a[i];
dp[0][i] = max(a[i], (int) dp[0][i - 1]);
}
for (int j = 1; j < k; ++j) {
stack<pair<int, long long>> stk;
for (int i = j + 1; i < n; ++i) {
long long x = dp[j - 1][i - 1];
while (!stk.empty() && stk.top().first <= a[i]) {
x = min(x, stk.top().second);
stk.pop();
}
if (stk.empty() || (stk.top().first + stk.top().second > a[i] + x)) {
stk.push({a[i], x});
}
dp[j][i] = stk.top().first + stk.top().second;
}
}
cout << dp[k - 1][n - 1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |