#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int dp[101][100'000];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
dp[1][0] = a[0];
for (int j = 2; j <= k; ++j)
dp[j][0] = 1000'000 * k + 1;
vector<int> st;
vector<vector<int>> dpst(k + 1, vector<int>(1, 0));
for (int i = 1; i < n; ++i)
{
auto it = upper_bound(st.rbegin(), st.rend(), i, [&a](int i, int j){ return a[i] < a[j]; });
int stm = (it == st.rend() ? -1 : *it);
for (int j = 1; j <= k; ++j)
{
if (j == 1)
dp[1][i] = max(dp[1][i - 1], a[i]);
else
{
int cur = 1000'000 * k + 1;
int m = stm;
if (m != -1)
cur = min(cur, dp[j][m]);
else
m = 0;
auto it = lower_bound(dpst[j - 1].begin(), dpst[j - 1].end(), m);
if (it != dpst[j - 1].end())
cur = min(cur, dp[j - 1][*it] + a[i]);
dp[j][i] = cur;
}
}
for (int j = 1; j <= k; ++j)
{
while (dpst[j].size() && dp[j][dpst[j].back()] >= dp[j][i])
dpst[j].pop_back();
dpst[j].push_back(i);
#ifdef MANSON
cerr << "dp[" << j << "][" << i << "] = " << dp[j][i] << '\n';
#endif
}
while (st.size() && a[st.back()] <= a[i])
st.pop_back();
st.push_back(i);
}
cout << dp[k][n - 1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
896 KB |
Output is correct |
16 |
Correct |
5 ms |
896 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
896 KB |
Output is correct |
19 |
Correct |
5 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1920 KB |
Output is correct |
2 |
Correct |
12 ms |
1152 KB |
Output is correct |
3 |
Correct |
19 ms |
2560 KB |
Output is correct |
4 |
Correct |
133 ms |
19448 KB |
Output is correct |
5 |
Correct |
500 ms |
57832 KB |
Output is correct |
6 |
Correct |
28 ms |
3340 KB |
Output is correct |
7 |
Correct |
171 ms |
27096 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
25 ms |
4864 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
11 ms |
1536 KB |
Output is correct |
17 |
Correct |
12 ms |
1024 KB |
Output is correct |
18 |
Correct |
18 ms |
2176 KB |
Output is correct |
19 |
Correct |
79 ms |
14456 KB |
Output is correct |
20 |
Correct |
233 ms |
40700 KB |
Output is correct |
21 |
Correct |
25 ms |
2560 KB |
Output is correct |
22 |
Correct |
118 ms |
19064 KB |
Output is correct |
23 |
Correct |
21 ms |
1792 KB |
Output is correct |
24 |
Correct |
43 ms |
5368 KB |
Output is correct |
25 |
Correct |
224 ms |
40756 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
19 ms |
4864 KB |
Output is correct |
29 |
Correct |
6 ms |
512 KB |
Output is correct |
30 |
Correct |
10 ms |
1536 KB |
Output is correct |
31 |
Correct |
12 ms |
1152 KB |
Output is correct |
32 |
Correct |
16 ms |
2176 KB |
Output is correct |
33 |
Correct |
54 ms |
14584 KB |
Output is correct |
34 |
Correct |
149 ms |
40824 KB |
Output is correct |
35 |
Correct |
25 ms |
2904 KB |
Output is correct |
36 |
Correct |
78 ms |
19320 KB |
Output is correct |
37 |
Correct |
7 ms |
640 KB |
Output is correct |
38 |
Correct |
39 ms |
6520 KB |
Output is correct |
39 |
Correct |
5 ms |
512 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |