/*
IZhO 14-Blocks(same as this 2008 Romanian problem - https://www.infoarena.ro/problema/ksecv)
We can solve it using deques, where dp[i][j] = min sum if we split the first j numbers in i sequences
*/
#include<bits/stdc++.h>
using namespace std;
int n, k, dp[3][100002], v[100002];
int val[100002], pmax[100002], pmin[100002], sz;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for(int i = 1; i <= n; ++i)
cin >> v[i];
int mx = 0;
for(int i = 1; i <= n; ++i)
{
mx = max(mx, v[i]);
dp[1][i] = mx;
}
for(int i = 2; i <= k; ++i)
{
sz = 0;
for(int j = i; j <= n; ++j)
{
int zmin = j-1;
while(sz && v[j] >= v[val[sz]])
{
if(dp[1][pmin[sz]] < dp[1][zmin])
zmin = pmin[sz];
--sz;
}
++sz;
val[sz] = j;
pmin[sz] = zmin;
if(sz == 1)
pmax[sz] = dp[1][zmin] + v[j];
else
{
if(pmax[sz - 1] < dp[1][zmin] + v[j])
pmax[sz] = pmax[sz - 1];
else
pmax[sz] = dp[1][zmin] + v[j];
}
dp[2][j] = pmax[sz];
}
for(int j = 1; j <= n; ++j)
dp[1][j] = dp[2][j], dp[2][j] = 0;
}
cout << dp[1][n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
9 ms |
896 KB |
Output is correct |
4 |
Correct |
40 ms |
896 KB |
Output is correct |
5 |
Correct |
121 ms |
1676 KB |
Output is correct |
6 |
Correct |
16 ms |
1508 KB |
Output is correct |
7 |
Correct |
59 ms |
1536 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
21 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
8 ms |
512 KB |
Output is correct |
17 |
Correct |
7 ms |
768 KB |
Output is correct |
18 |
Correct |
12 ms |
896 KB |
Output is correct |
19 |
Correct |
64 ms |
896 KB |
Output is correct |
20 |
Correct |
189 ms |
1656 KB |
Output is correct |
21 |
Correct |
16 ms |
1536 KB |
Output is correct |
22 |
Correct |
85 ms |
1536 KB |
Output is correct |
23 |
Correct |
13 ms |
1152 KB |
Output is correct |
24 |
Correct |
26 ms |
1536 KB |
Output is correct |
25 |
Correct |
185 ms |
1536 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
14 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
6 ms |
484 KB |
Output is correct |
31 |
Correct |
7 ms |
768 KB |
Output is correct |
32 |
Correct |
10 ms |
1124 KB |
Output is correct |
33 |
Correct |
45 ms |
1024 KB |
Output is correct |
34 |
Correct |
133 ms |
1920 KB |
Output is correct |
35 |
Correct |
25 ms |
1988 KB |
Output is correct |
36 |
Correct |
66 ms |
1920 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
13 ms |
512 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |