This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
const int N = 100100, K = 101;
int dp[N][K], a[N], prevv[N], tree[4 * N];
void build_tree(int x, int v, int tl, int tr) {
if (tl == tr) {
tree[v] = dp[tl][x];
}
else {
int tm = (tl + tr) / 2;
build_tree(x, 2 * v, tl, tm);
build_tree(x, 2 * v + 1, tm + 1, tr);
tree[v] = min(tree[2 * v], tree[2 * v + 1]);
}
}
int Min(int v, int tl, int tr, int l, int r) {
if (tl >= l && tr <= r) return tree[v];
if (tl > r || tr < l) return 2e9;
int tm = (tl + tr) / 2;
return min(Min(2 * v, tl, tm, l, r), Min(2 * v + 1, tm + 1, tr, l, r));
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, k;
cin >> n >> k;
a[0] = 1e6 + 10;
for (int i = 1; i <= n; i++) {
cin >> a[i];
int j = i - 1;
while (a[j] < a[i]) {
j = prevv[j];
}
prevv[i] = j;
}
for (int i = 1; i <= n; i++) {
dp[i][1] = max(dp[i - 1][1], a[i]);
}
build_tree(1, 1, 1, n);
for (int j = 2; j <= k; j++) {
for (int i = j; i <= n; i++) {
dp[i][j] = Min(1, 1, n, max(prevv[i], j - 1), i - 1) + a[i];
if (prevv[i] >= j) {
dp[i][j] = min(dp[i][j], dp[prevv[i]][j]);
}
}
build_tree(j, 1, 1, n);
}
cout << dp[n][k] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |