#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e5 + 10;
ll f[N][101], a[N];
ll sp[N][20], n, k;
int lg[N];
void build(int id) {
for (int i = 0; i <= n; i++) {
sp[i][0] = f[i][id];
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i + (1 << j) <= n + 1; i++) {
sp[i][j] = min(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
}
ll range(int l, int r, int id) {
if (l > r)
return 1e16;
int t = lg[r - l + 1];
return min(sp[l][t], sp[r - (1 << t) + 1][t]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (int i = 2; i < N; i++)
lg[i] = lg[i / 2] + 1;
mem(f, 0x3f);
cin >> n >> k;
f[0][0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int j = 1; j <= k; j++) {
build(j - 1);
stack <int> st;
vector <ll> pr;
for (int i = 0; i <= n; i++) {
while (sz(st) && a[i] >= a[st.top()]) {
st.pop();
pr.pop_back();
}
int lst = sz(st) ? st.top() : 0;
ll nxt = range(lst, i - 1, j - 1) + a[i];
if (sz(pr))
nxt = min(nxt, pr.back());
f[i][j] = nxt;
st.push(i);
ll v = sz(pr) ? pr.back() : 1e18;
pr.pb(min(v, range(lst, i - 1, j - 1) + a[i]));
}
}
cout << f[n][k] << '\n';
}
// 2 2
// 1 2
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
83804 KB |
Output is correct |
2 |
Correct |
11 ms |
83800 KB |
Output is correct |
3 |
Correct |
11 ms |
83804 KB |
Output is correct |
4 |
Correct |
12 ms |
83804 KB |
Output is correct |
5 |
Correct |
11 ms |
83804 KB |
Output is correct |
6 |
Correct |
11 ms |
83804 KB |
Output is correct |
7 |
Correct |
11 ms |
83936 KB |
Output is correct |
8 |
Correct |
11 ms |
83804 KB |
Output is correct |
9 |
Correct |
11 ms |
83896 KB |
Output is correct |
10 |
Correct |
12 ms |
83804 KB |
Output is correct |
11 |
Correct |
11 ms |
83920 KB |
Output is correct |
12 |
Correct |
11 ms |
83804 KB |
Output is correct |
13 |
Correct |
11 ms |
83804 KB |
Output is correct |
14 |
Correct |
12 ms |
83948 KB |
Output is correct |
15 |
Correct |
11 ms |
83804 KB |
Output is correct |
16 |
Correct |
12 ms |
83800 KB |
Output is correct |
17 |
Correct |
11 ms |
83804 KB |
Output is correct |
18 |
Correct |
11 ms |
83948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
83804 KB |
Output is correct |
2 |
Correct |
12 ms |
83804 KB |
Output is correct |
3 |
Correct |
11 ms |
83804 KB |
Output is correct |
4 |
Correct |
11 ms |
83964 KB |
Output is correct |
5 |
Correct |
11 ms |
83804 KB |
Output is correct |
6 |
Correct |
11 ms |
83804 KB |
Output is correct |
7 |
Correct |
12 ms |
83804 KB |
Output is correct |
8 |
Correct |
11 ms |
83804 KB |
Output is correct |
9 |
Correct |
11 ms |
83912 KB |
Output is correct |
10 |
Correct |
12 ms |
83804 KB |
Output is correct |
11 |
Correct |
11 ms |
84040 KB |
Output is correct |
12 |
Correct |
11 ms |
83892 KB |
Output is correct |
13 |
Correct |
11 ms |
83804 KB |
Output is correct |
14 |
Correct |
12 ms |
83844 KB |
Output is correct |
15 |
Correct |
11 ms |
84040 KB |
Output is correct |
16 |
Correct |
11 ms |
83804 KB |
Output is correct |
17 |
Correct |
11 ms |
83804 KB |
Output is correct |
18 |
Correct |
11 ms |
83804 KB |
Output is correct |
19 |
Correct |
12 ms |
83800 KB |
Output is correct |
20 |
Correct |
11 ms |
83804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
83804 KB |
Output is correct |
2 |
Correct |
11 ms |
83800 KB |
Output is correct |
3 |
Correct |
11 ms |
83804 KB |
Output is correct |
4 |
Correct |
12 ms |
83804 KB |
Output is correct |
5 |
Correct |
11 ms |
83804 KB |
Output is correct |
6 |
Correct |
11 ms |
83804 KB |
Output is correct |
7 |
Correct |
11 ms |
83936 KB |
Output is correct |
8 |
Correct |
11 ms |
83804 KB |
Output is correct |
9 |
Correct |
11 ms |
83896 KB |
Output is correct |
10 |
Correct |
12 ms |
83804 KB |
Output is correct |
11 |
Correct |
11 ms |
83920 KB |
Output is correct |
12 |
Correct |
11 ms |
83804 KB |
Output is correct |
13 |
Correct |
11 ms |
83804 KB |
Output is correct |
14 |
Correct |
12 ms |
83948 KB |
Output is correct |
15 |
Correct |
11 ms |
83804 KB |
Output is correct |
16 |
Correct |
12 ms |
83800 KB |
Output is correct |
17 |
Correct |
11 ms |
83804 KB |
Output is correct |
18 |
Correct |
11 ms |
83948 KB |
Output is correct |
19 |
Correct |
11 ms |
83804 KB |
Output is correct |
20 |
Correct |
12 ms |
83804 KB |
Output is correct |
21 |
Correct |
11 ms |
83804 KB |
Output is correct |
22 |
Correct |
11 ms |
83964 KB |
Output is correct |
23 |
Correct |
11 ms |
83804 KB |
Output is correct |
24 |
Correct |
11 ms |
83804 KB |
Output is correct |
25 |
Correct |
12 ms |
83804 KB |
Output is correct |
26 |
Correct |
11 ms |
83804 KB |
Output is correct |
27 |
Correct |
11 ms |
83912 KB |
Output is correct |
28 |
Correct |
12 ms |
83804 KB |
Output is correct |
29 |
Correct |
11 ms |
84040 KB |
Output is correct |
30 |
Correct |
11 ms |
83892 KB |
Output is correct |
31 |
Correct |
11 ms |
83804 KB |
Output is correct |
32 |
Correct |
12 ms |
83844 KB |
Output is correct |
33 |
Correct |
11 ms |
84040 KB |
Output is correct |
34 |
Correct |
11 ms |
83804 KB |
Output is correct |
35 |
Correct |
11 ms |
83804 KB |
Output is correct |
36 |
Correct |
11 ms |
83804 KB |
Output is correct |
37 |
Correct |
12 ms |
83800 KB |
Output is correct |
38 |
Correct |
11 ms |
83804 KB |
Output is correct |
39 |
Correct |
11 ms |
83800 KB |
Output is correct |
40 |
Correct |
11 ms |
83804 KB |
Output is correct |
41 |
Correct |
11 ms |
83916 KB |
Output is correct |
42 |
Correct |
11 ms |
83804 KB |
Output is correct |
43 |
Correct |
11 ms |
83804 KB |
Output is correct |
44 |
Correct |
11 ms |
83804 KB |
Output is correct |
45 |
Correct |
11 ms |
83804 KB |
Output is correct |
46 |
Correct |
11 ms |
84040 KB |
Output is correct |
47 |
Correct |
11 ms |
83804 KB |
Output is correct |
48 |
Correct |
11 ms |
83804 KB |
Output is correct |
49 |
Correct |
11 ms |
83804 KB |
Output is correct |
50 |
Correct |
11 ms |
84056 KB |
Output is correct |
51 |
Correct |
11 ms |
83804 KB |
Output is correct |
52 |
Correct |
11 ms |
83980 KB |
Output is correct |
53 |
Correct |
12 ms |
83804 KB |
Output is correct |
54 |
Correct |
11 ms |
83928 KB |
Output is correct |
55 |
Correct |
11 ms |
83800 KB |
Output is correct |
56 |
Correct |
12 ms |
83980 KB |
Output is correct |
57 |
Correct |
11 ms |
83936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
83804 KB |
Output is correct |
2 |
Correct |
11 ms |
83800 KB |
Output is correct |
3 |
Correct |
11 ms |
83804 KB |
Output is correct |
4 |
Correct |
12 ms |
83804 KB |
Output is correct |
5 |
Correct |
11 ms |
83804 KB |
Output is correct |
6 |
Correct |
11 ms |
83804 KB |
Output is correct |
7 |
Correct |
11 ms |
83936 KB |
Output is correct |
8 |
Correct |
11 ms |
83804 KB |
Output is correct |
9 |
Correct |
11 ms |
83896 KB |
Output is correct |
10 |
Correct |
12 ms |
83804 KB |
Output is correct |
11 |
Correct |
11 ms |
83920 KB |
Output is correct |
12 |
Correct |
11 ms |
83804 KB |
Output is correct |
13 |
Correct |
11 ms |
83804 KB |
Output is correct |
14 |
Correct |
12 ms |
83948 KB |
Output is correct |
15 |
Correct |
11 ms |
83804 KB |
Output is correct |
16 |
Correct |
12 ms |
83800 KB |
Output is correct |
17 |
Correct |
11 ms |
83804 KB |
Output is correct |
18 |
Correct |
11 ms |
83948 KB |
Output is correct |
19 |
Correct |
11 ms |
83804 KB |
Output is correct |
20 |
Correct |
12 ms |
83804 KB |
Output is correct |
21 |
Correct |
11 ms |
83804 KB |
Output is correct |
22 |
Correct |
11 ms |
83964 KB |
Output is correct |
23 |
Correct |
11 ms |
83804 KB |
Output is correct |
24 |
Correct |
11 ms |
83804 KB |
Output is correct |
25 |
Correct |
12 ms |
83804 KB |
Output is correct |
26 |
Correct |
11 ms |
83804 KB |
Output is correct |
27 |
Correct |
11 ms |
83912 KB |
Output is correct |
28 |
Correct |
12 ms |
83804 KB |
Output is correct |
29 |
Correct |
11 ms |
84040 KB |
Output is correct |
30 |
Correct |
11 ms |
83892 KB |
Output is correct |
31 |
Correct |
11 ms |
83804 KB |
Output is correct |
32 |
Correct |
12 ms |
83844 KB |
Output is correct |
33 |
Correct |
11 ms |
84040 KB |
Output is correct |
34 |
Correct |
11 ms |
83804 KB |
Output is correct |
35 |
Correct |
11 ms |
83804 KB |
Output is correct |
36 |
Correct |
11 ms |
83804 KB |
Output is correct |
37 |
Correct |
12 ms |
83800 KB |
Output is correct |
38 |
Correct |
11 ms |
83804 KB |
Output is correct |
39 |
Correct |
11 ms |
83800 KB |
Output is correct |
40 |
Correct |
11 ms |
83804 KB |
Output is correct |
41 |
Correct |
11 ms |
83916 KB |
Output is correct |
42 |
Correct |
11 ms |
83804 KB |
Output is correct |
43 |
Correct |
11 ms |
83804 KB |
Output is correct |
44 |
Correct |
11 ms |
83804 KB |
Output is correct |
45 |
Correct |
11 ms |
83804 KB |
Output is correct |
46 |
Correct |
11 ms |
84040 KB |
Output is correct |
47 |
Correct |
11 ms |
83804 KB |
Output is correct |
48 |
Correct |
11 ms |
83804 KB |
Output is correct |
49 |
Correct |
11 ms |
83804 KB |
Output is correct |
50 |
Correct |
11 ms |
84056 KB |
Output is correct |
51 |
Correct |
11 ms |
83804 KB |
Output is correct |
52 |
Correct |
11 ms |
83980 KB |
Output is correct |
53 |
Correct |
12 ms |
83804 KB |
Output is correct |
54 |
Correct |
11 ms |
83928 KB |
Output is correct |
55 |
Correct |
11 ms |
83800 KB |
Output is correct |
56 |
Correct |
12 ms |
83980 KB |
Output is correct |
57 |
Correct |
11 ms |
83936 KB |
Output is correct |
58 |
Correct |
23 ms |
84060 KB |
Output is correct |
59 |
Correct |
18 ms |
90456 KB |
Output is correct |
60 |
Correct |
31 ms |
90460 KB |
Output is correct |
61 |
Correct |
180 ms |
90480 KB |
Output is correct |
62 |
Correct |
994 ms |
97016 KB |
Output is correct |
63 |
Correct |
47 ms |
96848 KB |
Output is correct |
64 |
Correct |
461 ms |
97020 KB |
Output is correct |
65 |
Correct |
11 ms |
83804 KB |
Output is correct |
66 |
Correct |
12 ms |
83948 KB |
Output is correct |
67 |
Correct |
11 ms |
83804 KB |
Output is correct |
68 |
Correct |
12 ms |
84036 KB |
Output is correct |
69 |
Correct |
11 ms |
83800 KB |
Output is correct |
70 |
Correct |
11 ms |
83804 KB |
Output is correct |
71 |
Correct |
55 ms |
83912 KB |
Output is correct |
72 |
Correct |
13 ms |
83916 KB |
Output is correct |
73 |
Correct |
24 ms |
84056 KB |
Output is correct |
74 |
Correct |
18 ms |
90460 KB |
Output is correct |
75 |
Correct |
32 ms |
90456 KB |
Output is correct |
76 |
Correct |
184 ms |
90460 KB |
Output is correct |
77 |
Correct |
971 ms |
97008 KB |
Output is correct |
78 |
Correct |
48 ms |
96852 KB |
Output is correct |
79 |
Correct |
450 ms |
97008 KB |
Output is correct |
80 |
Correct |
29 ms |
96924 KB |
Output is correct |
81 |
Correct |
118 ms |
97012 KB |
Output is correct |
82 |
Correct |
943 ms |
97020 KB |
Output is correct |
83 |
Correct |
11 ms |
84060 KB |
Output is correct |
84 |
Correct |
12 ms |
83864 KB |
Output is correct |
85 |
Correct |
49 ms |
84116 KB |
Output is correct |
86 |
Correct |
13 ms |
84060 KB |
Output is correct |
87 |
Correct |
22 ms |
84060 KB |
Output is correct |
88 |
Correct |
17 ms |
90716 KB |
Output is correct |
89 |
Correct |
30 ms |
90760 KB |
Output is correct |
90 |
Correct |
190 ms |
90736 KB |
Output is correct |
91 |
Execution timed out |
1065 ms |
97996 KB |
Time limit exceeded |
92 |
Halted |
0 ms |
0 KB |
- |