#include <bits/stdc++.h>
#define int long long
#define mask(k) (1<<(k))
using namespace std;
int n,k,a[100001],dp[100001][101],l[100001],mx[100001][21];
stack<int> st;
int getmn(int l, int r)
{
int p=__lg(r-l+1);
return min(mx[l][p],mx[r-mask(p)+1][p]);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("kblocks.inp","r",stdin);
//freopen("kblocks.out","w",stdout);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=n; i++)
{
while (!st.empty() && a[st.top()]<=a[i]) st.pop();
if (!st.empty()) l[i]=st.top(); else l[i]=0;
st.push(i);
}
memset(dp,0x3f,sizeof(dp));
dp[0][0]=0;
for (int j=1; j<=k; j++)
{
for (int i=0; i<=n; i++) mx[i][0]=dp[i][j-1];
for (int j=1; mask(j)<=n+1; j++)
for (int i=0; i+mask(j)-1<=n; i++)
mx[i][j]=min(mx[i][j-1],mx[i+mask(j-1)][j-1]);
for (int i=1; i<=n; i++)
{
dp[i][j]=min(dp[i][j],getmn(l[i],i-1)+a[i]);
dp[i][j]=min(dp[i][j],dp[l[i]][j]);
}
}
cout<<dp[n][k];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
82512 KB |
Output is correct |
2 |
Correct |
10 ms |
82512 KB |
Output is correct |
3 |
Correct |
10 ms |
82512 KB |
Output is correct |
4 |
Correct |
11 ms |
82512 KB |
Output is correct |
5 |
Correct |
11 ms |
82512 KB |
Output is correct |
6 |
Correct |
11 ms |
82496 KB |
Output is correct |
7 |
Correct |
11 ms |
82512 KB |
Output is correct |
8 |
Correct |
11 ms |
82512 KB |
Output is correct |
9 |
Correct |
11 ms |
82456 KB |
Output is correct |
10 |
Correct |
12 ms |
82396 KB |
Output is correct |
11 |
Correct |
11 ms |
82512 KB |
Output is correct |
12 |
Correct |
11 ms |
82512 KB |
Output is correct |
13 |
Correct |
10 ms |
82512 KB |
Output is correct |
14 |
Correct |
12 ms |
82512 KB |
Output is correct |
15 |
Correct |
10 ms |
82512 KB |
Output is correct |
16 |
Correct |
11 ms |
82512 KB |
Output is correct |
17 |
Correct |
11 ms |
82512 KB |
Output is correct |
18 |
Correct |
11 ms |
82512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
82512 KB |
Output is correct |
2 |
Correct |
10 ms |
82512 KB |
Output is correct |
3 |
Correct |
11 ms |
82512 KB |
Output is correct |
4 |
Correct |
11 ms |
82512 KB |
Output is correct |
5 |
Correct |
11 ms |
82512 KB |
Output is correct |
6 |
Correct |
11 ms |
82512 KB |
Output is correct |
7 |
Correct |
11 ms |
82512 KB |
Output is correct |
8 |
Correct |
11 ms |
82472 KB |
Output is correct |
9 |
Correct |
11 ms |
82420 KB |
Output is correct |
10 |
Correct |
11 ms |
82512 KB |
Output is correct |
11 |
Correct |
11 ms |
82512 KB |
Output is correct |
12 |
Correct |
11 ms |
82404 KB |
Output is correct |
13 |
Correct |
11 ms |
82512 KB |
Output is correct |
14 |
Correct |
12 ms |
82512 KB |
Output is correct |
15 |
Correct |
11 ms |
82512 KB |
Output is correct |
16 |
Correct |
11 ms |
82512 KB |
Output is correct |
17 |
Correct |
11 ms |
82524 KB |
Output is correct |
18 |
Correct |
12 ms |
82512 KB |
Output is correct |
19 |
Correct |
10 ms |
82512 KB |
Output is correct |
20 |
Correct |
12 ms |
82512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
82512 KB |
Output is correct |
2 |
Correct |
10 ms |
82512 KB |
Output is correct |
3 |
Correct |
10 ms |
82512 KB |
Output is correct |
4 |
Correct |
11 ms |
82512 KB |
Output is correct |
5 |
Correct |
11 ms |
82512 KB |
Output is correct |
6 |
Correct |
11 ms |
82496 KB |
Output is correct |
7 |
Correct |
11 ms |
82512 KB |
Output is correct |
8 |
Correct |
11 ms |
82512 KB |
Output is correct |
9 |
Correct |
11 ms |
82456 KB |
Output is correct |
10 |
Correct |
12 ms |
82396 KB |
Output is correct |
11 |
Correct |
11 ms |
82512 KB |
Output is correct |
12 |
Correct |
11 ms |
82512 KB |
Output is correct |
13 |
Correct |
10 ms |
82512 KB |
Output is correct |
14 |
Correct |
12 ms |
82512 KB |
Output is correct |
15 |
Correct |
10 ms |
82512 KB |
Output is correct |
16 |
Correct |
11 ms |
82512 KB |
Output is correct |
17 |
Correct |
11 ms |
82512 KB |
Output is correct |
18 |
Correct |
11 ms |
82512 KB |
Output is correct |
19 |
Correct |
10 ms |
82512 KB |
Output is correct |
20 |
Correct |
10 ms |
82512 KB |
Output is correct |
21 |
Correct |
11 ms |
82512 KB |
Output is correct |
22 |
Correct |
11 ms |
82512 KB |
Output is correct |
23 |
Correct |
11 ms |
82512 KB |
Output is correct |
24 |
Correct |
11 ms |
82512 KB |
Output is correct |
25 |
Correct |
11 ms |
82512 KB |
Output is correct |
26 |
Correct |
11 ms |
82472 KB |
Output is correct |
27 |
Correct |
11 ms |
82420 KB |
Output is correct |
28 |
Correct |
11 ms |
82512 KB |
Output is correct |
29 |
Correct |
11 ms |
82512 KB |
Output is correct |
30 |
Correct |
11 ms |
82404 KB |
Output is correct |
31 |
Correct |
11 ms |
82512 KB |
Output is correct |
32 |
Correct |
12 ms |
82512 KB |
Output is correct |
33 |
Correct |
11 ms |
82512 KB |
Output is correct |
34 |
Correct |
11 ms |
82512 KB |
Output is correct |
35 |
Correct |
11 ms |
82524 KB |
Output is correct |
36 |
Correct |
12 ms |
82512 KB |
Output is correct |
37 |
Correct |
10 ms |
82512 KB |
Output is correct |
38 |
Correct |
12 ms |
82512 KB |
Output is correct |
39 |
Correct |
12 ms |
82512 KB |
Output is correct |
40 |
Correct |
12 ms |
82680 KB |
Output is correct |
41 |
Correct |
11 ms |
82512 KB |
Output is correct |
42 |
Correct |
12 ms |
82536 KB |
Output is correct |
43 |
Correct |
12 ms |
82512 KB |
Output is correct |
44 |
Correct |
11 ms |
82544 KB |
Output is correct |
45 |
Correct |
11 ms |
82512 KB |
Output is correct |
46 |
Correct |
11 ms |
82512 KB |
Output is correct |
47 |
Correct |
10 ms |
82436 KB |
Output is correct |
48 |
Correct |
11 ms |
82512 KB |
Output is correct |
49 |
Correct |
12 ms |
82512 KB |
Output is correct |
50 |
Correct |
12 ms |
82640 KB |
Output is correct |
51 |
Correct |
12 ms |
82512 KB |
Output is correct |
52 |
Correct |
13 ms |
82512 KB |
Output is correct |
53 |
Correct |
11 ms |
82512 KB |
Output is correct |
54 |
Correct |
11 ms |
82516 KB |
Output is correct |
55 |
Correct |
10 ms |
82512 KB |
Output is correct |
56 |
Correct |
11 ms |
82512 KB |
Output is correct |
57 |
Correct |
11 ms |
82512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
82512 KB |
Output is correct |
2 |
Correct |
10 ms |
82512 KB |
Output is correct |
3 |
Correct |
10 ms |
82512 KB |
Output is correct |
4 |
Correct |
11 ms |
82512 KB |
Output is correct |
5 |
Correct |
11 ms |
82512 KB |
Output is correct |
6 |
Correct |
11 ms |
82496 KB |
Output is correct |
7 |
Correct |
11 ms |
82512 KB |
Output is correct |
8 |
Correct |
11 ms |
82512 KB |
Output is correct |
9 |
Correct |
11 ms |
82456 KB |
Output is correct |
10 |
Correct |
12 ms |
82396 KB |
Output is correct |
11 |
Correct |
11 ms |
82512 KB |
Output is correct |
12 |
Correct |
11 ms |
82512 KB |
Output is correct |
13 |
Correct |
10 ms |
82512 KB |
Output is correct |
14 |
Correct |
12 ms |
82512 KB |
Output is correct |
15 |
Correct |
10 ms |
82512 KB |
Output is correct |
16 |
Correct |
11 ms |
82512 KB |
Output is correct |
17 |
Correct |
11 ms |
82512 KB |
Output is correct |
18 |
Correct |
11 ms |
82512 KB |
Output is correct |
19 |
Correct |
10 ms |
82512 KB |
Output is correct |
20 |
Correct |
10 ms |
82512 KB |
Output is correct |
21 |
Correct |
11 ms |
82512 KB |
Output is correct |
22 |
Correct |
11 ms |
82512 KB |
Output is correct |
23 |
Correct |
11 ms |
82512 KB |
Output is correct |
24 |
Correct |
11 ms |
82512 KB |
Output is correct |
25 |
Correct |
11 ms |
82512 KB |
Output is correct |
26 |
Correct |
11 ms |
82472 KB |
Output is correct |
27 |
Correct |
11 ms |
82420 KB |
Output is correct |
28 |
Correct |
11 ms |
82512 KB |
Output is correct |
29 |
Correct |
11 ms |
82512 KB |
Output is correct |
30 |
Correct |
11 ms |
82404 KB |
Output is correct |
31 |
Correct |
11 ms |
82512 KB |
Output is correct |
32 |
Correct |
12 ms |
82512 KB |
Output is correct |
33 |
Correct |
11 ms |
82512 KB |
Output is correct |
34 |
Correct |
11 ms |
82512 KB |
Output is correct |
35 |
Correct |
11 ms |
82524 KB |
Output is correct |
36 |
Correct |
12 ms |
82512 KB |
Output is correct |
37 |
Correct |
10 ms |
82512 KB |
Output is correct |
38 |
Correct |
12 ms |
82512 KB |
Output is correct |
39 |
Correct |
12 ms |
82512 KB |
Output is correct |
40 |
Correct |
12 ms |
82680 KB |
Output is correct |
41 |
Correct |
11 ms |
82512 KB |
Output is correct |
42 |
Correct |
12 ms |
82536 KB |
Output is correct |
43 |
Correct |
12 ms |
82512 KB |
Output is correct |
44 |
Correct |
11 ms |
82544 KB |
Output is correct |
45 |
Correct |
11 ms |
82512 KB |
Output is correct |
46 |
Correct |
11 ms |
82512 KB |
Output is correct |
47 |
Correct |
10 ms |
82436 KB |
Output is correct |
48 |
Correct |
11 ms |
82512 KB |
Output is correct |
49 |
Correct |
12 ms |
82512 KB |
Output is correct |
50 |
Correct |
12 ms |
82640 KB |
Output is correct |
51 |
Correct |
12 ms |
82512 KB |
Output is correct |
52 |
Correct |
13 ms |
82512 KB |
Output is correct |
53 |
Correct |
11 ms |
82512 KB |
Output is correct |
54 |
Correct |
11 ms |
82516 KB |
Output is correct |
55 |
Correct |
10 ms |
82512 KB |
Output is correct |
56 |
Correct |
11 ms |
82512 KB |
Output is correct |
57 |
Correct |
11 ms |
82512 KB |
Output is correct |
58 |
Correct |
23 ms |
84560 KB |
Output is correct |
59 |
Correct |
17 ms |
91108 KB |
Output is correct |
60 |
Correct |
31 ms |
90704 KB |
Output is correct |
61 |
Correct |
189 ms |
90704 KB |
Output is correct |
62 |
Correct |
628 ms |
97360 KB |
Output is correct |
63 |
Correct |
36 ms |
97360 KB |
Output is correct |
64 |
Correct |
300 ms |
97528 KB |
Output is correct |
65 |
Correct |
12 ms |
82412 KB |
Output is correct |
66 |
Correct |
12 ms |
82512 KB |
Output is correct |
67 |
Correct |
12 ms |
82512 KB |
Output is correct |
68 |
Correct |
11 ms |
82492 KB |
Output is correct |
69 |
Correct |
12 ms |
82512 KB |
Output is correct |
70 |
Correct |
11 ms |
82512 KB |
Output is correct |
71 |
Correct |
53 ms |
84560 KB |
Output is correct |
72 |
Correct |
12 ms |
84560 KB |
Output is correct |
73 |
Correct |
23 ms |
84560 KB |
Output is correct |
74 |
Correct |
17 ms |
90704 KB |
Output is correct |
75 |
Correct |
27 ms |
90704 KB |
Output is correct |
76 |
Correct |
145 ms |
90704 KB |
Output is correct |
77 |
Correct |
461 ms |
97360 KB |
Output is correct |
78 |
Correct |
34 ms |
97360 KB |
Output is correct |
79 |
Correct |
255 ms |
97360 KB |
Output is correct |
80 |
Correct |
22 ms |
97376 KB |
Output is correct |
81 |
Correct |
61 ms |
97360 KB |
Output is correct |
82 |
Correct |
473 ms |
97528 KB |
Output is correct |
83 |
Correct |
10 ms |
82512 KB |
Output is correct |
84 |
Correct |
10 ms |
82672 KB |
Output is correct |
85 |
Correct |
55 ms |
84560 KB |
Output is correct |
86 |
Correct |
14 ms |
84752 KB |
Output is correct |
87 |
Correct |
21 ms |
84560 KB |
Output is correct |
88 |
Correct |
18 ms |
90960 KB |
Output is correct |
89 |
Correct |
29 ms |
90960 KB |
Output is correct |
90 |
Correct |
149 ms |
90960 KB |
Output is correct |
91 |
Correct |
507 ms |
97616 KB |
Output is correct |
92 |
Correct |
37 ms |
97616 KB |
Output is correct |
93 |
Correct |
231 ms |
97916 KB |
Output is correct |
94 |
Correct |
12 ms |
84560 KB |
Output is correct |
95 |
Correct |
52 ms |
84560 KB |
Output is correct |
96 |
Correct |
12 ms |
82524 KB |
Output is correct |
97 |
Correct |
11 ms |
82468 KB |
Output is correct |
98 |
Correct |
11 ms |
82512 KB |
Output is correct |
99 |
Correct |
11 ms |
82512 KB |
Output is correct |