#include<bits/stdc++.h>
using namespace std;
int n, k, dp[3][100002], v[100002];
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)
{
deque<int>val;
deque<int>pmax;
deque<int>pmin;
for(int j = i; j <= n; ++j)
{
int zmin = j-1;
while(!val.empty() && v[j] >= v[val.back()])
{
if(dp[1][pmin.back()] < dp[1][zmin])
zmin = pmin.back();
pmin.pop_back();
val.pop_back();
pmax.pop_back();
}
val.push_back(j);
pmin.push_back(zmin);
if(val.size() == 1)
pmax.push_back(dp[1][zmin] + v[j]);
else
{
if(pmax.back() < dp[1][zmin] + v[j])
pmax.push_back(pmax.back());
else
pmax.push_back(dp[1][zmin] + v[j]);
}
dp[2][j] = pmax.back();
}
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 |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 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 |
2 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 |
3 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 |
404 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
512 KB |
Output is correct |
2 |
Correct |
8 ms |
1024 KB |
Output is correct |
3 |
Correct |
14 ms |
1212 KB |
Output is correct |
4 |
Correct |
76 ms |
1216 KB |
Output is correct |
5 |
Correct |
205 ms |
2296 KB |
Output is correct |
6 |
Correct |
17 ms |
2176 KB |
Output is correct |
7 |
Correct |
95 ms |
2176 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 |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
29 ms |
512 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
10 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
1024 KB |
Output is correct |
18 |
Correct |
14 ms |
1152 KB |
Output is correct |
19 |
Correct |
97 ms |
1152 KB |
Output is correct |
20 |
Correct |
279 ms |
2296 KB |
Output is correct |
21 |
Correct |
18 ms |
2176 KB |
Output is correct |
22 |
Correct |
167 ms |
2168 KB |
Output is correct |
23 |
Correct |
13 ms |
1792 KB |
Output is correct |
24 |
Correct |
33 ms |
2176 KB |
Output is correct |
25 |
Correct |
275 ms |
2304 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
23 ms |
640 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
8 ms |
640 KB |
Output is correct |
31 |
Correct |
7 ms |
1024 KB |
Output is correct |
32 |
Correct |
14 ms |
1432 KB |
Output is correct |
33 |
Correct |
83 ms |
1472 KB |
Output is correct |
34 |
Correct |
244 ms |
2728 KB |
Output is correct |
35 |
Correct |
17 ms |
2760 KB |
Output is correct |
36 |
Correct |
145 ms |
2688 KB |
Output is correct |
37 |
Correct |
4 ms |
512 KB |
Output is correct |
38 |
Correct |
21 ms |
512 KB |
Output is correct |
39 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |