#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;
template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
struct Tree {
using Node = ll;
Node pull(const Node &a, const Node &b) {
return min(a, b);
}
int n;
vector<Node> t;
Tree(int _n) : n(_n) {
t.resize(4 * n, infll);
}
Node get(int v, int l, int r, int lq, int rq) {
if (rq <= l || r <= lq) {
return infll;
}
if (lq <= l && r <= rq) {
return t[v];
}
int mid = (l + r) / 2;
return pull(get(2 * v + 1, l, mid, lq, rq), get(2 * v + 2, mid, r, lq, rq));
}
ll get(int l, int r) {
return get(0, 0, n, l, r);
}
ll get(int i) {
return get(i, i + 1);
}
void upd(int v, int l, int r, int i, ll x) {
if (l + 1 == r) {
t[v] = x;
return;
}
int mid = (l + r) / 2;
if (i < mid) {
upd(2 * v + 1, l, mid, i, x);
} else {
upd(2 * v + 2, mid, r, i, x);
}
t[v] = pull(t[2 * v + 1], t[2 * v + 2]);
}
void upd(int i, ll x) {
upd(0, 0, n, i, x);
}
};
int32_t main() {
#ifdef LOCAL
freopen("/tmp/input.txt", "r", stdin);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n, k;
cin >> n >> k;
vector<int> a(n);
cin >> a;
vector<int> l(n);
for (int i = 0; i < n; ++i) {
l[i] = i - 1;
while (l[i] >= 0 && a[l[i]] < a[i]) {
l[i] = l[l[i]];
}
}
for (auto &i : l) {
++i;
}
Tree dp(n + 1);
dp.upd(0, 0);
for (int j = 1; j <= k; ++j) {
Tree dpp(n + 1);
for (int i = 1; i <= n; ++i) {
if (l[i - 1] == 0) {
dpp.upd(i, dp.get(0, i) + a[i - 1]);
} else {
dpp.upd(i, min(dpp.get(l[i - 1], i), dp.get(l[i - 1], i) + a[i - 1]));
}
}
dp = dpp;
}
cout << dp.get(n) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |