#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f3f3f3f3f;
vector<int>dp_before(N),dp_cur(N);
int a[N],st[4 * N],n,k;
void build(int id,int l,int r) {
if (l == r) {
st[id] = a[l];
return;
}
int mid = (l + r) / 2;
build(id * 2,l,mid);
build(id * 2 + 1,mid + 1,r);
st[id] = max(st[id * 2],st[id * 2 + 1]);
}
int get(int id,int l,int r,int u,int v) {
if (v < l || r < u) return -inf;
if (u <= l && r <= v) return st[id];
int mid = (l + r) / 2;
return max(get(id * 2,l,mid,u,v),get(id * 2 + 1,mid + 1,r,u,v));
}
void compute(int l,int r,int optl,int optr) {
if (l > r) return;
int mid = (l + r) / 2;
int best = inf,opt = -1;
for (int i = optl; i <= min(optr,mid); i++) {
int tmp = dp_before[i - 1] + get(1,1,n,i,mid);
if (tmp < best) best = tmp,opt = i;
}
dp_cur[mid] = best;
compute(l,mid - 1,optl,opt);
compute(mid + 1,r,opt,optr);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
build(1,1,n);
dp_before[0] = inf;
for (int i = 1; i <= n; i++) dp_before[i] = get(1,1,n,1,i);
for (int i = 2; i <= k; i++) {
compute(1,n,1,n);
dp_before = dp_cur;
dp_before[0] = inf;
}
cout << dp_before[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
2036 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
2036 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
2036 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |