#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int MAXN = 1e5+5;
ll pref[MAXN], suf[MAXN], in[MAXN];
ll dp[105][MAXN];
ll seg[MAXN * 4];
int n, blocks;
void build(int v, int l, int r)
{
if(l == r)
{
seg[v] = in[l];
return;
}
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m + 1, r);
seg[v] = max(seg[v * 2], seg[v * 2 + 1]);
}
ll get(int v, int tl, int tr, int l, int r)
{
if(l > r)
return 0;
if(l == tl && tr == r)
return seg[v];
int tm = (tl + tr) / 2;
return max(get(2 * v, tl, tm, l, min(tm, r)), get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
void compute(int l, int r, int optl, int optr, int block)
{
if(l > r)
return;
int mid = (l + r) / 2;
pair<ll, int> best = {1e18, -1};
for(int k = optl; k <= min(mid, optr); k++) {
best = min(best, {(k ? dp[block - 1][k - 1] : 0) + get(1, 0, n - 1, k, mid), k});
//cout << k << " " << mid << " " << get(1, 0, n-1, k, mid) << " " << best.first << "\n";
}
dp[block][mid] = best.first;
int opt = best.second;
//cout << block << " " << mid << " " << dp[block][mid] << "\n";
compute(l, mid - 1, optl, opt, block);
compute(mid + 1, r, opt, optr, block);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> blocks;
vector<int> max_idx;
pref[0] = suf[n+1] = 0;
for(int i = 0; i < n; i++) {
cin >> in[i];
}
build(1, 0, n - 1);
for(int i = 0; i < n; i++)
dp[1][i] = get(1, 0, n - 1, 0, i);
//cout << "hm\n";
for(int block = 2; block <= blocks; block++)
{
compute(block - 1, n - 1, block - 1, n - 1, block);
}
cout << dp[blocks][n - 1] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |