// https://oj.uz/problem/view/IZhO14_blocks
#include <bits/stdc++.h>
using namespace std;
#define ii pair<int , int>
#define iv pair<ii , ii>
#define iii pair<int , ii>
#define fi first
#define se second
#define int long long
const int inf = 1e18 + 7;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7;
main() {
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
int n , k; cin >> n >> k;
vector<int> a(n + 1) , L(n + 1);
a[0] = inf;
deque<int> dq; dq.push_back(0);
for (int i = 1 ; i <= n ;i ++) {
cin >> a[i];
while (!dq.empty() && a[dq.back()] <= a[i]) dq.pop_back();
L[i] = dq.back() + 1;
dq.push_back(i);
}
vector<vector<int>> f(n + 1 , vector<int> (k + 1 , inf));
// nhan xet ta co f[i][j] <= f[i + 1][j] <= f[i + 2][j] .. <= f[n][j]
// nen ta nhan a[i] la max se lay' doan L xa nhat sao cho a[i] la max
f[0][0] = 0;
for (int j = 1 ; j <= k ; j++) {
for (int i = j ; i <= n ;i ++) {
f[i][j] = min(f[max(L[i] - 1 , j - 1)][j] , f[max(L[i] - 1 , j - 1)][j - 1] + a[i]);
}
}
cout << f[n][k];
// for (int j = 1 ; j <= k ;j ++) {
// for (int i = 1 ; i <= n ; i++) {
// cerr << f[i][j] << " ";
// }
// cerr << '\n';
// }
return 0;
}
Compilation message
blocks.cpp:17:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
17 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |