#include <bits/stdc++.h>
#include <iostream>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int N = 1e5 + 10;
const int M = 110;
const long long INF = 1e17;
long long n,k;
long long dp[N + 1][M + 1];
long long a[N + 1];
int main(){
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
memset(dp,0x3f3f3f3f,sizeof(dp));
dp[0][1] = 0;
for(int i = 1; i <= n; i++) dp[i][1] = max(dp[i - 1][1], a[i]);
for(int i = 2; i <= k; i++){
stack<pair<long long,int>>stk;
for(int j = i; j <= n; j++){
long long mn = dp[j - 1][i - 1];
while(stk.size() && a[stk.top().second] <= a[j]){
mn = min(mn, stk.top().first);
stk.pop();
}
dp[j][i] = min(dp[stk.size() ? stk.top().second : 0][i], mn + a[j]);
stk.push({mn,j});
}
}
cout << dp[n][k] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87116 KB |
Output is correct |
2 |
Correct |
32 ms |
87064 KB |
Output is correct |
3 |
Correct |
32 ms |
87132 KB |
Output is correct |
4 |
Correct |
41 ms |
87168 KB |
Output is correct |
5 |
Correct |
32 ms |
87116 KB |
Output is correct |
6 |
Correct |
33 ms |
87128 KB |
Output is correct |
7 |
Correct |
37 ms |
87132 KB |
Output is correct |
8 |
Correct |
33 ms |
87116 KB |
Output is correct |
9 |
Correct |
36 ms |
87188 KB |
Output is correct |
10 |
Correct |
34 ms |
87092 KB |
Output is correct |
11 |
Correct |
33 ms |
87116 KB |
Output is correct |
12 |
Correct |
32 ms |
87068 KB |
Output is correct |
13 |
Correct |
33 ms |
87136 KB |
Output is correct |
14 |
Correct |
32 ms |
87200 KB |
Output is correct |
15 |
Correct |
33 ms |
87140 KB |
Output is correct |
16 |
Correct |
33 ms |
87068 KB |
Output is correct |
17 |
Correct |
32 ms |
87116 KB |
Output is correct |
18 |
Correct |
32 ms |
87116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87108 KB |
Output is correct |
2 |
Correct |
32 ms |
87148 KB |
Output is correct |
3 |
Correct |
31 ms |
87184 KB |
Output is correct |
4 |
Correct |
33 ms |
87132 KB |
Output is correct |
5 |
Correct |
35 ms |
87092 KB |
Output is correct |
6 |
Correct |
38 ms |
87192 KB |
Output is correct |
7 |
Correct |
32 ms |
87124 KB |
Output is correct |
8 |
Correct |
32 ms |
87156 KB |
Output is correct |
9 |
Correct |
32 ms |
87072 KB |
Output is correct |
10 |
Correct |
32 ms |
87152 KB |
Output is correct |
11 |
Correct |
33 ms |
87080 KB |
Output is correct |
12 |
Correct |
33 ms |
87128 KB |
Output is correct |
13 |
Correct |
32 ms |
87252 KB |
Output is correct |
14 |
Correct |
32 ms |
87180 KB |
Output is correct |
15 |
Correct |
32 ms |
87168 KB |
Output is correct |
16 |
Correct |
33 ms |
87164 KB |
Output is correct |
17 |
Correct |
33 ms |
87180 KB |
Output is correct |
18 |
Correct |
33 ms |
87112 KB |
Output is correct |
19 |
Correct |
33 ms |
87192 KB |
Output is correct |
20 |
Correct |
34 ms |
87188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87116 KB |
Output is correct |
2 |
Correct |
32 ms |
87064 KB |
Output is correct |
3 |
Correct |
32 ms |
87132 KB |
Output is correct |
4 |
Correct |
41 ms |
87168 KB |
Output is correct |
5 |
Correct |
32 ms |
87116 KB |
Output is correct |
6 |
Correct |
33 ms |
87128 KB |
Output is correct |
7 |
Correct |
37 ms |
87132 KB |
Output is correct |
8 |
Correct |
33 ms |
87116 KB |
Output is correct |
9 |
Correct |
36 ms |
87188 KB |
Output is correct |
10 |
Correct |
34 ms |
87092 KB |
Output is correct |
11 |
Correct |
33 ms |
87116 KB |
Output is correct |
12 |
Correct |
32 ms |
87068 KB |
Output is correct |
13 |
Correct |
33 ms |
87136 KB |
Output is correct |
14 |
Correct |
32 ms |
87200 KB |
Output is correct |
15 |
Correct |
33 ms |
87140 KB |
Output is correct |
16 |
Correct |
33 ms |
87068 KB |
Output is correct |
17 |
Correct |
32 ms |
87116 KB |
Output is correct |
18 |
Correct |
32 ms |
87116 KB |
Output is correct |
19 |
Correct |
32 ms |
87108 KB |
Output is correct |
20 |
Correct |
32 ms |
87148 KB |
Output is correct |
21 |
Correct |
31 ms |
87184 KB |
Output is correct |
22 |
Correct |
33 ms |
87132 KB |
Output is correct |
23 |
Correct |
35 ms |
87092 KB |
Output is correct |
24 |
Correct |
38 ms |
87192 KB |
Output is correct |
25 |
Correct |
32 ms |
87124 KB |
Output is correct |
26 |
Correct |
32 ms |
87156 KB |
Output is correct |
27 |
Correct |
32 ms |
87072 KB |
Output is correct |
28 |
Correct |
32 ms |
87152 KB |
Output is correct |
29 |
Correct |
33 ms |
87080 KB |
Output is correct |
30 |
Correct |
33 ms |
87128 KB |
Output is correct |
31 |
Correct |
32 ms |
87252 KB |
Output is correct |
32 |
Correct |
32 ms |
87180 KB |
Output is correct |
33 |
Correct |
32 ms |
87168 KB |
Output is correct |
34 |
Correct |
33 ms |
87164 KB |
Output is correct |
35 |
Correct |
33 ms |
87180 KB |
Output is correct |
36 |
Correct |
33 ms |
87112 KB |
Output is correct |
37 |
Correct |
33 ms |
87192 KB |
Output is correct |
38 |
Correct |
34 ms |
87188 KB |
Output is correct |
39 |
Correct |
33 ms |
87084 KB |
Output is correct |
40 |
Correct |
34 ms |
87144 KB |
Output is correct |
41 |
Correct |
35 ms |
87180 KB |
Output is correct |
42 |
Correct |
35 ms |
87156 KB |
Output is correct |
43 |
Correct |
33 ms |
87124 KB |
Output is correct |
44 |
Correct |
35 ms |
87156 KB |
Output is correct |
45 |
Correct |
34 ms |
87208 KB |
Output is correct |
46 |
Correct |
34 ms |
87124 KB |
Output is correct |
47 |
Correct |
33 ms |
87088 KB |
Output is correct |
48 |
Correct |
38 ms |
87120 KB |
Output is correct |
49 |
Correct |
33 ms |
87124 KB |
Output is correct |
50 |
Correct |
34 ms |
87192 KB |
Output is correct |
51 |
Correct |
32 ms |
87124 KB |
Output is correct |
52 |
Correct |
32 ms |
87156 KB |
Output is correct |
53 |
Correct |
38 ms |
87088 KB |
Output is correct |
54 |
Correct |
33 ms |
87196 KB |
Output is correct |
55 |
Correct |
33 ms |
87084 KB |
Output is correct |
56 |
Correct |
33 ms |
87192 KB |
Output is correct |
57 |
Correct |
33 ms |
87124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87116 KB |
Output is correct |
2 |
Correct |
32 ms |
87064 KB |
Output is correct |
3 |
Correct |
32 ms |
87132 KB |
Output is correct |
4 |
Correct |
41 ms |
87168 KB |
Output is correct |
5 |
Correct |
32 ms |
87116 KB |
Output is correct |
6 |
Correct |
33 ms |
87128 KB |
Output is correct |
7 |
Correct |
37 ms |
87132 KB |
Output is correct |
8 |
Correct |
33 ms |
87116 KB |
Output is correct |
9 |
Correct |
36 ms |
87188 KB |
Output is correct |
10 |
Correct |
34 ms |
87092 KB |
Output is correct |
11 |
Correct |
33 ms |
87116 KB |
Output is correct |
12 |
Correct |
32 ms |
87068 KB |
Output is correct |
13 |
Correct |
33 ms |
87136 KB |
Output is correct |
14 |
Correct |
32 ms |
87200 KB |
Output is correct |
15 |
Correct |
33 ms |
87140 KB |
Output is correct |
16 |
Correct |
33 ms |
87068 KB |
Output is correct |
17 |
Correct |
32 ms |
87116 KB |
Output is correct |
18 |
Correct |
32 ms |
87116 KB |
Output is correct |
19 |
Correct |
32 ms |
87108 KB |
Output is correct |
20 |
Correct |
32 ms |
87148 KB |
Output is correct |
21 |
Correct |
31 ms |
87184 KB |
Output is correct |
22 |
Correct |
33 ms |
87132 KB |
Output is correct |
23 |
Correct |
35 ms |
87092 KB |
Output is correct |
24 |
Correct |
38 ms |
87192 KB |
Output is correct |
25 |
Correct |
32 ms |
87124 KB |
Output is correct |
26 |
Correct |
32 ms |
87156 KB |
Output is correct |
27 |
Correct |
32 ms |
87072 KB |
Output is correct |
28 |
Correct |
32 ms |
87152 KB |
Output is correct |
29 |
Correct |
33 ms |
87080 KB |
Output is correct |
30 |
Correct |
33 ms |
87128 KB |
Output is correct |
31 |
Correct |
32 ms |
87252 KB |
Output is correct |
32 |
Correct |
32 ms |
87180 KB |
Output is correct |
33 |
Correct |
32 ms |
87168 KB |
Output is correct |
34 |
Correct |
33 ms |
87164 KB |
Output is correct |
35 |
Correct |
33 ms |
87180 KB |
Output is correct |
36 |
Correct |
33 ms |
87112 KB |
Output is correct |
37 |
Correct |
33 ms |
87192 KB |
Output is correct |
38 |
Correct |
34 ms |
87188 KB |
Output is correct |
39 |
Correct |
33 ms |
87084 KB |
Output is correct |
40 |
Correct |
34 ms |
87144 KB |
Output is correct |
41 |
Correct |
35 ms |
87180 KB |
Output is correct |
42 |
Correct |
35 ms |
87156 KB |
Output is correct |
43 |
Correct |
33 ms |
87124 KB |
Output is correct |
44 |
Correct |
35 ms |
87156 KB |
Output is correct |
45 |
Correct |
34 ms |
87208 KB |
Output is correct |
46 |
Correct |
34 ms |
87124 KB |
Output is correct |
47 |
Correct |
33 ms |
87088 KB |
Output is correct |
48 |
Correct |
38 ms |
87120 KB |
Output is correct |
49 |
Correct |
33 ms |
87124 KB |
Output is correct |
50 |
Correct |
34 ms |
87192 KB |
Output is correct |
51 |
Correct |
32 ms |
87124 KB |
Output is correct |
52 |
Correct |
32 ms |
87156 KB |
Output is correct |
53 |
Correct |
38 ms |
87088 KB |
Output is correct |
54 |
Correct |
33 ms |
87196 KB |
Output is correct |
55 |
Correct |
33 ms |
87084 KB |
Output is correct |
56 |
Correct |
33 ms |
87192 KB |
Output is correct |
57 |
Correct |
33 ms |
87124 KB |
Output is correct |
58 |
Correct |
39 ms |
87272 KB |
Output is correct |
59 |
Correct |
45 ms |
87812 KB |
Output is correct |
60 |
Correct |
49 ms |
87788 KB |
Output is correct |
61 |
Correct |
97 ms |
87840 KB |
Output is correct |
62 |
Correct |
331 ms |
88736 KB |
Output is correct |
63 |
Correct |
68 ms |
88656 KB |
Output is correct |
64 |
Correct |
186 ms |
88524 KB |
Output is correct |
65 |
Correct |
38 ms |
87084 KB |
Output is correct |
66 |
Correct |
32 ms |
87208 KB |
Output is correct |
67 |
Correct |
33 ms |
87192 KB |
Output is correct |
68 |
Correct |
33 ms |
87124 KB |
Output is correct |
69 |
Correct |
35 ms |
87180 KB |
Output is correct |
70 |
Correct |
34 ms |
87280 KB |
Output is correct |
71 |
Correct |
56 ms |
87268 KB |
Output is correct |
72 |
Correct |
37 ms |
87248 KB |
Output is correct |
73 |
Correct |
52 ms |
87264 KB |
Output is correct |
74 |
Correct |
60 ms |
87760 KB |
Output is correct |
75 |
Correct |
59 ms |
87716 KB |
Output is correct |
76 |
Correct |
142 ms |
87836 KB |
Output is correct |
77 |
Correct |
406 ms |
88636 KB |
Output is correct |
78 |
Correct |
82 ms |
88600 KB |
Output is correct |
79 |
Correct |
204 ms |
88640 KB |
Output is correct |
80 |
Correct |
76 ms |
88632 KB |
Output is correct |
81 |
Correct |
83 ms |
88536 KB |
Output is correct |
82 |
Correct |
284 ms |
88652 KB |
Output is correct |
83 |
Correct |
33 ms |
87116 KB |
Output is correct |
84 |
Correct |
32 ms |
87100 KB |
Output is correct |
85 |
Correct |
53 ms |
87388 KB |
Output is correct |
86 |
Correct |
37 ms |
87344 KB |
Output is correct |
87 |
Correct |
39 ms |
87380 KB |
Output is correct |
88 |
Correct |
46 ms |
87736 KB |
Output is correct |
89 |
Correct |
55 ms |
88048 KB |
Output is correct |
90 |
Correct |
98 ms |
88096 KB |
Output is correct |
91 |
Correct |
288 ms |
89232 KB |
Output is correct |
92 |
Correct |
66 ms |
89164 KB |
Output is correct |
93 |
Correct |
159 ms |
89328 KB |
Output is correct |
94 |
Correct |
36 ms |
87324 KB |
Output is correct |
95 |
Correct |
51 ms |
87336 KB |
Output is correct |
96 |
Correct |
33 ms |
87100 KB |
Output is correct |
97 |
Correct |
34 ms |
87116 KB |
Output is correct |
98 |
Correct |
37 ms |
87080 KB |
Output is correct |
99 |
Correct |
33 ms |
87108 KB |
Output is correct |