#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
int n,m;
int a[100001];
int dp[100001][101];
int t[101][400001];
int marc[100001];
void update(int v,int tl,int tr,int pos,int x,int ind){
if(tl==tr){
t[ind][v]=x;
}
else{
int m=(tl+tr)/2;
if(pos<=m){
update(2*v,tl,m,pos,x,ind);
}
else{
update(2*v+1,m+1,tr,pos,x,ind);
}
t[ind][v]=min(t[ind][2*v],t[ind][2*v+1]);
}
}
int get(int v,int tl,int tr,int l,int r,int ind){
if(tr<l||tl>r){
return 1e9;
}
if(tl>=l&&tr<=r){
return t[ind][v];
}
int m=(tl+tr)/2;
int f1=get(2*v,tl,m,l,r,ind);
int f2=get(2*v+1,m+1,tr,l,r,ind);
int f=min(f1,f2);
return f;
}
void make(){
stack<int> st;
for(int k=n;k>=1;k--){
while(!st.empty()&&a[k]>a[st.top()]){
int f=a[st.top()];
marc[st.top()]=k;
st.pop();
}
st.push(k);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i=0;i<=100000;i++){
for(int j=0;j<=100;j++){
dp[i][j]=1e9;
}
}
cin>>n>>m;
int mx=0;
for(int k=1;k<=n;k++){
cin>>a[k];
mx=max(mx,a[k]);
dp[k][1]=mx;
update(1,1,n,k,mx,1);
}
make();
for(int k=2;k<=m;k++){
for(int i=k;i<=n;i++){
int f=marc[i];
f=max(f,k-1);
int mn1=dp[i-1][k-1]+a[i];
int mn=get(1,1,n,f,i-1,k-1);
mn=mn+a[i];
int f1=dp[f][k];
mn=min(mn,f1);
mn=min(mn,mn1);
dp[i][k]=mn;
update(1,1,n,i,mn,k);
}
}
cout<<dp[n][m];
return 0;
}
Compilation message
blocks.cpp: In function 'void make()':
blocks.cpp:48:8: warning: unused variable 'f' [-Wunused-variable]
48 | int f=a[st.top()];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
39764 KB |
Output is correct |
2 |
Correct |
22 ms |
39764 KB |
Output is correct |
3 |
Correct |
20 ms |
39856 KB |
Output is correct |
4 |
Correct |
25 ms |
39732 KB |
Output is correct |
5 |
Correct |
20 ms |
39852 KB |
Output is correct |
6 |
Correct |
20 ms |
39736 KB |
Output is correct |
7 |
Correct |
21 ms |
39788 KB |
Output is correct |
8 |
Correct |
19 ms |
39784 KB |
Output is correct |
9 |
Correct |
26 ms |
39788 KB |
Output is correct |
10 |
Correct |
20 ms |
39860 KB |
Output is correct |
11 |
Correct |
20 ms |
39892 KB |
Output is correct |
12 |
Correct |
22 ms |
39768 KB |
Output is correct |
13 |
Correct |
25 ms |
39884 KB |
Output is correct |
14 |
Correct |
20 ms |
39856 KB |
Output is correct |
15 |
Correct |
22 ms |
39892 KB |
Output is correct |
16 |
Correct |
20 ms |
39840 KB |
Output is correct |
17 |
Correct |
20 ms |
39796 KB |
Output is correct |
18 |
Correct |
20 ms |
39812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
39792 KB |
Output is correct |
2 |
Correct |
20 ms |
39824 KB |
Output is correct |
3 |
Correct |
20 ms |
39796 KB |
Output is correct |
4 |
Correct |
20 ms |
39764 KB |
Output is correct |
5 |
Correct |
20 ms |
39800 KB |
Output is correct |
6 |
Correct |
20 ms |
39852 KB |
Output is correct |
7 |
Correct |
23 ms |
39828 KB |
Output is correct |
8 |
Correct |
20 ms |
39764 KB |
Output is correct |
9 |
Correct |
20 ms |
39804 KB |
Output is correct |
10 |
Correct |
20 ms |
39764 KB |
Output is correct |
11 |
Correct |
20 ms |
39852 KB |
Output is correct |
12 |
Correct |
20 ms |
39780 KB |
Output is correct |
13 |
Correct |
22 ms |
39864 KB |
Output is correct |
14 |
Correct |
23 ms |
39880 KB |
Output is correct |
15 |
Correct |
20 ms |
39824 KB |
Output is correct |
16 |
Correct |
22 ms |
39956 KB |
Output is correct |
17 |
Correct |
21 ms |
39884 KB |
Output is correct |
18 |
Correct |
28 ms |
39908 KB |
Output is correct |
19 |
Correct |
23 ms |
39892 KB |
Output is correct |
20 |
Correct |
20 ms |
39920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
39764 KB |
Output is correct |
2 |
Correct |
22 ms |
39764 KB |
Output is correct |
3 |
Correct |
20 ms |
39856 KB |
Output is correct |
4 |
Correct |
25 ms |
39732 KB |
Output is correct |
5 |
Correct |
20 ms |
39852 KB |
Output is correct |
6 |
Correct |
20 ms |
39736 KB |
Output is correct |
7 |
Correct |
21 ms |
39788 KB |
Output is correct |
8 |
Correct |
19 ms |
39784 KB |
Output is correct |
9 |
Correct |
26 ms |
39788 KB |
Output is correct |
10 |
Correct |
20 ms |
39860 KB |
Output is correct |
11 |
Correct |
20 ms |
39892 KB |
Output is correct |
12 |
Correct |
22 ms |
39768 KB |
Output is correct |
13 |
Correct |
25 ms |
39884 KB |
Output is correct |
14 |
Correct |
20 ms |
39856 KB |
Output is correct |
15 |
Correct |
22 ms |
39892 KB |
Output is correct |
16 |
Correct |
20 ms |
39840 KB |
Output is correct |
17 |
Correct |
20 ms |
39796 KB |
Output is correct |
18 |
Correct |
20 ms |
39812 KB |
Output is correct |
19 |
Correct |
20 ms |
39792 KB |
Output is correct |
20 |
Correct |
20 ms |
39824 KB |
Output is correct |
21 |
Correct |
20 ms |
39796 KB |
Output is correct |
22 |
Correct |
20 ms |
39764 KB |
Output is correct |
23 |
Correct |
20 ms |
39800 KB |
Output is correct |
24 |
Correct |
20 ms |
39852 KB |
Output is correct |
25 |
Correct |
23 ms |
39828 KB |
Output is correct |
26 |
Correct |
20 ms |
39764 KB |
Output is correct |
27 |
Correct |
20 ms |
39804 KB |
Output is correct |
28 |
Correct |
20 ms |
39764 KB |
Output is correct |
29 |
Correct |
20 ms |
39852 KB |
Output is correct |
30 |
Correct |
20 ms |
39780 KB |
Output is correct |
31 |
Correct |
22 ms |
39864 KB |
Output is correct |
32 |
Correct |
23 ms |
39880 KB |
Output is correct |
33 |
Correct |
20 ms |
39824 KB |
Output is correct |
34 |
Correct |
22 ms |
39956 KB |
Output is correct |
35 |
Correct |
21 ms |
39884 KB |
Output is correct |
36 |
Correct |
28 ms |
39908 KB |
Output is correct |
37 |
Correct |
23 ms |
39892 KB |
Output is correct |
38 |
Correct |
20 ms |
39920 KB |
Output is correct |
39 |
Correct |
24 ms |
40208 KB |
Output is correct |
40 |
Correct |
20 ms |
39836 KB |
Output is correct |
41 |
Correct |
20 ms |
39764 KB |
Output is correct |
42 |
Correct |
23 ms |
39808 KB |
Output is correct |
43 |
Correct |
20 ms |
39764 KB |
Output is correct |
44 |
Correct |
20 ms |
39892 KB |
Output is correct |
45 |
Correct |
20 ms |
39764 KB |
Output is correct |
46 |
Correct |
26 ms |
39812 KB |
Output is correct |
47 |
Correct |
26 ms |
39848 KB |
Output is correct |
48 |
Correct |
20 ms |
39816 KB |
Output is correct |
49 |
Correct |
20 ms |
39764 KB |
Output is correct |
50 |
Correct |
20 ms |
39888 KB |
Output is correct |
51 |
Correct |
20 ms |
40016 KB |
Output is correct |
52 |
Correct |
21 ms |
40028 KB |
Output is correct |
53 |
Correct |
22 ms |
40556 KB |
Output is correct |
54 |
Correct |
21 ms |
40620 KB |
Output is correct |
55 |
Correct |
22 ms |
40280 KB |
Output is correct |
56 |
Correct |
20 ms |
40612 KB |
Output is correct |
57 |
Correct |
24 ms |
40668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
39764 KB |
Output is correct |
2 |
Correct |
22 ms |
39764 KB |
Output is correct |
3 |
Correct |
20 ms |
39856 KB |
Output is correct |
4 |
Correct |
25 ms |
39732 KB |
Output is correct |
5 |
Correct |
20 ms |
39852 KB |
Output is correct |
6 |
Correct |
20 ms |
39736 KB |
Output is correct |
7 |
Correct |
21 ms |
39788 KB |
Output is correct |
8 |
Correct |
19 ms |
39784 KB |
Output is correct |
9 |
Correct |
26 ms |
39788 KB |
Output is correct |
10 |
Correct |
20 ms |
39860 KB |
Output is correct |
11 |
Correct |
20 ms |
39892 KB |
Output is correct |
12 |
Correct |
22 ms |
39768 KB |
Output is correct |
13 |
Correct |
25 ms |
39884 KB |
Output is correct |
14 |
Correct |
20 ms |
39856 KB |
Output is correct |
15 |
Correct |
22 ms |
39892 KB |
Output is correct |
16 |
Correct |
20 ms |
39840 KB |
Output is correct |
17 |
Correct |
20 ms |
39796 KB |
Output is correct |
18 |
Correct |
20 ms |
39812 KB |
Output is correct |
19 |
Correct |
20 ms |
39792 KB |
Output is correct |
20 |
Correct |
20 ms |
39824 KB |
Output is correct |
21 |
Correct |
20 ms |
39796 KB |
Output is correct |
22 |
Correct |
20 ms |
39764 KB |
Output is correct |
23 |
Correct |
20 ms |
39800 KB |
Output is correct |
24 |
Correct |
20 ms |
39852 KB |
Output is correct |
25 |
Correct |
23 ms |
39828 KB |
Output is correct |
26 |
Correct |
20 ms |
39764 KB |
Output is correct |
27 |
Correct |
20 ms |
39804 KB |
Output is correct |
28 |
Correct |
20 ms |
39764 KB |
Output is correct |
29 |
Correct |
20 ms |
39852 KB |
Output is correct |
30 |
Correct |
20 ms |
39780 KB |
Output is correct |
31 |
Correct |
22 ms |
39864 KB |
Output is correct |
32 |
Correct |
23 ms |
39880 KB |
Output is correct |
33 |
Correct |
20 ms |
39824 KB |
Output is correct |
34 |
Correct |
22 ms |
39956 KB |
Output is correct |
35 |
Correct |
21 ms |
39884 KB |
Output is correct |
36 |
Correct |
28 ms |
39908 KB |
Output is correct |
37 |
Correct |
23 ms |
39892 KB |
Output is correct |
38 |
Correct |
20 ms |
39920 KB |
Output is correct |
39 |
Correct |
24 ms |
40208 KB |
Output is correct |
40 |
Correct |
20 ms |
39836 KB |
Output is correct |
41 |
Correct |
20 ms |
39764 KB |
Output is correct |
42 |
Correct |
23 ms |
39808 KB |
Output is correct |
43 |
Correct |
20 ms |
39764 KB |
Output is correct |
44 |
Correct |
20 ms |
39892 KB |
Output is correct |
45 |
Correct |
20 ms |
39764 KB |
Output is correct |
46 |
Correct |
26 ms |
39812 KB |
Output is correct |
47 |
Correct |
26 ms |
39848 KB |
Output is correct |
48 |
Correct |
20 ms |
39816 KB |
Output is correct |
49 |
Correct |
20 ms |
39764 KB |
Output is correct |
50 |
Correct |
20 ms |
39888 KB |
Output is correct |
51 |
Correct |
20 ms |
40016 KB |
Output is correct |
52 |
Correct |
21 ms |
40028 KB |
Output is correct |
53 |
Correct |
22 ms |
40556 KB |
Output is correct |
54 |
Correct |
21 ms |
40620 KB |
Output is correct |
55 |
Correct |
22 ms |
40280 KB |
Output is correct |
56 |
Correct |
20 ms |
40612 KB |
Output is correct |
57 |
Correct |
24 ms |
40668 KB |
Output is correct |
58 |
Correct |
84 ms |
43232 KB |
Output is correct |
59 |
Correct |
36 ms |
40708 KB |
Output is correct |
60 |
Correct |
108 ms |
43848 KB |
Output is correct |
61 |
Execution timed out |
1050 ms |
79908 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |