#include <iostream>
#include <algorithm>
#define ll long long
#define inf 1e9
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int N = 1e5 + 9;
int n, k, st[N*4];
ll dp[109][N], a[N], p[109][N];
void build(int id, int l, int r){
if (l==r) {
st[id] = a[l];
return;
}
int mid = l+r>>1;
build(id*2, l, mid); build(id*2+1, mid+1, r);
st[id] = max(st[id*2], st[id*2+1]);
}
ll get(int id, int l, int r, int u, int v){
if (v < l || u > r) return -1;
if (u <= l && r <= v) return st[id];
int mid = l+r>>1;
return max(get(id*2, l, mid, u, v), get(id*2+1, mid+1, r, u, v));
}
void solve(int k, int l, int r, int from, int to){
if (l > r) return;
int mid = l+r>>1;
dp[k][mid] = inf;
p[k][mid] = from;
for (int i = from; i <= min(mid-1, to); ++i){
ll new_cost = dp[k-1][i] + get(1, 1, n, i+1, mid);
if (new_cost < dp[k][mid]) {
dp[k][mid] = new_cost;
p[k][mid] = i;
}
}
solve(k, l, mid-1, from, p[k][mid]);
solve(k, mid+1, r, p[k][mid], to);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("nhap.inp", "r", stdin);
// freopen("xuat.out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
build(1, 1, n);
FOR(i, 1, k) FOR(j,1 ,n) dp[i][j] = inf;
FOR(i, 1, n) dp[1][i] = max(dp[1][i-1], a[i]);
FOR(t, 2, k) solve(t, 1, n, 1, n);
cout << dp[k][n];
return 0;
}
Compilation message
blocks.cpp: In function 'void build(int, int, int)':
blocks.cpp:19:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = l+r>>1;
| ~^~
blocks.cpp: In function 'long long int get(int, int, int, int, int)':
blocks.cpp:26:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l+r>>1;
| ~^~
blocks.cpp: In function 'void solve(int, int, int, int, int)':
blocks.cpp:32:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid = l+r>>1;
| ~^~
# |
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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
324 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 |
328 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 |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
324 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |