#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#include "generate.h"
#else
#define dbg(...) 42
#endif
vector<int> st, a;
void build(int id, int tl, int tr) {
if (tl == tr) {
st[id] = a[tl];
return;
}
int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
build(x, tl, tm);
build(y, tm + 1, tr);
st[id] = max(st[x], st[y]);
}
int query(int id, int tl, int tr, int l, int r) {
if (r < tl || tr < l) return 0;
if (l <= tl && tr <= r) return st[id];
int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
return max(query(x, tl, tm, l, r), query(y, tm + 1, tr, l, r));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
a.resize(n + 1, 0);
st.assign(4 * n + 10, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build(0, 0, n);
vector<vector<int64_t>> dp(n + 1, vector<int64_t>(k + 1, 1e18));
dp[1][1] = a[1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= min(i, k); j++) {
for (int r = j - 1; r <= i - 1; r++) {
dp[i][j] = min(dp[i][j], dp[r][j - 1] + query(0, 0, n, r + 1, i));
}
}
}
cout << dp[n][k] << '\n';
return 0;
}
# | 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... |