#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "blocks"
#define fi first
#define se second
#define pll pair<ll, ll>
using namespace std;
const ll N = 1e5 + 5;
const ll mod = 1e9 + 7;
ll n, sum= 0, m, tong, k, ans, T, t;
ll a[N], dp[N][102];
char c[N];
inline void sol()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)cin >> a[i];
for(int i = 0; i <= n; i ++)
{
for(int j = 0; j <= k; j ++)
dp[i][j] = mod;
}
dp[0][1] = 0;
for(int i = 1; i <= n; i ++)
{
dp[i][1] = max(dp[i-1][1], a[i]);
//cout << dp[i][1] <<" ";
}
for(int j = 2; j <= k; j ++)
{
stack<pll> ds;
for(int i = j; i <= n; i ++)
{
ll mn = dp[i-1][j-1];
while(!ds.empty() && a[ds.top().se] <= a[i])
{
mn = min(mn, ds.top().fi);
ds.pop();
}
dp[i][j] = min(dp[ds.empty() ? 0 : ds.top().se][j], mn + a[i]);
ds.push({mn, i});
//cout << dp[i][j] <<' ';
}
//cout << '\n';
}
cout << dp[n][k] << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
sol();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
416 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
8448 KB |
Output is correct |
2 |
Correct |
23 ms |
36480 KB |
Output is correct |
3 |
Correct |
29 ms |
36472 KB |
Output is correct |
4 |
Correct |
78 ms |
36480 KB |
Output is correct |
5 |
Correct |
255 ms |
81656 KB |
Output is correct |
6 |
Correct |
58 ms |
81668 KB |
Output is correct |
7 |
Correct |
157 ms |
81784 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
22 ms |
8448 KB |
Output is correct |
15 |
Correct |
7 ms |
8448 KB |
Output is correct |
16 |
Correct |
9 ms |
8448 KB |
Output is correct |
17 |
Correct |
26 ms |
36472 KB |
Output is correct |
18 |
Correct |
31 ms |
36480 KB |
Output is correct |
19 |
Correct |
85 ms |
36480 KB |
Output is correct |
20 |
Correct |
361 ms |
81784 KB |
Output is correct |
21 |
Correct |
53 ms |
81784 KB |
Output is correct |
22 |
Correct |
158 ms |
81656 KB |
Output is correct |
23 |
Correct |
58 ms |
81700 KB |
Output is correct |
24 |
Correct |
72 ms |
81660 KB |
Output is correct |
25 |
Correct |
287 ms |
81784 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
1 ms |
1168 KB |
Output is correct |
28 |
Correct |
21 ms |
8576 KB |
Output is correct |
29 |
Correct |
5 ms |
8576 KB |
Output is correct |
30 |
Correct |
10 ms |
8576 KB |
Output is correct |
31 |
Correct |
24 ms |
36472 KB |
Output is correct |
32 |
Correct |
28 ms |
36736 KB |
Output is correct |
33 |
Correct |
87 ms |
36800 KB |
Output is correct |
34 |
Correct |
341 ms |
82456 KB |
Output is correct |
35 |
Correct |
65 ms |
82228 KB |
Output is correct |
36 |
Correct |
166 ms |
82344 KB |
Output is correct |
37 |
Correct |
6 ms |
8448 KB |
Output is correct |
38 |
Correct |
20 ms |
8448 KB |
Output is correct |
39 |
Correct |
1 ms |
1152 KB |
Output is correct |
40 |
Correct |
2 ms |
1152 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
308 KB |
Output is correct |