#include <bits/stdc++.h>
using namespace std;
struct segtree {
vector<int> st;
int sz;
void init(int n) {
sz = n;
st.resize(sz << 1);
}
void upd(int u, int x) {
for (st[u += sz] = x; u >>= 1;) st[u] = max(st[u << 1], st[u << 1 | 1]);
}
int qry(int l, int r) {
int mx = 0;
for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) mx = max(mx, st[l++]);
if (r & 1) mx = max(mx, st[--r]);
}
return mx;
}
} st;
vector<vector<long long>> dp;
int n;
void dnc(int k, int l, int r, int optl, int optr) {
if (l > r) return;
int mid = l + r >> 1;
int opt = -1;
for (int j = max(0, optl - 1); j <= min(mid - 1, optr + 1); j++) {
int mx = st.qry(j + 1, mid);
if (dp[k][mid] > dp[k - 1][j] + mx)
dp[k][mid] = dp[k - 1][j] + mx, opt = j;
}
dnc(k, l, mid - 1, optl, opt == -1 ? mid - 2 : opt);
dnc(k, mid + 1, r, opt == -1 ? 0 : opt, optr);
}
int main() {
int k, x;
cin >> n >> k;
st.init(n);
dp = vector<vector<long long>> (k, vector<long long> (n, 1e18));
for (int i = 0; i < n; i++) cin >> x, st.upd(i, x);
for (int i = 0; i < n; i++) dp[0][i] = st.qry(0, i);
for (int i = 1; i < k; i++) dnc(i, 0, n - 1, 0, n - 2);
cout << dp[k - 1][n - 1] << '\n';
}
Compilation message
blocks.cpp: In function 'void dnc(int, int, int, int, int)':
blocks.cpp:29:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |