#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int N = 100005;
const int K = 103;
const int inf=1e9;
int dp[N], ndp[N], pos[N], lg[N];
int sp[N][17];
int get(int l, int r){
if(l>r)return 0;
int rng=r-l+1;
int lga=lg[rng];
return max(sp[l][lga], sp[r-(1<<lga)+1][lga]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int a[n];
for(int i=0; i< n; i++)cin >> a[i];
int mx=0;
lg[1]=0;
for(int i=2; i<N; i++){
lg[i]=lg[i/2]+1;
}
for(int i=0; i< n; i++){
mx=max(mx, a[i]);
dp[i]=mx;
sp[i][0]=a[i];
}
for(int j=1; j<17; j++){
for(int i=0; i+(1<<j)<=n; i++){
sp[i][j]=max(sp[i][j-1], sp[i+(1<<(j-1))][j-1]);
}
}
stack<pair<int, int>> st;
st.push({inf, -1});
for(int i=0; i< n; i++){
while(!st.empty() && st.top().F<=a[i]){
st.pop();
}
pos[i]=st.top().S+1;
st.push({a[i], i});
}
while(!st.empty())st.pop();
for(int j=2; j<=k; j++){
st.push({inf, -1});
for(int i=0; i< n; i++){
int ps=pos[i];
pair<int, int> lst={-2, -1};
while(st.size() && ps<st.top().S){
lst=st.top();
st.pop();
}
if(st.size()==0 || (lst.F!=-2 && st.top().F+get(st.top().S+1, i)>lst.F+get(lst.S+1, i)))st.push(lst);
while(st.size()>1){
pair<int, int> p1=st.top();
st.pop();
pair<int, int> p2=st.top();
if(p1.F+get(p1.S+1, i)<p2.F+get(p2.S+1, i)){
st.push(p1);
break;
}
}
ndp[i]=st.top().F+get(st.top().S+1, i);
while(st.size() && st.top().F>= dp[i])st.pop();
if(dp[i]<ndp[i])st.push({dp[i], i});
}
while(!st.empty())st.pop();
for(int i=0; i< n; i++){
dp[i]=ndp[i];
}
}
cout << dp[n-1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
0 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
0 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
0 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
860 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
0 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
856 KB |
Output is correct |
40 |
Correct |
0 ms |
860 KB |
Output is correct |
41 |
Correct |
0 ms |
712 KB |
Output is correct |
42 |
Correct |
1 ms |
860 KB |
Output is correct |
43 |
Correct |
0 ms |
716 KB |
Output is correct |
44 |
Correct |
1 ms |
860 KB |
Output is correct |
45 |
Correct |
0 ms |
860 KB |
Output is correct |
46 |
Correct |
0 ms |
860 KB |
Output is correct |
47 |
Correct |
0 ms |
860 KB |
Output is correct |
48 |
Correct |
1 ms |
860 KB |
Output is correct |
49 |
Correct |
1 ms |
860 KB |
Output is correct |
50 |
Correct |
1 ms |
1112 KB |
Output is correct |
51 |
Correct |
1 ms |
860 KB |
Output is correct |
52 |
Correct |
1 ms |
860 KB |
Output is correct |
53 |
Correct |
1 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
1 ms |
860 KB |
Output is correct |
56 |
Correct |
1 ms |
860 KB |
Output is correct |
57 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
0 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
0 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
860 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
0 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
856 KB |
Output is correct |
40 |
Correct |
0 ms |
860 KB |
Output is correct |
41 |
Correct |
0 ms |
712 KB |
Output is correct |
42 |
Correct |
1 ms |
860 KB |
Output is correct |
43 |
Correct |
0 ms |
716 KB |
Output is correct |
44 |
Correct |
1 ms |
860 KB |
Output is correct |
45 |
Correct |
0 ms |
860 KB |
Output is correct |
46 |
Correct |
0 ms |
860 KB |
Output is correct |
47 |
Correct |
0 ms |
860 KB |
Output is correct |
48 |
Correct |
1 ms |
860 KB |
Output is correct |
49 |
Correct |
1 ms |
860 KB |
Output is correct |
50 |
Correct |
1 ms |
1112 KB |
Output is correct |
51 |
Correct |
1 ms |
860 KB |
Output is correct |
52 |
Correct |
1 ms |
860 KB |
Output is correct |
53 |
Correct |
1 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
860 KB |
Output is correct |
55 |
Correct |
1 ms |
860 KB |
Output is correct |
56 |
Correct |
1 ms |
860 KB |
Output is correct |
57 |
Correct |
1 ms |
724 KB |
Output is correct |
58 |
Correct |
9 ms |
3164 KB |
Output is correct |
59 |
Correct |
6 ms |
5740 KB |
Output is correct |
60 |
Correct |
13 ms |
5976 KB |
Output is correct |
61 |
Correct |
105 ms |
6116 KB |
Output is correct |
62 |
Correct |
305 ms |
9560 KB |
Output is correct |
63 |
Correct |
17 ms |
9564 KB |
Output is correct |
64 |
Correct |
148 ms |
9856 KB |
Output is correct |
65 |
Correct |
1 ms |
856 KB |
Output is correct |
66 |
Correct |
1 ms |
860 KB |
Output is correct |
67 |
Correct |
1 ms |
860 KB |
Output is correct |
68 |
Correct |
1 ms |
856 KB |
Output is correct |
69 |
Correct |
1 ms |
860 KB |
Output is correct |
70 |
Correct |
1 ms |
856 KB |
Output is correct |
71 |
Correct |
34 ms |
3288 KB |
Output is correct |
72 |
Correct |
2 ms |
3164 KB |
Output is correct |
73 |
Correct |
9 ms |
3164 KB |
Output is correct |
74 |
Correct |
6 ms |
5976 KB |
Output is correct |
75 |
Correct |
17 ms |
5980 KB |
Output is correct |
76 |
Correct |
113 ms |
5980 KB |
Output is correct |
77 |
Correct |
330 ms |
9720 KB |
Output is correct |
78 |
Correct |
18 ms |
9564 KB |
Output is correct |
79 |
Correct |
152 ms |
9716 KB |
Output is correct |
80 |
Correct |
12 ms |
9560 KB |
Output is correct |
81 |
Correct |
39 ms |
9736 KB |
Output is correct |
82 |
Correct |
330 ms |
9720 KB |
Output is correct |
83 |
Correct |
1 ms |
856 KB |
Output is correct |
84 |
Correct |
1 ms |
860 KB |
Output is correct |
85 |
Correct |
25 ms |
3164 KB |
Output is correct |
86 |
Correct |
2 ms |
3164 KB |
Output is correct |
87 |
Correct |
8 ms |
3232 KB |
Output is correct |
88 |
Correct |
6 ms |
5980 KB |
Output is correct |
89 |
Correct |
12 ms |
6232 KB |
Output is correct |
90 |
Correct |
84 ms |
6236 KB |
Output is correct |
91 |
Correct |
245 ms |
10024 KB |
Output is correct |
92 |
Correct |
20 ms |
9904 KB |
Output is correct |
93 |
Correct |
115 ms |
10016 KB |
Output is correct |
94 |
Correct |
2 ms |
3164 KB |
Output is correct |
95 |
Correct |
31 ms |
3296 KB |
Output is correct |
96 |
Correct |
1 ms |
860 KB |
Output is correct |
97 |
Correct |
1 ms |
860 KB |
Output is correct |
98 |
Correct |
0 ms |
860 KB |
Output is correct |
99 |
Correct |
0 ms |
860 KB |
Output is correct |