#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb emplace_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=1e5+3;
const ll INF=1e17;
int n,k,a[N],l[N],r[N];
ll f[N][104],stb[N][20];
stack<int> skull;
vector<ll> in[N],out[N];
multiset<ll> st;
void pre(int k) {
For(i,1,n) stb[i][0]=f[i][k];
for(int j=1;j<=16;j++)
for(int i=1;i+(1<<j)-1<=n;i++) stb[i][j]=min(stb[i][j-1],stb[i+(1<<j-1)][j-1]);
}
int get(int l,int r) {
int len=__lg(r-l+1);
return min(stb[l][len],stb[r-(1<<len)+1][len]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
For(i,1,n) cin >> a[i];
fill(l,l+n+1,-1);
fill(r,r+n+1,n+1);
For(i,1,n) {
while(sz(skull) && a[skull.top()]<a[i]) {
r[skull.top()]=i;
skull.pop();
}
skull.push(i);
}
while(sz(skull)) skull.pop();
ForD(i,n,1) {
while(sz(skull) && a[skull.top()]<a[i]) {
l[skull.top()]=i;
skull.pop();
}
skull.push(i);
}
For(i,1,n) l[i]++,r[i]--;
For(i,0,n)
For(j,1,k) f[i][j]=INF;
// Từ l[i]->i
f[1][1]=a[1];
For(i,2,n) f[i][1]=max(1LL*a[i],f[i-1][1]);
For(j,2,k) {
st.clear();
pre(j-1);
For(i,1,n) in[i].clear(),out[i].clear();
For(i,j,n) {
for(auto el: in[i]) st.insert(el);
for(auto el: out[i]) st.erase(st.find(el));
ll cur=get(max(l[i]-1,j-1),i-1)+a[i];
f[i][j]=min(f[i][j],cur);
if (sz(st)) f[i][j]=min(f[i][j],*st.begin());
if (r[i]>i) {
in[i+1].pb(cur);
out[r[i]+1].pb(cur);
}
// For(k,j,i) f[i][j]=min(f[i][j],f[k-1][j-1]+get(k,i));
}
}
cout << f[n][k];
return 0;
}
Compilation message
blocks.cpp: In function 'void pre(int)':
blocks.cpp:40:77: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
40 | for(int i=1;i+(1<<j)-1<=n;i++) stb[i][j]=min(stb[i][j-1],stb[i+(1<<j-1)][j-1]);
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10900 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10892 KB |
Output is correct |
17 |
Correct |
1 ms |
10888 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
1 ms |
10860 KB |
Output is correct |
5 |
Correct |
1 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
10680 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
1 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
1 ms |
10844 KB |
Output is correct |
13 |
Correct |
1 ms |
10900 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
1 ms |
10844 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10900 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10892 KB |
Output is correct |
17 |
Correct |
1 ms |
10888 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10840 KB |
Output is correct |
20 |
Correct |
1 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
1 ms |
10860 KB |
Output is correct |
23 |
Correct |
1 ms |
10844 KB |
Output is correct |
24 |
Correct |
1 ms |
10680 KB |
Output is correct |
25 |
Correct |
1 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
11096 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
1 ms |
10844 KB |
Output is correct |
31 |
Correct |
1 ms |
10900 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10840 KB |
Output is correct |
34 |
Correct |
1 ms |
10844 KB |
Output is correct |
35 |
Correct |
1 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10860 KB |
Output is correct |
39 |
Correct |
2 ms |
10844 KB |
Output is correct |
40 |
Correct |
1 ms |
10844 KB |
Output is correct |
41 |
Correct |
1 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
2 ms |
10844 KB |
Output is correct |
44 |
Correct |
2 ms |
10840 KB |
Output is correct |
45 |
Correct |
1 ms |
10844 KB |
Output is correct |
46 |
Correct |
2 ms |
10844 KB |
Output is correct |
47 |
Correct |
2 ms |
10840 KB |
Output is correct |
48 |
Correct |
2 ms |
10844 KB |
Output is correct |
49 |
Correct |
2 ms |
10844 KB |
Output is correct |
50 |
Correct |
1 ms |
10888 KB |
Output is correct |
51 |
Correct |
1 ms |
10844 KB |
Output is correct |
52 |
Correct |
2 ms |
10892 KB |
Output is correct |
53 |
Correct |
2 ms |
10892 KB |
Output is correct |
54 |
Correct |
2 ms |
10844 KB |
Output is correct |
55 |
Correct |
2 ms |
10844 KB |
Output is correct |
56 |
Correct |
2 ms |
10840 KB |
Output is correct |
57 |
Correct |
2 ms |
10960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10900 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10892 KB |
Output is correct |
17 |
Correct |
1 ms |
10888 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10840 KB |
Output is correct |
20 |
Correct |
1 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
10840 KB |
Output is correct |
22 |
Correct |
1 ms |
10860 KB |
Output is correct |
23 |
Correct |
1 ms |
10844 KB |
Output is correct |
24 |
Correct |
1 ms |
10680 KB |
Output is correct |
25 |
Correct |
1 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
11096 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
1 ms |
10844 KB |
Output is correct |
31 |
Correct |
1 ms |
10900 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10840 KB |
Output is correct |
34 |
Correct |
1 ms |
10844 KB |
Output is correct |
35 |
Correct |
1 ms |
10844 KB |
Output is correct |
36 |
Correct |
2 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10860 KB |
Output is correct |
39 |
Correct |
2 ms |
10844 KB |
Output is correct |
40 |
Correct |
1 ms |
10844 KB |
Output is correct |
41 |
Correct |
1 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
2 ms |
10844 KB |
Output is correct |
44 |
Correct |
2 ms |
10840 KB |
Output is correct |
45 |
Correct |
1 ms |
10844 KB |
Output is correct |
46 |
Correct |
2 ms |
10844 KB |
Output is correct |
47 |
Correct |
2 ms |
10840 KB |
Output is correct |
48 |
Correct |
2 ms |
10844 KB |
Output is correct |
49 |
Correct |
2 ms |
10844 KB |
Output is correct |
50 |
Correct |
1 ms |
10888 KB |
Output is correct |
51 |
Correct |
1 ms |
10844 KB |
Output is correct |
52 |
Correct |
2 ms |
10892 KB |
Output is correct |
53 |
Correct |
2 ms |
10892 KB |
Output is correct |
54 |
Correct |
2 ms |
10844 KB |
Output is correct |
55 |
Correct |
2 ms |
10844 KB |
Output is correct |
56 |
Correct |
2 ms |
10840 KB |
Output is correct |
57 |
Correct |
2 ms |
10960 KB |
Output is correct |
58 |
Correct |
21 ms |
18776 KB |
Output is correct |
59 |
Correct |
15 ms |
46940 KB |
Output is correct |
60 |
Correct |
41 ms |
53332 KB |
Output is correct |
61 |
Correct |
239 ms |
53332 KB |
Output is correct |
62 |
Correct |
856 ms |
106576 KB |
Output is correct |
63 |
Correct |
58 ms |
106580 KB |
Output is correct |
64 |
Correct |
356 ms |
106652 KB |
Output is correct |
65 |
Correct |
2 ms |
10844 KB |
Output is correct |
66 |
Correct |
2 ms |
10844 KB |
Output is correct |
67 |
Correct |
2 ms |
10844 KB |
Output is correct |
68 |
Correct |
1 ms |
10844 KB |
Output is correct |
69 |
Correct |
2 ms |
12960 KB |
Output is correct |
70 |
Correct |
2 ms |
12892 KB |
Output is correct |
71 |
Correct |
89 ms |
19036 KB |
Output is correct |
72 |
Correct |
6 ms |
18780 KB |
Output is correct |
73 |
Correct |
29 ms |
18780 KB |
Output is correct |
74 |
Correct |
16 ms |
46940 KB |
Output is correct |
75 |
Correct |
41 ms |
53340 KB |
Output is correct |
76 |
Correct |
313 ms |
53464 KB |
Output is correct |
77 |
Execution timed out |
1027 ms |
106324 KB |
Time limit exceeded |
78 |
Halted |
0 ms |
0 KB |
- |