#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <map>
#include <set>
using namespace std;
struct SparseTableMax {
vector<vector<int64_t>> dp;
int64_t query (int l, int r) {
int sz = log2(r - l + 1);
return max(dp[l][sz], dp[r - (1 << sz) + 1][sz]);
}
SparseTableMax (vector<int64_t> a) {
int n = a.size();
dp.resize(n);
for (int i = 0; i < a.size(); i++) {
dp[i].resize((int)log2(n) + 1);
dp[i][0] = a[0];
}
for (int j = 1; j < dp[0].size(); j++) {
for (int i = 0; i < a.size(); i++) {
dp[i][j] = max(dp[i][j - 1], dp[min(i + (1 << (j - 1)), n - 1)][j - 1]);
}
}
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
int64_t arr[N];
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int64_t INF = 1e17;
vector<int64_t> prev(N + 1), cur(N + 1);
prev.assign(N + 1, INF), cur.assign(N + 1, INF);
for (int j = 1; j <= N; j++) {
prev[j] = ((j == 1) ? arr[0] : max(prev[j - 1], arr[j - 1]));
}
for (int i = 2; i <= K; i++) {
vector<int64_t> val;
val.assign(N + 1, INF);
for (int j = 1; j <= N; j++) {
if (j == 1) {
val[0] = prev[0] + arr[0];
cur[1] = prev[0] + arr[0];
} else {
cur[j] = INF;
int64_t myMax = 0;
int ind = -1;
for (int l = j - 1; l >= 0; l--) {
myMax = max(myMax, arr[l]);
if (myMax == arr[j - 1]) {
cur[j] = min(cur[j], prev[l] + arr[j - 1]);
val[l] = prev[l] + arr[j - 1];
ind = l;
} else {
break;
}
}
SparseTableMax st(val);
cur[j] = st.query(0, ind - 1);
}
}
swap(cur, prev);
}
cout << prev[N];
}
Compilation message
blocks.cpp: In constructor 'SparseTableMax::SparseTableMax(std::vector<long int>)':
blocks.cpp:17:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
blocks.cpp:21:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int j = 1; j < dp[0].size(); j++) {
| ~~^~~~~~~~~~~~~~
blocks.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
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 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
5 |
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 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
5 |
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 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
5 |
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 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |