#include <bits/stdc++.h>
#define f0(i, n) for(int i(0); i<(n); i++)
#define f1(i, n) for(int i(1); i<=(n); i++)
using namespace std;
typedef long long ll;
const int N = 1e5 + 3;
int n, k, a[N], f[N][102];
int main(){
ios_base::sync_with_stdio(0);
cin >> n >> k;
f1(i, n) cin >> a[i];
stack <int> s;
f1(j, k){
while(s.size()) s.pop();
s.push(j - 1);
f1(i, n){
f[i][j] = 1e9;
int &res = f[i][j];
while(s.size() && a[s.top()] <= a[i]){
if(s.top() >= j) res = min(res, f[s.top()][j] - a[s.top()] + a[i]);
if(s.top() >= j - 1) res = min(res, f[s.top()][j - 1] + a[i]);
s.pop();
}
if(s.size()){
if(s.top() >= j) res = min(res, f[s.top()][j] - a[s.top()] + a[i]);
if(s.top() >= j - 1) res = min(res, f[s.top()][j - 1] + a[i]);
}
else res = min(res, f[j - 1][j - 1] + a[i]);
s.push(i);
}
}
int best = f[n][k], ma = a[n];
for(int i = n - 1; i >= k - 1; i--){
/// i + 1, n
best = min(best, f[i][k - 1] + ma);
ma = max(ma, a[i]);
}
cout << best;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
428 KB |
Output is correct |
2 |
Incorrect |
3 ms |
616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
4556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |