#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 1e5 + 5;
const int K = 105;
template <typename T>
struct SegmentTree {
vector <T> f;
T NEUTRAL = numeric_limits <T>::max();
int n;
SegmentTree(int _n) { init(_n); }
void init(int _n) {
n = _n;
f.resize((_n << 1) + 5, NEUTRAL);
}
T merge(T x, T y) { return min(x, y); }
void upd(int x, T val) {
x += n;
f[x] = merge(f[x], val);
for (; x > 0; x >>= 1)
f[x >> 1] = merge(f[x], f[x ^ 1]);
}
T qry(int l, int r) {
T ql = NEUTRAL;
T qr = NEUTRAL;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) ql = merge(ql, f[l++]);
if (r & 1) qr = merge(f[--r], qr);
}
return merge(ql, qr);
}
};
int n, k;
int a[N], lhs[N];
ll dp[K][N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
// dp(k, i) = min(dp(k - 1, j) + max(j..i));
// j = lhs[i] + 1; -> dp(k, i) = a[i] + min(dp(k - 1, j)) (j : lhs[i] + 1 -> i);
memset(dp, 0x3f, sizeof dp);
dp[1][0] = 0;
for (int i = 1; i <= n; ++i)
dp[1][i] = max(dp[1][i - 1], 1LL * a[i]);
for (int _k = 2; _k <= k; ++_k) {
stack <pair <ll, int>> S;
for (int i = _k; i <= n; ++i) {
ll p = dp[_k - 1][i - 1];
while (!S.empty() && a[i] >= a[S.top().second]) {
p = min(p, S.top().first);
S.pop();
}
int j = S.empty()? 0 : S.top().second;
dp[_k][i] = min(dp[_k][j], p + a[i]);
S.push({p, i});
}
}
cout << dp[k][n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
82524 KB |
Output is correct |
2 |
Correct |
14 ms |
82692 KB |
Output is correct |
3 |
Correct |
14 ms |
82516 KB |
Output is correct |
4 |
Correct |
13 ms |
82520 KB |
Output is correct |
5 |
Correct |
14 ms |
82524 KB |
Output is correct |
6 |
Correct |
14 ms |
82524 KB |
Output is correct |
7 |
Correct |
14 ms |
82520 KB |
Output is correct |
8 |
Correct |
14 ms |
82524 KB |
Output is correct |
9 |
Correct |
13 ms |
82680 KB |
Output is correct |
10 |
Correct |
13 ms |
82524 KB |
Output is correct |
11 |
Correct |
14 ms |
82712 KB |
Output is correct |
12 |
Correct |
14 ms |
82680 KB |
Output is correct |
13 |
Correct |
14 ms |
82524 KB |
Output is correct |
14 |
Correct |
14 ms |
82664 KB |
Output is correct |
15 |
Correct |
13 ms |
82524 KB |
Output is correct |
16 |
Correct |
14 ms |
82524 KB |
Output is correct |
17 |
Correct |
13 ms |
82644 KB |
Output is correct |
18 |
Correct |
13 ms |
82520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
82524 KB |
Output is correct |
2 |
Correct |
14 ms |
82524 KB |
Output is correct |
3 |
Correct |
14 ms |
82520 KB |
Output is correct |
4 |
Correct |
14 ms |
82776 KB |
Output is correct |
5 |
Correct |
13 ms |
82644 KB |
Output is correct |
6 |
Correct |
14 ms |
82524 KB |
Output is correct |
7 |
Correct |
14 ms |
82524 KB |
Output is correct |
8 |
Correct |
15 ms |
82524 KB |
Output is correct |
9 |
Correct |
13 ms |
82628 KB |
Output is correct |
10 |
Correct |
14 ms |
82616 KB |
Output is correct |
11 |
Correct |
13 ms |
82524 KB |
Output is correct |
12 |
Correct |
14 ms |
82524 KB |
Output is correct |
13 |
Correct |
14 ms |
82524 KB |
Output is correct |
14 |
Correct |
14 ms |
82524 KB |
Output is correct |
15 |
Correct |
14 ms |
82524 KB |
Output is correct |
16 |
Correct |
14 ms |
82620 KB |
Output is correct |
17 |
Correct |
14 ms |
82456 KB |
Output is correct |
18 |
Correct |
14 ms |
82772 KB |
Output is correct |
19 |
Correct |
14 ms |
82524 KB |
Output is correct |
20 |
Correct |
14 ms |
82524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
82524 KB |
Output is correct |
2 |
Correct |
14 ms |
82692 KB |
Output is correct |
3 |
Correct |
14 ms |
82516 KB |
Output is correct |
4 |
Correct |
13 ms |
82520 KB |
Output is correct |
5 |
Correct |
14 ms |
82524 KB |
Output is correct |
6 |
Correct |
14 ms |
82524 KB |
Output is correct |
7 |
Correct |
14 ms |
82520 KB |
Output is correct |
8 |
Correct |
14 ms |
82524 KB |
Output is correct |
9 |
Correct |
13 ms |
82680 KB |
Output is correct |
10 |
Correct |
13 ms |
82524 KB |
Output is correct |
11 |
Correct |
14 ms |
82712 KB |
Output is correct |
12 |
Correct |
14 ms |
82680 KB |
Output is correct |
13 |
Correct |
14 ms |
82524 KB |
Output is correct |
14 |
Correct |
14 ms |
82664 KB |
Output is correct |
15 |
Correct |
13 ms |
82524 KB |
Output is correct |
16 |
Correct |
14 ms |
82524 KB |
Output is correct |
17 |
Correct |
13 ms |
82644 KB |
Output is correct |
18 |
Correct |
13 ms |
82520 KB |
Output is correct |
19 |
Correct |
14 ms |
82524 KB |
Output is correct |
20 |
Correct |
14 ms |
82524 KB |
Output is correct |
21 |
Correct |
14 ms |
82520 KB |
Output is correct |
22 |
Correct |
14 ms |
82776 KB |
Output is correct |
23 |
Correct |
13 ms |
82644 KB |
Output is correct |
24 |
Correct |
14 ms |
82524 KB |
Output is correct |
25 |
Correct |
14 ms |
82524 KB |
Output is correct |
26 |
Correct |
15 ms |
82524 KB |
Output is correct |
27 |
Correct |
13 ms |
82628 KB |
Output is correct |
28 |
Correct |
14 ms |
82616 KB |
Output is correct |
29 |
Correct |
13 ms |
82524 KB |
Output is correct |
30 |
Correct |
14 ms |
82524 KB |
Output is correct |
31 |
Correct |
14 ms |
82524 KB |
Output is correct |
32 |
Correct |
14 ms |
82524 KB |
Output is correct |
33 |
Correct |
14 ms |
82524 KB |
Output is correct |
34 |
Correct |
14 ms |
82620 KB |
Output is correct |
35 |
Correct |
14 ms |
82456 KB |
Output is correct |
36 |
Correct |
14 ms |
82772 KB |
Output is correct |
37 |
Correct |
14 ms |
82524 KB |
Output is correct |
38 |
Correct |
14 ms |
82524 KB |
Output is correct |
39 |
Correct |
14 ms |
82644 KB |
Output is correct |
40 |
Correct |
13 ms |
82712 KB |
Output is correct |
41 |
Correct |
13 ms |
82520 KB |
Output is correct |
42 |
Correct |
13 ms |
82524 KB |
Output is correct |
43 |
Correct |
14 ms |
82712 KB |
Output is correct |
44 |
Correct |
13 ms |
82524 KB |
Output is correct |
45 |
Correct |
13 ms |
82520 KB |
Output is correct |
46 |
Correct |
14 ms |
82524 KB |
Output is correct |
47 |
Correct |
13 ms |
82448 KB |
Output is correct |
48 |
Correct |
15 ms |
82640 KB |
Output is correct |
49 |
Correct |
14 ms |
82776 KB |
Output is correct |
50 |
Correct |
14 ms |
82520 KB |
Output is correct |
51 |
Correct |
13 ms |
82520 KB |
Output is correct |
52 |
Correct |
13 ms |
82644 KB |
Output is correct |
53 |
Correct |
14 ms |
82524 KB |
Output is correct |
54 |
Correct |
13 ms |
82576 KB |
Output is correct |
55 |
Correct |
14 ms |
82524 KB |
Output is correct |
56 |
Correct |
13 ms |
82524 KB |
Output is correct |
57 |
Correct |
14 ms |
82696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
82524 KB |
Output is correct |
2 |
Correct |
14 ms |
82692 KB |
Output is correct |
3 |
Correct |
14 ms |
82516 KB |
Output is correct |
4 |
Correct |
13 ms |
82520 KB |
Output is correct |
5 |
Correct |
14 ms |
82524 KB |
Output is correct |
6 |
Correct |
14 ms |
82524 KB |
Output is correct |
7 |
Correct |
14 ms |
82520 KB |
Output is correct |
8 |
Correct |
14 ms |
82524 KB |
Output is correct |
9 |
Correct |
13 ms |
82680 KB |
Output is correct |
10 |
Correct |
13 ms |
82524 KB |
Output is correct |
11 |
Correct |
14 ms |
82712 KB |
Output is correct |
12 |
Correct |
14 ms |
82680 KB |
Output is correct |
13 |
Correct |
14 ms |
82524 KB |
Output is correct |
14 |
Correct |
14 ms |
82664 KB |
Output is correct |
15 |
Correct |
13 ms |
82524 KB |
Output is correct |
16 |
Correct |
14 ms |
82524 KB |
Output is correct |
17 |
Correct |
13 ms |
82644 KB |
Output is correct |
18 |
Correct |
13 ms |
82520 KB |
Output is correct |
19 |
Correct |
14 ms |
82524 KB |
Output is correct |
20 |
Correct |
14 ms |
82524 KB |
Output is correct |
21 |
Correct |
14 ms |
82520 KB |
Output is correct |
22 |
Correct |
14 ms |
82776 KB |
Output is correct |
23 |
Correct |
13 ms |
82644 KB |
Output is correct |
24 |
Correct |
14 ms |
82524 KB |
Output is correct |
25 |
Correct |
14 ms |
82524 KB |
Output is correct |
26 |
Correct |
15 ms |
82524 KB |
Output is correct |
27 |
Correct |
13 ms |
82628 KB |
Output is correct |
28 |
Correct |
14 ms |
82616 KB |
Output is correct |
29 |
Correct |
13 ms |
82524 KB |
Output is correct |
30 |
Correct |
14 ms |
82524 KB |
Output is correct |
31 |
Correct |
14 ms |
82524 KB |
Output is correct |
32 |
Correct |
14 ms |
82524 KB |
Output is correct |
33 |
Correct |
14 ms |
82524 KB |
Output is correct |
34 |
Correct |
14 ms |
82620 KB |
Output is correct |
35 |
Correct |
14 ms |
82456 KB |
Output is correct |
36 |
Correct |
14 ms |
82772 KB |
Output is correct |
37 |
Correct |
14 ms |
82524 KB |
Output is correct |
38 |
Correct |
14 ms |
82524 KB |
Output is correct |
39 |
Correct |
14 ms |
82644 KB |
Output is correct |
40 |
Correct |
13 ms |
82712 KB |
Output is correct |
41 |
Correct |
13 ms |
82520 KB |
Output is correct |
42 |
Correct |
13 ms |
82524 KB |
Output is correct |
43 |
Correct |
14 ms |
82712 KB |
Output is correct |
44 |
Correct |
13 ms |
82524 KB |
Output is correct |
45 |
Correct |
13 ms |
82520 KB |
Output is correct |
46 |
Correct |
14 ms |
82524 KB |
Output is correct |
47 |
Correct |
13 ms |
82448 KB |
Output is correct |
48 |
Correct |
15 ms |
82640 KB |
Output is correct |
49 |
Correct |
14 ms |
82776 KB |
Output is correct |
50 |
Correct |
14 ms |
82520 KB |
Output is correct |
51 |
Correct |
13 ms |
82520 KB |
Output is correct |
52 |
Correct |
13 ms |
82644 KB |
Output is correct |
53 |
Correct |
14 ms |
82524 KB |
Output is correct |
54 |
Correct |
13 ms |
82576 KB |
Output is correct |
55 |
Correct |
14 ms |
82524 KB |
Output is correct |
56 |
Correct |
13 ms |
82524 KB |
Output is correct |
57 |
Correct |
14 ms |
82696 KB |
Output is correct |
58 |
Correct |
16 ms |
82780 KB |
Output is correct |
59 |
Correct |
16 ms |
83044 KB |
Output is correct |
60 |
Correct |
19 ms |
83160 KB |
Output is correct |
61 |
Correct |
44 ms |
83032 KB |
Output is correct |
62 |
Correct |
102 ms |
83632 KB |
Output is correct |
63 |
Correct |
23 ms |
83536 KB |
Output is correct |
64 |
Correct |
57 ms |
83548 KB |
Output is correct |
65 |
Correct |
14 ms |
82524 KB |
Output is correct |
66 |
Correct |
13 ms |
82724 KB |
Output is correct |
67 |
Correct |
14 ms |
82520 KB |
Output is correct |
68 |
Correct |
13 ms |
82524 KB |
Output is correct |
69 |
Correct |
14 ms |
82524 KB |
Output is correct |
70 |
Correct |
14 ms |
82524 KB |
Output is correct |
71 |
Correct |
24 ms |
82760 KB |
Output is correct |
72 |
Correct |
14 ms |
82724 KB |
Output is correct |
73 |
Correct |
16 ms |
82736 KB |
Output is correct |
74 |
Correct |
17 ms |
83036 KB |
Output is correct |
75 |
Correct |
19 ms |
83068 KB |
Output is correct |
76 |
Correct |
57 ms |
83176 KB |
Output is correct |
77 |
Correct |
129 ms |
83768 KB |
Output is correct |
78 |
Correct |
23 ms |
83536 KB |
Output is correct |
79 |
Correct |
70 ms |
83780 KB |
Output is correct |
80 |
Correct |
21 ms |
83800 KB |
Output is correct |
81 |
Correct |
30 ms |
83540 KB |
Output is correct |
82 |
Correct |
130 ms |
83548 KB |
Output is correct |
83 |
Correct |
13 ms |
82520 KB |
Output is correct |
84 |
Correct |
13 ms |
82524 KB |
Output is correct |
85 |
Correct |
26 ms |
82896 KB |
Output is correct |
86 |
Correct |
15 ms |
82804 KB |
Output is correct |
87 |
Correct |
16 ms |
82780 KB |
Output is correct |
88 |
Correct |
18 ms |
83096 KB |
Output is correct |
89 |
Correct |
20 ms |
83364 KB |
Output is correct |
90 |
Correct |
52 ms |
83464 KB |
Output is correct |
91 |
Correct |
129 ms |
84504 KB |
Output is correct |
92 |
Correct |
22 ms |
84308 KB |
Output is correct |
93 |
Correct |
70 ms |
84428 KB |
Output is correct |
94 |
Correct |
15 ms |
82780 KB |
Output is correct |
95 |
Correct |
24 ms |
83036 KB |
Output is correct |
96 |
Correct |
14 ms |
82524 KB |
Output is correct |
97 |
Correct |
14 ms |
82644 KB |
Output is correct |
98 |
Correct |
13 ms |
82524 KB |
Output is correct |
99 |
Correct |
14 ms |
82524 KB |
Output is correct |