#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx,avx2")
const int mx = 1e5 + 5;
int n, k, A[mx]; stack<int> stk;
struct Node{ int mnDP, vDP, vRange; };
template<int sz> struct ST{
Node seg[sz * 4]; int lz[sz * 4];
inline Node comb(Node a, Node b){
if (a.vDP + a.vRange > b.vDP + b.vRange) swap(a, b);
a.mnDP = min(a.mnDP, b.mnDP);
return a;
}
inline Node comb1(int i, int j){
Node a = seg[i], b = seg[j];
if (lz[i]) a.vDP = a.mnDP, a.vRange = lz[i];
if (lz[j]) b.vDP = b.mnDP, b.vRange = lz[j];
return comb(a, b);
}
inline void push(int l, int r, int i){
if (!lz[i]) return;
seg[i].vRange = lz[i];
if (l != r) lz[i * 2] = lz[i * 2 + 1] = lz[i];
lz[i] = 0;
}
inline void upd(int x, int y, int vDP, int vR, int l = 0, int r = sz, int i = 1){
push(l, r, i);
if (l > y or r < x) return;
if (l >= x and r <= y){
if (vDP) seg[i].mnDP = seg[i].vDP = vDP;
if (vR) lz[i] = vR, push(l, r, i);
if (l != r) seg[i] = comb1(i * 2, i * 2 + 1);
return;
}
int mid = (l + r) / 2;
upd(x, y, vDP, vR, l, mid, i * 2); upd(x, y, vDP, vR, mid + 1, r, i * 2 + 1);
seg[i] = comb1(i * 2, i * 2 + 1);
}
inline Node query(int x, int y, int l = 0, int r = sz, int i = 1){
push(l, r, i);
if (l > y or r < x) return {(int)1e9, (int)1e9, (int)1e9};
if (l != r) seg[i] = comb1(i * 2, i * 2 + 1);
if (l >= x and r <= y) return seg[i];
int mid = (l + r) / 2;
return comb(query(x, y, l, mid, i * 2), query(x, y, mid + 1, r, i * 2 + 1));
}
};ST<mx> dp[101];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int j = 1; j <= k; j++) dp[j].upd(0, 0, 1e9, 0);
for (int i = 1; i <= n; i++) dp[0].upd(i, i, 1e9, 0);
for (int i = 1; i <= n; i++){
cin >> A[i];
while (!stk.empty() and A[stk.top()] < A[i]) stk.pop();
int L = (stk.empty()) ? 0 : stk.top();
for (int j = 1; j <= k; j++){
dp[j - 1].upd(L, i - 1, 0, A[i]);
Node ret = dp[j - 1].query(0, i - 1);
int ans = ret.vDP + ret.vRange;
dp[j].upd(i, i, ans, 0);
if (i == n and j == k) cout<<ans<<endl;
}
stk.push(i);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1356 KB |
Output is correct |
20 |
Correct |
1 ms |
1612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
460 KB |
Output is correct |
23 |
Correct |
0 ms |
460 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
0 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
1 ms |
1100 KB |
Output is correct |
33 |
Correct |
1 ms |
972 KB |
Output is correct |
34 |
Correct |
1 ms |
1356 KB |
Output is correct |
35 |
Correct |
1 ms |
1228 KB |
Output is correct |
36 |
Correct |
1 ms |
972 KB |
Output is correct |
37 |
Correct |
1 ms |
1356 KB |
Output is correct |
38 |
Correct |
1 ms |
1612 KB |
Output is correct |
39 |
Correct |
5 ms |
4300 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
0 ms |
460 KB |
Output is correct |
44 |
Correct |
0 ms |
460 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
0 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
588 KB |
Output is correct |
50 |
Correct |
0 ms |
588 KB |
Output is correct |
51 |
Correct |
3 ms |
2508 KB |
Output is correct |
52 |
Correct |
3 ms |
2252 KB |
Output is correct |
53 |
Correct |
13 ms |
8268 KB |
Output is correct |
54 |
Correct |
11 ms |
8272 KB |
Output is correct |
55 |
Correct |
6 ms |
4812 KB |
Output is correct |
56 |
Correct |
11 ms |
9064 KB |
Output is correct |
57 |
Correct |
11 ms |
7116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
460 KB |
Output is correct |
23 |
Correct |
0 ms |
460 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
0 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
1 ms |
1100 KB |
Output is correct |
33 |
Correct |
1 ms |
972 KB |
Output is correct |
34 |
Correct |
1 ms |
1356 KB |
Output is correct |
35 |
Correct |
1 ms |
1228 KB |
Output is correct |
36 |
Correct |
1 ms |
972 KB |
Output is correct |
37 |
Correct |
1 ms |
1356 KB |
Output is correct |
38 |
Correct |
1 ms |
1612 KB |
Output is correct |
39 |
Correct |
5 ms |
4300 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
0 ms |
460 KB |
Output is correct |
44 |
Correct |
0 ms |
460 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
0 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
588 KB |
Output is correct |
50 |
Correct |
0 ms |
588 KB |
Output is correct |
51 |
Correct |
3 ms |
2508 KB |
Output is correct |
52 |
Correct |
3 ms |
2252 KB |
Output is correct |
53 |
Correct |
13 ms |
8268 KB |
Output is correct |
54 |
Correct |
11 ms |
8272 KB |
Output is correct |
55 |
Correct |
6 ms |
4812 KB |
Output is correct |
56 |
Correct |
11 ms |
9064 KB |
Output is correct |
57 |
Correct |
11 ms |
7116 KB |
Output is correct |
58 |
Correct |
157 ms |
13000 KB |
Output is correct |
59 |
Correct |
44 ms |
3740 KB |
Output is correct |
60 |
Correct |
223 ms |
15120 KB |
Output is correct |
61 |
Execution timed out |
1095 ms |
59308 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |