#include <iostream>
#include <stack>
#include <vector>
#include <map>
using namespace std;
const int inf = 2e9;
int dp[100001][101];
int a[100001];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, K;
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= K; j++) {
dp[i][j] = inf;
}
}
dp[0][0] = 0;
dp[1][1] = a[1];
for (int i = 2; i <= N; i++) {
dp[i][1] = max(dp[i - 1][1], a[i]);
}
auto getmax = [&](int l, int r) -> int {
int mx = a[l];
for (int i = l; i <= r; i++) {
mx = max(mx, a[i]);
}
return mx;
};
for (int k = 2; k <= K; k++) {
stack < pair < int, int > > st;
for (int i = 1; i <= N; i++) {
/*dp[i][k] = dp[i - 1][k - 1] + a[i];
int mn = dp[i - 1][k - 1];
while (!st.empty() && a[st.top().first] <= a[i]) {
mn = min(mn, st.top().second);
// SECOND VAARIANT
dp[i][k] = min(dp[i][k], mn + a[i]);
//
st.pop();
}
// FIRST VARIANT
if (!st.empty())
dp[i][k] = min(dp[i][k], dp[st.top().first][k]);
//
st.push({i, mn});*/
int l;
dp[i][k] = dp[i - 1][k - 1] + a[i];
for (l = i; l >= 1; l--) {
if (a[l] > a[i]) {
break;
}
dp[i][k] = min(dp[i][k], dp[l][k - 1] + a[i]);
}
if (l >= 1) {
dp[i][k] = min(dp[i][k], dp[l][k]);
}
}
}
cout << dp[N][K] << '\n';
return 0;
}
Compilation message
blocks.cpp: In function 'int32_t main()':
blocks.cpp:35:13: warning: variable 'getmax' set but not used [-Wunused-but-set-variable]
35 | auto getmax = [&](int l, int r) -> int {
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |