#include<bits/stdc++.h>
#define int long long
#define forint(i,a,b) for (int i=a;i<=b;++i)
#define forinc(i,a,b) for (int i=a;i>=b;--i)
#define vt vector<int>
#define par pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=1e5+1;
const int K=101;
const int MOD=1e9+7;
int n,k;
int a[N];
int f[N][K];
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
forint(i,1,n)
{
cin >> a[i];
}
memset(f,MOD,sizeof(f));
f[1][0]=0;
forint(i,1,n)
{
f[1][i]=max(f[1][i-1],a[i]);
}
forint(i,2,k)
{
stack <par> s;
forint(j,i,n)
{
int mi=f[i-1][j-1];
while(!s.empty()&&a[s.top().se]<=a[j])
{
mi=min(mi,s.top().fi);
s.pop();
}
f[i][j]=min(f[i][s.empty() ? 0 : s.top().se],mi+a[j]);
s.push({mi,j});
}
}
cout << f[k][n];
return 0;
}
Compilation message
blocks.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
16 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
79336 KB |
Output is correct |
2 |
Correct |
37 ms |
79444 KB |
Output is correct |
3 |
Correct |
38 ms |
79444 KB |
Output is correct |
4 |
Correct |
36 ms |
79440 KB |
Output is correct |
5 |
Correct |
40 ms |
79448 KB |
Output is correct |
6 |
Correct |
36 ms |
79440 KB |
Output is correct |
7 |
Correct |
36 ms |
79408 KB |
Output is correct |
8 |
Correct |
37 ms |
79300 KB |
Output is correct |
9 |
Correct |
36 ms |
79440 KB |
Output is correct |
10 |
Correct |
39 ms |
79436 KB |
Output is correct |
11 |
Correct |
44 ms |
79444 KB |
Output is correct |
12 |
Correct |
39 ms |
79436 KB |
Output is correct |
13 |
Correct |
36 ms |
79444 KB |
Output is correct |
14 |
Correct |
35 ms |
79440 KB |
Output is correct |
15 |
Correct |
37 ms |
79432 KB |
Output is correct |
16 |
Correct |
37 ms |
79460 KB |
Output is correct |
17 |
Correct |
36 ms |
79440 KB |
Output is correct |
18 |
Correct |
42 ms |
79440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
79388 KB |
Output is correct |
2 |
Correct |
37 ms |
79440 KB |
Output is correct |
3 |
Correct |
38 ms |
79404 KB |
Output is correct |
4 |
Correct |
35 ms |
79452 KB |
Output is correct |
5 |
Correct |
35 ms |
79452 KB |
Output is correct |
6 |
Correct |
35 ms |
79452 KB |
Output is correct |
7 |
Correct |
34 ms |
79316 KB |
Output is correct |
8 |
Correct |
36 ms |
79452 KB |
Output is correct |
9 |
Correct |
36 ms |
79440 KB |
Output is correct |
10 |
Correct |
33 ms |
79452 KB |
Output is correct |
11 |
Correct |
44 ms |
79376 KB |
Output is correct |
12 |
Correct |
33 ms |
79448 KB |
Output is correct |
13 |
Correct |
42 ms |
79488 KB |
Output is correct |
14 |
Correct |
40 ms |
79416 KB |
Output is correct |
15 |
Correct |
33 ms |
79352 KB |
Output is correct |
16 |
Correct |
34 ms |
79496 KB |
Output is correct |
17 |
Correct |
38 ms |
79384 KB |
Output is correct |
18 |
Correct |
35 ms |
79256 KB |
Output is correct |
19 |
Correct |
36 ms |
79444 KB |
Output is correct |
20 |
Correct |
36 ms |
79312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
79336 KB |
Output is correct |
2 |
Correct |
37 ms |
79444 KB |
Output is correct |
3 |
Correct |
38 ms |
79444 KB |
Output is correct |
4 |
Correct |
36 ms |
79440 KB |
Output is correct |
5 |
Correct |
40 ms |
79448 KB |
Output is correct |
6 |
Correct |
36 ms |
79440 KB |
Output is correct |
7 |
Correct |
36 ms |
79408 KB |
Output is correct |
8 |
Correct |
37 ms |
79300 KB |
Output is correct |
9 |
Correct |
36 ms |
79440 KB |
Output is correct |
10 |
Correct |
39 ms |
79436 KB |
Output is correct |
11 |
Correct |
44 ms |
79444 KB |
Output is correct |
12 |
Correct |
39 ms |
79436 KB |
Output is correct |
13 |
Correct |
36 ms |
79444 KB |
Output is correct |
14 |
Correct |
35 ms |
79440 KB |
Output is correct |
15 |
Correct |
37 ms |
79432 KB |
Output is correct |
16 |
Correct |
37 ms |
79460 KB |
Output is correct |
17 |
Correct |
36 ms |
79440 KB |
Output is correct |
18 |
Correct |
42 ms |
79440 KB |
Output is correct |
19 |
Correct |
37 ms |
79388 KB |
Output is correct |
20 |
Correct |
37 ms |
79440 KB |
Output is correct |
21 |
Correct |
38 ms |
79404 KB |
Output is correct |
22 |
Correct |
35 ms |
79452 KB |
Output is correct |
23 |
Correct |
35 ms |
79452 KB |
Output is correct |
24 |
Correct |
35 ms |
79452 KB |
Output is correct |
25 |
Correct |
34 ms |
79316 KB |
Output is correct |
26 |
Correct |
36 ms |
79452 KB |
Output is correct |
27 |
Correct |
36 ms |
79440 KB |
Output is correct |
28 |
Correct |
33 ms |
79452 KB |
Output is correct |
29 |
Correct |
44 ms |
79376 KB |
Output is correct |
30 |
Correct |
33 ms |
79448 KB |
Output is correct |
31 |
Correct |
42 ms |
79488 KB |
Output is correct |
32 |
Correct |
40 ms |
79416 KB |
Output is correct |
33 |
Correct |
33 ms |
79352 KB |
Output is correct |
34 |
Correct |
34 ms |
79496 KB |
Output is correct |
35 |
Correct |
38 ms |
79384 KB |
Output is correct |
36 |
Correct |
35 ms |
79256 KB |
Output is correct |
37 |
Correct |
36 ms |
79444 KB |
Output is correct |
38 |
Correct |
36 ms |
79312 KB |
Output is correct |
39 |
Correct |
35 ms |
79452 KB |
Output is correct |
40 |
Correct |
38 ms |
79448 KB |
Output is correct |
41 |
Correct |
36 ms |
79448 KB |
Output is correct |
42 |
Correct |
35 ms |
79444 KB |
Output is correct |
43 |
Correct |
34 ms |
79444 KB |
Output is correct |
44 |
Correct |
35 ms |
79448 KB |
Output is correct |
45 |
Correct |
36 ms |
79444 KB |
Output is correct |
46 |
Correct |
40 ms |
79336 KB |
Output is correct |
47 |
Correct |
38 ms |
79440 KB |
Output is correct |
48 |
Correct |
33 ms |
79460 KB |
Output is correct |
49 |
Correct |
37 ms |
79276 KB |
Output is correct |
50 |
Correct |
37 ms |
79432 KB |
Output is correct |
51 |
Correct |
36 ms |
79440 KB |
Output is correct |
52 |
Correct |
37 ms |
79440 KB |
Output is correct |
53 |
Correct |
38 ms |
79448 KB |
Output is correct |
54 |
Correct |
36 ms |
79440 KB |
Output is correct |
55 |
Correct |
36 ms |
79444 KB |
Output is correct |
56 |
Correct |
40 ms |
79440 KB |
Output is correct |
57 |
Correct |
41 ms |
79440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
79336 KB |
Output is correct |
2 |
Correct |
37 ms |
79444 KB |
Output is correct |
3 |
Correct |
38 ms |
79444 KB |
Output is correct |
4 |
Correct |
36 ms |
79440 KB |
Output is correct |
5 |
Correct |
40 ms |
79448 KB |
Output is correct |
6 |
Correct |
36 ms |
79440 KB |
Output is correct |
7 |
Correct |
36 ms |
79408 KB |
Output is correct |
8 |
Correct |
37 ms |
79300 KB |
Output is correct |
9 |
Correct |
36 ms |
79440 KB |
Output is correct |
10 |
Correct |
39 ms |
79436 KB |
Output is correct |
11 |
Correct |
44 ms |
79444 KB |
Output is correct |
12 |
Correct |
39 ms |
79436 KB |
Output is correct |
13 |
Correct |
36 ms |
79444 KB |
Output is correct |
14 |
Correct |
35 ms |
79440 KB |
Output is correct |
15 |
Correct |
37 ms |
79432 KB |
Output is correct |
16 |
Correct |
37 ms |
79460 KB |
Output is correct |
17 |
Correct |
36 ms |
79440 KB |
Output is correct |
18 |
Correct |
42 ms |
79440 KB |
Output is correct |
19 |
Correct |
37 ms |
79388 KB |
Output is correct |
20 |
Correct |
37 ms |
79440 KB |
Output is correct |
21 |
Correct |
38 ms |
79404 KB |
Output is correct |
22 |
Correct |
35 ms |
79452 KB |
Output is correct |
23 |
Correct |
35 ms |
79452 KB |
Output is correct |
24 |
Correct |
35 ms |
79452 KB |
Output is correct |
25 |
Correct |
34 ms |
79316 KB |
Output is correct |
26 |
Correct |
36 ms |
79452 KB |
Output is correct |
27 |
Correct |
36 ms |
79440 KB |
Output is correct |
28 |
Correct |
33 ms |
79452 KB |
Output is correct |
29 |
Correct |
44 ms |
79376 KB |
Output is correct |
30 |
Correct |
33 ms |
79448 KB |
Output is correct |
31 |
Correct |
42 ms |
79488 KB |
Output is correct |
32 |
Correct |
40 ms |
79416 KB |
Output is correct |
33 |
Correct |
33 ms |
79352 KB |
Output is correct |
34 |
Correct |
34 ms |
79496 KB |
Output is correct |
35 |
Correct |
38 ms |
79384 KB |
Output is correct |
36 |
Correct |
35 ms |
79256 KB |
Output is correct |
37 |
Correct |
36 ms |
79444 KB |
Output is correct |
38 |
Correct |
36 ms |
79312 KB |
Output is correct |
39 |
Correct |
35 ms |
79452 KB |
Output is correct |
40 |
Correct |
38 ms |
79448 KB |
Output is correct |
41 |
Correct |
36 ms |
79448 KB |
Output is correct |
42 |
Correct |
35 ms |
79444 KB |
Output is correct |
43 |
Correct |
34 ms |
79444 KB |
Output is correct |
44 |
Correct |
35 ms |
79448 KB |
Output is correct |
45 |
Correct |
36 ms |
79444 KB |
Output is correct |
46 |
Correct |
40 ms |
79336 KB |
Output is correct |
47 |
Correct |
38 ms |
79440 KB |
Output is correct |
48 |
Correct |
33 ms |
79460 KB |
Output is correct |
49 |
Correct |
37 ms |
79276 KB |
Output is correct |
50 |
Correct |
37 ms |
79432 KB |
Output is correct |
51 |
Correct |
36 ms |
79440 KB |
Output is correct |
52 |
Correct |
37 ms |
79440 KB |
Output is correct |
53 |
Correct |
38 ms |
79448 KB |
Output is correct |
54 |
Correct |
36 ms |
79440 KB |
Output is correct |
55 |
Correct |
36 ms |
79444 KB |
Output is correct |
56 |
Correct |
40 ms |
79440 KB |
Output is correct |
57 |
Correct |
41 ms |
79440 KB |
Output is correct |
58 |
Incorrect |
37 ms |
79448 KB |
Output isn't correct |
59 |
Halted |
0 ms |
0 KB |
- |