#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
const int N = 1e5, K = 100;
int a[1 + N];
int dp[1 + K][1 + N];
int mn[1 + N];
int stk[1 + N];
int main() {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[1][i] = max (dp[1][i - 1], a[i]);
}
dp[1][0] = (1 << 30);
for (int nrBlocks = 2; nrBlocks <= k; nrBlocks++) {
int vf = 0;
dp[nrBlocks][0] = (1 << 30);
for (int i = 1; i <= n; i++) {
int best = dp[nrBlocks - 1][i - 1];
while (vf && a[stk[vf]] <= a[i]) {
best = min (best, mn[stk[vf]]);
vf--;
}
mn[i] = best;
dp[nrBlocks][i] = a[i] + best;
if (vf)
dp[nrBlocks][i] = min (dp[nrBlocks][i], dp[nrBlocks][stk[vf]]);
stk[++vf] = i;
}
}
cout << dp[k][n] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 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 |
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 |
640 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
416 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
0 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
896 KB |
Output is correct |
17 |
Correct |
1 ms |
616 KB |
Output is correct |
18 |
Correct |
1 ms |
896 KB |
Output is correct |
19 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1536 KB |
Output is correct |
2 |
Correct |
6 ms |
1024 KB |
Output is correct |
3 |
Correct |
9 ms |
2304 KB |
Output is correct |
4 |
Correct |
43 ms |
14584 KB |
Output is correct |
5 |
Correct |
129 ms |
40952 KB |
Output is correct |
6 |
Correct |
16 ms |
2944 KB |
Output is correct |
7 |
Correct |
63 ms |
19448 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
16 ms |
4864 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
1536 KB |
Output is correct |
17 |
Correct |
6 ms |
1024 KB |
Output is correct |
18 |
Correct |
12 ms |
2220 KB |
Output is correct |
19 |
Correct |
57 ms |
14524 KB |
Output is correct |
20 |
Correct |
166 ms |
40936 KB |
Output is correct |
21 |
Correct |
16 ms |
3072 KB |
Output is correct |
22 |
Correct |
83 ms |
19448 KB |
Output is correct |
23 |
Correct |
14 ms |
1784 KB |
Output is correct |
24 |
Correct |
27 ms |
5752 KB |
Output is correct |
25 |
Correct |
166 ms |
40952 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
13 ms |
4864 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
1664 KB |
Output is correct |
31 |
Correct |
6 ms |
1024 KB |
Output is correct |
32 |
Correct |
9 ms |
2304 KB |
Output is correct |
33 |
Correct |
46 ms |
14584 KB |
Output is correct |
34 |
Correct |
133 ms |
41080 KB |
Output is correct |
35 |
Correct |
16 ms |
3200 KB |
Output is correct |
36 |
Correct |
69 ms |
19576 KB |
Output is correct |
37 |
Correct |
2 ms |
640 KB |
Output is correct |
38 |
Correct |
12 ms |
4864 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |