#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <map>
#include <set>
using namespace std;
vector<int64_t> p;
struct SegmentTree {
vector<int64_t> orig;
vector<int64_t> vec;
vector<int64_t> lazy;
const int64_t INF = 1e17;
void push (int dum, int tl, int tr) {
if (lazy[dum] == -1) {
return;
}
vec[dum] = lazy[dum] * (tr - tl + 1) + orig[dum];
if (tl != tr) {
lazy[2 * dum + 1] = lazy[dum];
lazy[2 * dum + 2] = lazy[dum];
}
lazy[dum] = -1;
}
void upd (int dum, int tl, int tr, int l, int r, int64_t x) {
push(dum, tl, tr);
if (tl >= l and tr <= r) {
lazy[dum] = x;
push(dum, tl, tr);
//cout << dum << " " << vec[dum] << " " << orig[dum] << " " << lazy[dum] << endl;
return;
}
if (tl > r or l > tr) {
return;
}
upd(2 * dum + 1, tl, (tl + tr)/2, l, r, x);
upd(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r, x);
vec[dum] = min(vec[2 * dum + 1], vec[2 * dum + 2]);
}
int64_t query (int dum, int tl, int tr, int l, int r) {
push(dum, tl, tr);
if (tl >= l and tr <= r) {
return vec[dum];
}
if (tl > r or l > tr) {
return INF;
}
return min(query(2 * dum + 1, tl, (tl + tr)/2, l, r), query(2 * dum + 2, (tl + tr)/2 + 1, tr, l, r));
}
void upd (int l, int r, int64_t x) {
upd(0, 0, (int)vec.size()/2 - 1, l, r, x);
}
int64_t query (int l, int r) {
return query(0, 0, (int)vec.size()/2 - 1, l, r);
}
void build (int dum, int tl, int tr) {
if (tl == tr) {
orig[dum] = p[tl];
return;
}
build(2 * dum + 1, tl, (tl + tr)/2);
build(2 * dum + 2, (tl + tr)/2 + 1, tr);
orig[dum] = min(orig[2 * dum + 1], orig[2 * dum + 2]);
}
SegmentTree () {
while (__builtin_popcount(p.size()) != 1) {
p.push_back(0);
}
int n = p.size();
orig.assign(2 * n, 0);
lazy.assign(2 * n, -1);
build(0, 0, n - 1);
vec = orig;
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
int64_t arr[N];
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int64_t INF = 1e17;
vector<int64_t> cur(N + 1);
p.assign(N + 1, INF), cur.assign(N + 1, INF);
for (int j = 1; j <= N; j++) {
p[j] = ((j == 1) ? arr[0] : max(p[j - 1], arr[j - 1]));
}
for (int i = 2; i <= K; i++) {
SegmentTree val;
for (int j = 1; j <= N; j++) {
if (j == 1) {
cur[j] = INF;
int64_t myMax = 0;
for (int l = j - 1; l >= 0; l--) {
myMax = max(myMax, arr[l]);
cur[j] = min(cur[j], p[l] + myMax);
val.upd(l, l, myMax);
//val[l] = p[l] + myMax;
}
} else {
cur[j] = INF;
int64_t myMax = 0;
for (int l = j - 1; l >= 0; l--) {
myMax = max(myMax, arr[l]);
if (myMax == arr[j - 1]) {
cur[j] = min(cur[j], p[l] + myMax);
val.upd(l, l, myMax);
} else {
assert(p[l] + myMax == val.query(l, l));
cur[j] = min(cur[j], val.query(l, l));
}
}
}
}
swap(cur, p);
}
cout << p[N];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Runtime error |
1 ms |
452 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |