#include <bits/stdc++.h>
#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define fi first
#define se second
#define oo 1e18
#define bit(x,i) (((x)>>(i))&1)
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int K = 1e2 + 5;
int n,k,a[N];
ll dp[K][N],f[N],L[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>k;
fo(i,1,n)
cin>>a[i];
memset(dp,0x3f,sizeof(dp));
dp[0][0] = 0;
f[0] = 1e18;
fo(i,1,k)
{
int mx = 0;
fo(j,1,n)
{
mx = max(mx, a[j]);
if(i == 1)
dp[i][j] = f[j] = mx;
L[j] = j-1;
while(L[j] > 0 && a[L[j]] <= a[j])
{
f[L[L[j]]] = min(f[L[L[j]]], f[L[j]]);
L[j] = L[L[j]];
}
// if(i == 3 && j == 6)
// cout<<L[j]<<' '<<f[L[j]]<<'\n';
if(i <= j)
dp[i][j] = min({dp[i][j], dp[i][L[j]], f[L[j]] + a[j]});
// cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
}
fo(j,0,n)
f[j] = dp[i][j];
}
cout<<dp[k][n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
82508 KB |
Output is correct |
2 |
Correct |
35 ms |
82380 KB |
Output is correct |
3 |
Correct |
33 ms |
82528 KB |
Output is correct |
4 |
Correct |
36 ms |
82384 KB |
Output is correct |
5 |
Correct |
34 ms |
82508 KB |
Output is correct |
6 |
Correct |
37 ms |
82460 KB |
Output is correct |
7 |
Correct |
35 ms |
82500 KB |
Output is correct |
8 |
Correct |
33 ms |
82516 KB |
Output is correct |
9 |
Correct |
36 ms |
82416 KB |
Output is correct |
10 |
Correct |
33 ms |
82440 KB |
Output is correct |
11 |
Correct |
41 ms |
82412 KB |
Output is correct |
12 |
Correct |
35 ms |
82472 KB |
Output is correct |
13 |
Correct |
32 ms |
82516 KB |
Output is correct |
14 |
Correct |
34 ms |
82396 KB |
Output is correct |
15 |
Correct |
33 ms |
82424 KB |
Output is correct |
16 |
Correct |
33 ms |
82404 KB |
Output is correct |
17 |
Correct |
34 ms |
82440 KB |
Output is correct |
18 |
Correct |
34 ms |
82424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
82404 KB |
Output is correct |
2 |
Correct |
33 ms |
82504 KB |
Output is correct |
3 |
Correct |
33 ms |
82428 KB |
Output is correct |
4 |
Correct |
35 ms |
82436 KB |
Output is correct |
5 |
Correct |
35 ms |
82532 KB |
Output is correct |
6 |
Correct |
38 ms |
82400 KB |
Output is correct |
7 |
Correct |
32 ms |
82516 KB |
Output is correct |
8 |
Correct |
33 ms |
82452 KB |
Output is correct |
9 |
Correct |
34 ms |
82452 KB |
Output is correct |
10 |
Correct |
39 ms |
82392 KB |
Output is correct |
11 |
Correct |
34 ms |
82508 KB |
Output is correct |
12 |
Correct |
34 ms |
82496 KB |
Output is correct |
13 |
Correct |
34 ms |
82464 KB |
Output is correct |
14 |
Correct |
33 ms |
82460 KB |
Output is correct |
15 |
Correct |
33 ms |
82412 KB |
Output is correct |
16 |
Correct |
34 ms |
82380 KB |
Output is correct |
17 |
Correct |
33 ms |
82516 KB |
Output is correct |
18 |
Correct |
33 ms |
82468 KB |
Output is correct |
19 |
Correct |
34 ms |
82516 KB |
Output is correct |
20 |
Correct |
36 ms |
82424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
82508 KB |
Output is correct |
2 |
Correct |
35 ms |
82380 KB |
Output is correct |
3 |
Correct |
33 ms |
82528 KB |
Output is correct |
4 |
Correct |
36 ms |
82384 KB |
Output is correct |
5 |
Correct |
34 ms |
82508 KB |
Output is correct |
6 |
Correct |
37 ms |
82460 KB |
Output is correct |
7 |
Correct |
35 ms |
82500 KB |
Output is correct |
8 |
Correct |
33 ms |
82516 KB |
Output is correct |
9 |
Correct |
36 ms |
82416 KB |
Output is correct |
10 |
Correct |
33 ms |
82440 KB |
Output is correct |
11 |
Correct |
41 ms |
82412 KB |
Output is correct |
12 |
Correct |
35 ms |
82472 KB |
Output is correct |
13 |
Correct |
32 ms |
82516 KB |
Output is correct |
14 |
Correct |
34 ms |
82396 KB |
Output is correct |
15 |
Correct |
33 ms |
82424 KB |
Output is correct |
16 |
Correct |
33 ms |
82404 KB |
Output is correct |
17 |
Correct |
34 ms |
82440 KB |
Output is correct |
18 |
Correct |
34 ms |
82424 KB |
Output is correct |
19 |
Correct |
34 ms |
82404 KB |
Output is correct |
20 |
Correct |
33 ms |
82504 KB |
Output is correct |
21 |
Correct |
33 ms |
82428 KB |
Output is correct |
22 |
Correct |
35 ms |
82436 KB |
Output is correct |
23 |
Correct |
35 ms |
82532 KB |
Output is correct |
24 |
Correct |
38 ms |
82400 KB |
Output is correct |
25 |
Correct |
32 ms |
82516 KB |
Output is correct |
26 |
Correct |
33 ms |
82452 KB |
Output is correct |
27 |
Correct |
34 ms |
82452 KB |
Output is correct |
28 |
Correct |
39 ms |
82392 KB |
Output is correct |
29 |
Correct |
34 ms |
82508 KB |
Output is correct |
30 |
Correct |
34 ms |
82496 KB |
Output is correct |
31 |
Correct |
34 ms |
82464 KB |
Output is correct |
32 |
Correct |
33 ms |
82460 KB |
Output is correct |
33 |
Correct |
33 ms |
82412 KB |
Output is correct |
34 |
Correct |
34 ms |
82380 KB |
Output is correct |
35 |
Correct |
33 ms |
82516 KB |
Output is correct |
36 |
Correct |
33 ms |
82468 KB |
Output is correct |
37 |
Correct |
34 ms |
82516 KB |
Output is correct |
38 |
Correct |
36 ms |
82424 KB |
Output is correct |
39 |
Correct |
33 ms |
82424 KB |
Output is correct |
40 |
Correct |
34 ms |
82516 KB |
Output is correct |
41 |
Correct |
34 ms |
82380 KB |
Output is correct |
42 |
Correct |
36 ms |
82380 KB |
Output is correct |
43 |
Correct |
32 ms |
82516 KB |
Output is correct |
44 |
Correct |
34 ms |
82424 KB |
Output is correct |
45 |
Correct |
32 ms |
82492 KB |
Output is correct |
46 |
Correct |
33 ms |
82388 KB |
Output is correct |
47 |
Correct |
33 ms |
82436 KB |
Output is correct |
48 |
Correct |
34 ms |
82380 KB |
Output is correct |
49 |
Correct |
34 ms |
82528 KB |
Output is correct |
50 |
Correct |
33 ms |
82440 KB |
Output is correct |
51 |
Correct |
33 ms |
82488 KB |
Output is correct |
52 |
Correct |
33 ms |
82452 KB |
Output is correct |
53 |
Correct |
33 ms |
82380 KB |
Output is correct |
54 |
Correct |
33 ms |
82392 KB |
Output is correct |
55 |
Correct |
34 ms |
82396 KB |
Output is correct |
56 |
Correct |
34 ms |
82380 KB |
Output is correct |
57 |
Correct |
33 ms |
82440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
82508 KB |
Output is correct |
2 |
Correct |
35 ms |
82380 KB |
Output is correct |
3 |
Correct |
33 ms |
82528 KB |
Output is correct |
4 |
Correct |
36 ms |
82384 KB |
Output is correct |
5 |
Correct |
34 ms |
82508 KB |
Output is correct |
6 |
Correct |
37 ms |
82460 KB |
Output is correct |
7 |
Correct |
35 ms |
82500 KB |
Output is correct |
8 |
Correct |
33 ms |
82516 KB |
Output is correct |
9 |
Correct |
36 ms |
82416 KB |
Output is correct |
10 |
Correct |
33 ms |
82440 KB |
Output is correct |
11 |
Correct |
41 ms |
82412 KB |
Output is correct |
12 |
Correct |
35 ms |
82472 KB |
Output is correct |
13 |
Correct |
32 ms |
82516 KB |
Output is correct |
14 |
Correct |
34 ms |
82396 KB |
Output is correct |
15 |
Correct |
33 ms |
82424 KB |
Output is correct |
16 |
Correct |
33 ms |
82404 KB |
Output is correct |
17 |
Correct |
34 ms |
82440 KB |
Output is correct |
18 |
Correct |
34 ms |
82424 KB |
Output is correct |
19 |
Correct |
34 ms |
82404 KB |
Output is correct |
20 |
Correct |
33 ms |
82504 KB |
Output is correct |
21 |
Correct |
33 ms |
82428 KB |
Output is correct |
22 |
Correct |
35 ms |
82436 KB |
Output is correct |
23 |
Correct |
35 ms |
82532 KB |
Output is correct |
24 |
Correct |
38 ms |
82400 KB |
Output is correct |
25 |
Correct |
32 ms |
82516 KB |
Output is correct |
26 |
Correct |
33 ms |
82452 KB |
Output is correct |
27 |
Correct |
34 ms |
82452 KB |
Output is correct |
28 |
Correct |
39 ms |
82392 KB |
Output is correct |
29 |
Correct |
34 ms |
82508 KB |
Output is correct |
30 |
Correct |
34 ms |
82496 KB |
Output is correct |
31 |
Correct |
34 ms |
82464 KB |
Output is correct |
32 |
Correct |
33 ms |
82460 KB |
Output is correct |
33 |
Correct |
33 ms |
82412 KB |
Output is correct |
34 |
Correct |
34 ms |
82380 KB |
Output is correct |
35 |
Correct |
33 ms |
82516 KB |
Output is correct |
36 |
Correct |
33 ms |
82468 KB |
Output is correct |
37 |
Correct |
34 ms |
82516 KB |
Output is correct |
38 |
Correct |
36 ms |
82424 KB |
Output is correct |
39 |
Correct |
33 ms |
82424 KB |
Output is correct |
40 |
Correct |
34 ms |
82516 KB |
Output is correct |
41 |
Correct |
34 ms |
82380 KB |
Output is correct |
42 |
Correct |
36 ms |
82380 KB |
Output is correct |
43 |
Correct |
32 ms |
82516 KB |
Output is correct |
44 |
Correct |
34 ms |
82424 KB |
Output is correct |
45 |
Correct |
32 ms |
82492 KB |
Output is correct |
46 |
Correct |
33 ms |
82388 KB |
Output is correct |
47 |
Correct |
33 ms |
82436 KB |
Output is correct |
48 |
Correct |
34 ms |
82380 KB |
Output is correct |
49 |
Correct |
34 ms |
82528 KB |
Output is correct |
50 |
Correct |
33 ms |
82440 KB |
Output is correct |
51 |
Correct |
33 ms |
82488 KB |
Output is correct |
52 |
Correct |
33 ms |
82452 KB |
Output is correct |
53 |
Correct |
33 ms |
82380 KB |
Output is correct |
54 |
Correct |
33 ms |
82392 KB |
Output is correct |
55 |
Correct |
34 ms |
82396 KB |
Output is correct |
56 |
Correct |
34 ms |
82380 KB |
Output is correct |
57 |
Correct |
33 ms |
82440 KB |
Output is correct |
58 |
Correct |
39 ms |
82592 KB |
Output is correct |
59 |
Correct |
39 ms |
83404 KB |
Output is correct |
60 |
Correct |
41 ms |
83352 KB |
Output is correct |
61 |
Correct |
64 ms |
83260 KB |
Output is correct |
62 |
Correct |
124 ms |
84476 KB |
Output is correct |
63 |
Correct |
46 ms |
84424 KB |
Output is correct |
64 |
Correct |
83 ms |
84340 KB |
Output is correct |
65 |
Correct |
37 ms |
82500 KB |
Output is correct |
66 |
Correct |
33 ms |
82452 KB |
Output is correct |
67 |
Correct |
32 ms |
82516 KB |
Output is correct |
68 |
Correct |
32 ms |
82508 KB |
Output is correct |
69 |
Correct |
35 ms |
82448 KB |
Output is correct |
70 |
Correct |
34 ms |
82500 KB |
Output is correct |
71 |
Correct |
43 ms |
82636 KB |
Output is correct |
72 |
Correct |
34 ms |
82644 KB |
Output is correct |
73 |
Correct |
39 ms |
82688 KB |
Output is correct |
74 |
Correct |
39 ms |
83300 KB |
Output is correct |
75 |
Correct |
41 ms |
83260 KB |
Output is correct |
76 |
Correct |
74 ms |
83380 KB |
Output is correct |
77 |
Correct |
151 ms |
84556 KB |
Output is correct |
78 |
Correct |
47 ms |
84428 KB |
Output is correct |
79 |
Correct |
91 ms |
84412 KB |
Output is correct |
80 |
Correct |
49 ms |
84420 KB |
Output is correct |
81 |
Correct |
52 ms |
84452 KB |
Output is correct |
82 |
Correct |
154 ms |
84452 KB |
Output is correct |
83 |
Correct |
34 ms |
82440 KB |
Output is correct |
84 |
Correct |
33 ms |
82516 KB |
Output is correct |
85 |
Correct |
41 ms |
82588 KB |
Output is correct |
86 |
Correct |
34 ms |
82660 KB |
Output is correct |
87 |
Correct |
40 ms |
82676 KB |
Output is correct |
88 |
Correct |
38 ms |
83296 KB |
Output is correct |
89 |
Correct |
40 ms |
83256 KB |
Output is correct |
90 |
Correct |
64 ms |
83360 KB |
Output is correct |
91 |
Correct |
126 ms |
84452 KB |
Output is correct |
92 |
Correct |
43 ms |
84508 KB |
Output is correct |
93 |
Correct |
79 ms |
84452 KB |
Output is correct |
94 |
Correct |
38 ms |
82664 KB |
Output is correct |
95 |
Correct |
42 ms |
82660 KB |
Output is correct |
96 |
Correct |
33 ms |
82508 KB |
Output is correct |
97 |
Correct |
32 ms |
82508 KB |
Output is correct |
98 |
Correct |
35 ms |
82384 KB |
Output is correct |
99 |
Correct |
37 ms |
82452 KB |
Output is correct |