#include <bits/stdc++.h>
#define base 31
#define fi first
#define endl "\n"
#define se second
#define NAME "FILE"
#define ll long long
//#define int long long
#define mod 1000000007
#define pii pair<int,int>
#define bit(mask,i) (mask&(1<<i))
#define lcm(a,b) ((a*b)/__gcd(a,b))
#define turn_on(mask,i) (mask|(1<<i))
#define turn_off(mask,i) (mask^(1<<i))
template <class T1, class T2>
bool maximize(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
template <class T1, class T2>
void add(T1 &a, T2 b){a += b; if (a >= mod) a -= mod;}
template <class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if (a < 0) a += mod;}
using namespace std;
int n,k,a[100010];
int mn[100010][20],l[1000010];
stack<int>st;
int get(int l,int r){
int k=__lg(r-l+1);
return min(mn[l][k],mn[r-(1<<k)+1][k]);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
if (fopen(NAME".inp","r")){
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
cin>>n>>k;
int dp[n+10][k+10];
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++){
while(st.size()&&a[st.top()]<=a[i]) st.pop();
if(st.size())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++)mn[i][0]=dp[i][j-1];
for(int j=1;(1<<j)<=n+1;j++){
for(int i=0;i+(1<<j)-1<=n;i++){
mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
}
}
for(int i=1;i<=n;i++){
minimize(dp[i][j],get(l[i],i-1)+a[i]);
minimize(dp[i][j],dp[l[i]][j]);
}
}
cout<<dp[n][k];
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:40:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:41:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(NAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2616 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2552 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2552 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2616 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2552 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2552 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2384 KB |
Output is correct |
29 |
Correct |
1 ms |
2552 KB |
Output is correct |
30 |
Correct |
1 ms |
2384 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
36 |
Correct |
1 ms |
2384 KB |
Output is correct |
37 |
Correct |
1 ms |
2384 KB |
Output is correct |
38 |
Correct |
1 ms |
2384 KB |
Output is correct |
39 |
Correct |
1 ms |
2552 KB |
Output is correct |
40 |
Correct |
1 ms |
2384 KB |
Output is correct |
41 |
Correct |
1 ms |
2384 KB |
Output is correct |
42 |
Correct |
1 ms |
2384 KB |
Output is correct |
43 |
Correct |
1 ms |
2384 KB |
Output is correct |
44 |
Correct |
1 ms |
2384 KB |
Output is correct |
45 |
Correct |
1 ms |
2384 KB |
Output is correct |
46 |
Correct |
1 ms |
2384 KB |
Output is correct |
47 |
Correct |
1 ms |
2384 KB |
Output is correct |
48 |
Correct |
1 ms |
2384 KB |
Output is correct |
49 |
Correct |
1 ms |
2384 KB |
Output is correct |
50 |
Correct |
1 ms |
2384 KB |
Output is correct |
51 |
Correct |
1 ms |
2384 KB |
Output is correct |
52 |
Correct |
1 ms |
2384 KB |
Output is correct |
53 |
Correct |
1 ms |
2552 KB |
Output is correct |
54 |
Correct |
1 ms |
2384 KB |
Output is correct |
55 |
Correct |
1 ms |
2384 KB |
Output is correct |
56 |
Correct |
1 ms |
2384 KB |
Output is correct |
57 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2616 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2552 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2552 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2384 KB |
Output is correct |
29 |
Correct |
1 ms |
2552 KB |
Output is correct |
30 |
Correct |
1 ms |
2384 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
36 |
Correct |
1 ms |
2384 KB |
Output is correct |
37 |
Correct |
1 ms |
2384 KB |
Output is correct |
38 |
Correct |
1 ms |
2384 KB |
Output is correct |
39 |
Correct |
1 ms |
2552 KB |
Output is correct |
40 |
Correct |
1 ms |
2384 KB |
Output is correct |
41 |
Correct |
1 ms |
2384 KB |
Output is correct |
42 |
Correct |
1 ms |
2384 KB |
Output is correct |
43 |
Correct |
1 ms |
2384 KB |
Output is correct |
44 |
Correct |
1 ms |
2384 KB |
Output is correct |
45 |
Correct |
1 ms |
2384 KB |
Output is correct |
46 |
Correct |
1 ms |
2384 KB |
Output is correct |
47 |
Correct |
1 ms |
2384 KB |
Output is correct |
48 |
Correct |
1 ms |
2384 KB |
Output is correct |
49 |
Correct |
1 ms |
2384 KB |
Output is correct |
50 |
Correct |
1 ms |
2384 KB |
Output is correct |
51 |
Correct |
1 ms |
2384 KB |
Output is correct |
52 |
Correct |
1 ms |
2384 KB |
Output is correct |
53 |
Correct |
1 ms |
2552 KB |
Output is correct |
54 |
Correct |
1 ms |
2384 KB |
Output is correct |
55 |
Correct |
1 ms |
2384 KB |
Output is correct |
56 |
Correct |
1 ms |
2384 KB |
Output is correct |
57 |
Correct |
1 ms |
2384 KB |
Output is correct |
58 |
Correct |
11 ms |
5968 KB |
Output is correct |
59 |
Correct |
8 ms |
8784 KB |
Output is correct |
60 |
Correct |
18 ms |
9932 KB |
Output is correct |
61 |
Correct |
138 ms |
21840 KB |
Output is correct |
62 |
Correct |
475 ms |
55620 KB |
Output is correct |
63 |
Correct |
23 ms |
17488 KB |
Output is correct |
64 |
Correct |
190 ms |
33872 KB |
Output is correct |
65 |
Correct |
1 ms |
2384 KB |
Output is correct |
66 |
Correct |
1 ms |
2384 KB |
Output is correct |
67 |
Correct |
1 ms |
2384 KB |
Output is correct |
68 |
Correct |
1 ms |
2384 KB |
Output is correct |
69 |
Correct |
1 ms |
2384 KB |
Output is correct |
70 |
Correct |
1 ms |
2384 KB |
Output is correct |
71 |
Correct |
31 ms |
8784 KB |
Output is correct |
72 |
Correct |
3 ms |
4944 KB |
Output is correct |
73 |
Correct |
10 ms |
6024 KB |
Output is correct |
74 |
Correct |
7 ms |
8784 KB |
Output is correct |
75 |
Correct |
16 ms |
9808 KB |
Output is correct |
76 |
Correct |
119 ms |
21916 KB |
Output is correct |
77 |
Correct |
383 ms |
55512 KB |
Output is correct |
78 |
Correct |
27 ms |
17488 KB |
Output is correct |
79 |
Correct |
174 ms |
33872 KB |
Output is correct |
80 |
Correct |
15 ms |
16720 KB |
Output is correct |
81 |
Correct |
46 ms |
20304 KB |
Output is correct |
82 |
Correct |
403 ms |
55404 KB |
Output is correct |
83 |
Correct |
1 ms |
2384 KB |
Output is correct |
84 |
Correct |
1 ms |
2384 KB |
Output is correct |
85 |
Correct |
28 ms |
8784 KB |
Output is correct |
86 |
Correct |
3 ms |
4944 KB |
Output is correct |
87 |
Correct |
9 ms |
6048 KB |
Output is correct |
88 |
Correct |
7 ms |
8784 KB |
Output is correct |
89 |
Correct |
19 ms |
9808 KB |
Output is correct |
90 |
Correct |
110 ms |
21840 KB |
Output is correct |
91 |
Correct |
362 ms |
55888 KB |
Output is correct |
92 |
Correct |
24 ms |
17744 KB |
Output is correct |
93 |
Correct |
178 ms |
34128 KB |
Output is correct |
94 |
Correct |
3 ms |
5112 KB |
Output is correct |
95 |
Correct |
30 ms |
8960 KB |
Output is correct |
96 |
Correct |
1 ms |
2384 KB |
Output is correct |
97 |
Correct |
1 ms |
2384 KB |
Output is correct |
98 |
Correct |
1 ms |
2384 KB |
Output is correct |
99 |
Correct |
1 ms |
2384 KB |
Output is correct |