#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const int N = 18;
int n, k, a[maxn], dp[maxn][105], spt[maxn][20];
int get(int l, int r) {
int j = __lg(r - l + 1);
return max(spt[l][j], spt[r - (1 << j) + 1][j]);
}
void Solve(int l, int r, int optL, int optR, int k) {
if (l > r) return;
int m = (l + r) / 2, opt = optL;
for (int i = optL; i <= min(m - 1, optR); i++) {
if (dp[i][k - 1] + get(i + 1, m) < dp[m][k]) {
dp[m][k] = dp[i][k - 1] + get(i + 1, m);
opt = i;
}
}
Solve(l, m - 1, optL, opt, k);
Solve(m + 1, r, opt, optR, k);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("BLOCKS.INP", "r", stdin);
//freopen("BLOCKS.OUT", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
spt[i][0] = a[i];
}
for (int j = 1; j <= N; j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
spt[i][j] = max(spt[i][j - 1], spt[i + (1 << (j - 1))][j - 1]);
memset(dp, 63, sizeof dp);
for (int i = 1; i <= n; i++)
dp[i][1] = get(1, i);
if (n <= 100) {
for (int t = 2; t <= k; t++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (dp[j][t - 1] + get(j + 1, i) < dp[i][t]) {
dp[i][t] = dp[j][t - 1] + get(j + 1, i);
}
}
}
}
}
else {
for (int t = 2; t <= k; t++)
Solve(1, n, 1, n, t);
}
cout << dp[n][k];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
82508 KB |
Output is correct |
2 |
Correct |
31 ms |
82516 KB |
Output is correct |
3 |
Correct |
30 ms |
82468 KB |
Output is correct |
4 |
Correct |
33 ms |
82404 KB |
Output is correct |
5 |
Correct |
32 ms |
82500 KB |
Output is correct |
6 |
Correct |
35 ms |
82520 KB |
Output is correct |
7 |
Correct |
33 ms |
82380 KB |
Output is correct |
8 |
Correct |
36 ms |
82432 KB |
Output is correct |
9 |
Correct |
31 ms |
82516 KB |
Output is correct |
10 |
Correct |
33 ms |
82396 KB |
Output is correct |
11 |
Correct |
35 ms |
82468 KB |
Output is correct |
12 |
Correct |
32 ms |
82508 KB |
Output is correct |
13 |
Correct |
34 ms |
82508 KB |
Output is correct |
14 |
Correct |
32 ms |
82544 KB |
Output is correct |
15 |
Correct |
31 ms |
82496 KB |
Output is correct |
16 |
Correct |
35 ms |
82436 KB |
Output is correct |
17 |
Correct |
39 ms |
82496 KB |
Output is correct |
18 |
Correct |
34 ms |
82468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
82516 KB |
Output is correct |
2 |
Correct |
33 ms |
82464 KB |
Output is correct |
3 |
Correct |
38 ms |
82484 KB |
Output is correct |
4 |
Correct |
32 ms |
82516 KB |
Output is correct |
5 |
Correct |
34 ms |
82504 KB |
Output is correct |
6 |
Correct |
42 ms |
82436 KB |
Output is correct |
7 |
Correct |
36 ms |
82432 KB |
Output is correct |
8 |
Correct |
32 ms |
82420 KB |
Output is correct |
9 |
Correct |
33 ms |
82484 KB |
Output is correct |
10 |
Correct |
33 ms |
82400 KB |
Output is correct |
11 |
Correct |
32 ms |
82460 KB |
Output is correct |
12 |
Correct |
32 ms |
82444 KB |
Output is correct |
13 |
Correct |
33 ms |
82476 KB |
Output is correct |
14 |
Correct |
35 ms |
82384 KB |
Output is correct |
15 |
Correct |
35 ms |
82508 KB |
Output is correct |
16 |
Correct |
37 ms |
82508 KB |
Output is correct |
17 |
Correct |
34 ms |
82516 KB |
Output is correct |
18 |
Correct |
36 ms |
82436 KB |
Output is correct |
19 |
Correct |
37 ms |
82464 KB |
Output is correct |
20 |
Correct |
37 ms |
82448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
82508 KB |
Output is correct |
2 |
Correct |
31 ms |
82516 KB |
Output is correct |
3 |
Correct |
30 ms |
82468 KB |
Output is correct |
4 |
Correct |
33 ms |
82404 KB |
Output is correct |
5 |
Correct |
32 ms |
82500 KB |
Output is correct |
6 |
Correct |
35 ms |
82520 KB |
Output is correct |
7 |
Correct |
33 ms |
82380 KB |
Output is correct |
8 |
Correct |
36 ms |
82432 KB |
Output is correct |
9 |
Correct |
31 ms |
82516 KB |
Output is correct |
10 |
Correct |
33 ms |
82396 KB |
Output is correct |
11 |
Correct |
35 ms |
82468 KB |
Output is correct |
12 |
Correct |
32 ms |
82508 KB |
Output is correct |
13 |
Correct |
34 ms |
82508 KB |
Output is correct |
14 |
Correct |
32 ms |
82544 KB |
Output is correct |
15 |
Correct |
31 ms |
82496 KB |
Output is correct |
16 |
Correct |
35 ms |
82436 KB |
Output is correct |
17 |
Correct |
39 ms |
82496 KB |
Output is correct |
18 |
Correct |
34 ms |
82468 KB |
Output is correct |
19 |
Correct |
31 ms |
82516 KB |
Output is correct |
20 |
Correct |
33 ms |
82464 KB |
Output is correct |
21 |
Correct |
38 ms |
82484 KB |
Output is correct |
22 |
Correct |
32 ms |
82516 KB |
Output is correct |
23 |
Correct |
34 ms |
82504 KB |
Output is correct |
24 |
Correct |
42 ms |
82436 KB |
Output is correct |
25 |
Correct |
36 ms |
82432 KB |
Output is correct |
26 |
Correct |
32 ms |
82420 KB |
Output is correct |
27 |
Correct |
33 ms |
82484 KB |
Output is correct |
28 |
Correct |
33 ms |
82400 KB |
Output is correct |
29 |
Correct |
32 ms |
82460 KB |
Output is correct |
30 |
Correct |
32 ms |
82444 KB |
Output is correct |
31 |
Correct |
33 ms |
82476 KB |
Output is correct |
32 |
Correct |
35 ms |
82384 KB |
Output is correct |
33 |
Correct |
35 ms |
82508 KB |
Output is correct |
34 |
Correct |
37 ms |
82508 KB |
Output is correct |
35 |
Correct |
34 ms |
82516 KB |
Output is correct |
36 |
Correct |
36 ms |
82436 KB |
Output is correct |
37 |
Correct |
37 ms |
82464 KB |
Output is correct |
38 |
Correct |
37 ms |
82448 KB |
Output is correct |
39 |
Correct |
40 ms |
82516 KB |
Output is correct |
40 |
Correct |
38 ms |
82392 KB |
Output is correct |
41 |
Correct |
36 ms |
82520 KB |
Output is correct |
42 |
Correct |
34 ms |
82452 KB |
Output is correct |
43 |
Correct |
33 ms |
82516 KB |
Output is correct |
44 |
Correct |
39 ms |
82408 KB |
Output is correct |
45 |
Correct |
33 ms |
82380 KB |
Output is correct |
46 |
Correct |
36 ms |
82380 KB |
Output is correct |
47 |
Correct |
33 ms |
82496 KB |
Output is correct |
48 |
Correct |
32 ms |
82496 KB |
Output is correct |
49 |
Correct |
33 ms |
82508 KB |
Output is correct |
50 |
Correct |
36 ms |
82508 KB |
Output is correct |
51 |
Correct |
38 ms |
82504 KB |
Output is correct |
52 |
Correct |
34 ms |
82420 KB |
Output is correct |
53 |
Correct |
42 ms |
82528 KB |
Output is correct |
54 |
Correct |
40 ms |
82440 KB |
Output is correct |
55 |
Correct |
33 ms |
82512 KB |
Output is correct |
56 |
Correct |
33 ms |
82492 KB |
Output is correct |
57 |
Correct |
44 ms |
82520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
82508 KB |
Output is correct |
2 |
Correct |
31 ms |
82516 KB |
Output is correct |
3 |
Correct |
30 ms |
82468 KB |
Output is correct |
4 |
Correct |
33 ms |
82404 KB |
Output is correct |
5 |
Correct |
32 ms |
82500 KB |
Output is correct |
6 |
Correct |
35 ms |
82520 KB |
Output is correct |
7 |
Correct |
33 ms |
82380 KB |
Output is correct |
8 |
Correct |
36 ms |
82432 KB |
Output is correct |
9 |
Correct |
31 ms |
82516 KB |
Output is correct |
10 |
Correct |
33 ms |
82396 KB |
Output is correct |
11 |
Correct |
35 ms |
82468 KB |
Output is correct |
12 |
Correct |
32 ms |
82508 KB |
Output is correct |
13 |
Correct |
34 ms |
82508 KB |
Output is correct |
14 |
Correct |
32 ms |
82544 KB |
Output is correct |
15 |
Correct |
31 ms |
82496 KB |
Output is correct |
16 |
Correct |
35 ms |
82436 KB |
Output is correct |
17 |
Correct |
39 ms |
82496 KB |
Output is correct |
18 |
Correct |
34 ms |
82468 KB |
Output is correct |
19 |
Correct |
31 ms |
82516 KB |
Output is correct |
20 |
Correct |
33 ms |
82464 KB |
Output is correct |
21 |
Correct |
38 ms |
82484 KB |
Output is correct |
22 |
Correct |
32 ms |
82516 KB |
Output is correct |
23 |
Correct |
34 ms |
82504 KB |
Output is correct |
24 |
Correct |
42 ms |
82436 KB |
Output is correct |
25 |
Correct |
36 ms |
82432 KB |
Output is correct |
26 |
Correct |
32 ms |
82420 KB |
Output is correct |
27 |
Correct |
33 ms |
82484 KB |
Output is correct |
28 |
Correct |
33 ms |
82400 KB |
Output is correct |
29 |
Correct |
32 ms |
82460 KB |
Output is correct |
30 |
Correct |
32 ms |
82444 KB |
Output is correct |
31 |
Correct |
33 ms |
82476 KB |
Output is correct |
32 |
Correct |
35 ms |
82384 KB |
Output is correct |
33 |
Correct |
35 ms |
82508 KB |
Output is correct |
34 |
Correct |
37 ms |
82508 KB |
Output is correct |
35 |
Correct |
34 ms |
82516 KB |
Output is correct |
36 |
Correct |
36 ms |
82436 KB |
Output is correct |
37 |
Correct |
37 ms |
82464 KB |
Output is correct |
38 |
Correct |
37 ms |
82448 KB |
Output is correct |
39 |
Correct |
40 ms |
82516 KB |
Output is correct |
40 |
Correct |
38 ms |
82392 KB |
Output is correct |
41 |
Correct |
36 ms |
82520 KB |
Output is correct |
42 |
Correct |
34 ms |
82452 KB |
Output is correct |
43 |
Correct |
33 ms |
82516 KB |
Output is correct |
44 |
Correct |
39 ms |
82408 KB |
Output is correct |
45 |
Correct |
33 ms |
82380 KB |
Output is correct |
46 |
Correct |
36 ms |
82380 KB |
Output is correct |
47 |
Correct |
33 ms |
82496 KB |
Output is correct |
48 |
Correct |
32 ms |
82496 KB |
Output is correct |
49 |
Correct |
33 ms |
82508 KB |
Output is correct |
50 |
Correct |
36 ms |
82508 KB |
Output is correct |
51 |
Correct |
38 ms |
82504 KB |
Output is correct |
52 |
Correct |
34 ms |
82420 KB |
Output is correct |
53 |
Correct |
42 ms |
82528 KB |
Output is correct |
54 |
Correct |
40 ms |
82440 KB |
Output is correct |
55 |
Correct |
33 ms |
82512 KB |
Output is correct |
56 |
Correct |
33 ms |
82492 KB |
Output is correct |
57 |
Correct |
44 ms |
82520 KB |
Output is correct |
58 |
Correct |
67 ms |
84076 KB |
Output is correct |
59 |
Correct |
47 ms |
89756 KB |
Output is correct |
60 |
Correct |
108 ms |
89824 KB |
Output is correct |
61 |
Incorrect |
956 ms |
89848 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |