#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> p;
const int N = 100005;
const int INF = 1e9;
int n, k, f[2][N], a[N];
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) f[0][i] = INF;
for (int i = 1; i <= k; ++i) {
bool t = i & 1;
stack<p> st[2];
f[t][0] = INF;
for (int j = 1; j <= n; ++j) {
int pos = j;
int val = f[t ^ 1][j - 1];
while (st[0].size() && a[st[0].top().fi] <= a[j]) {
pos = st[0].top().se.fi;
val = min(val, st[0].top().se.se);
st[0].pop();
}
st[0].push(p(j, ii(pos, val)));
while (st[1].size() && st[1].top().se.fi >= pos) st[1].pop();
if (!(st[1].size() && st[1].top().se.se <= val + a[j])) {
st[1].push(p(j, ii(pos, val + a[j])));
}
f[t][j] = st[1].top().se.se;
//cout << pos << ' ' << val << ' ' << f[t][j] << ' ' << j << '\n';
}
}
cout << f[k & 1][n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3348 KB |
Output is correct |
2 |
Correct |
0 ms |
3348 KB |
Output is correct |
3 |
Correct |
0 ms |
3348 KB |
Output is correct |
4 |
Correct |
0 ms |
3348 KB |
Output is correct |
5 |
Correct |
0 ms |
3348 KB |
Output is correct |
6 |
Correct |
0 ms |
3348 KB |
Output is correct |
7 |
Correct |
0 ms |
3348 KB |
Output is correct |
8 |
Correct |
0 ms |
3348 KB |
Output is correct |
9 |
Correct |
0 ms |
3348 KB |
Output is correct |
10 |
Correct |
0 ms |
3348 KB |
Output is correct |
11 |
Correct |
0 ms |
3348 KB |
Output is correct |
12 |
Correct |
0 ms |
3348 KB |
Output is correct |
13 |
Correct |
0 ms |
3348 KB |
Output is correct |
14 |
Correct |
0 ms |
3348 KB |
Output is correct |
15 |
Correct |
0 ms |
3348 KB |
Output is correct |
16 |
Correct |
0 ms |
3348 KB |
Output is correct |
17 |
Correct |
0 ms |
3348 KB |
Output is correct |
18 |
Correct |
0 ms |
3348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3348 KB |
Output is correct |
2 |
Correct |
0 ms |
3348 KB |
Output is correct |
3 |
Correct |
0 ms |
3348 KB |
Output is correct |
4 |
Correct |
0 ms |
3348 KB |
Output is correct |
5 |
Correct |
0 ms |
3348 KB |
Output is correct |
6 |
Correct |
0 ms |
3348 KB |
Output is correct |
7 |
Correct |
0 ms |
3348 KB |
Output is correct |
8 |
Correct |
0 ms |
3348 KB |
Output is correct |
9 |
Correct |
0 ms |
3348 KB |
Output is correct |
10 |
Correct |
0 ms |
3348 KB |
Output is correct |
11 |
Correct |
0 ms |
3348 KB |
Output is correct |
12 |
Correct |
0 ms |
3348 KB |
Output is correct |
13 |
Correct |
0 ms |
3348 KB |
Output is correct |
14 |
Correct |
0 ms |
3348 KB |
Output is correct |
15 |
Correct |
0 ms |
3348 KB |
Output is correct |
16 |
Correct |
0 ms |
3348 KB |
Output is correct |
17 |
Correct |
0 ms |
3348 KB |
Output is correct |
18 |
Correct |
0 ms |
3348 KB |
Output is correct |
19 |
Correct |
0 ms |
3348 KB |
Output is correct |
20 |
Correct |
0 ms |
3348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3348 KB |
Output is correct |
2 |
Correct |
0 ms |
3348 KB |
Output is correct |
3 |
Correct |
0 ms |
3348 KB |
Output is correct |
4 |
Correct |
0 ms |
3348 KB |
Output is correct |
5 |
Correct |
0 ms |
3348 KB |
Output is correct |
6 |
Correct |
0 ms |
3348 KB |
Output is correct |
7 |
Correct |
0 ms |
3348 KB |
Output is correct |
8 |
Correct |
0 ms |
3348 KB |
Output is correct |
9 |
Correct |
0 ms |
3348 KB |
Output is correct |
10 |
Correct |
0 ms |
3348 KB |
Output is correct |
11 |
Correct |
0 ms |
3348 KB |
Output is correct |
12 |
Correct |
0 ms |
3348 KB |
Output is correct |
13 |
Correct |
0 ms |
3348 KB |
Output is correct |
14 |
Correct |
0 ms |
3348 KB |
Output is correct |
15 |
Correct |
0 ms |
3348 KB |
Output is correct |
16 |
Correct |
0 ms |
3348 KB |
Output is correct |
17 |
Correct |
0 ms |
3348 KB |
Output is correct |
18 |
Correct |
0 ms |
3348 KB |
Output is correct |
19 |
Correct |
0 ms |
3348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3348 KB |
Output is correct |
2 |
Correct |
6 ms |
3348 KB |
Output is correct |
3 |
Correct |
13 ms |
3348 KB |
Output is correct |
4 |
Correct |
89 ms |
3348 KB |
Output is correct |
5 |
Correct |
263 ms |
3348 KB |
Output is correct |
6 |
Correct |
16 ms |
3348 KB |
Output is correct |
7 |
Correct |
123 ms |
3348 KB |
Output is correct |
8 |
Correct |
0 ms |
3348 KB |
Output is correct |
9 |
Correct |
0 ms |
3348 KB |
Output is correct |
10 |
Correct |
0 ms |
3348 KB |
Output is correct |
11 |
Correct |
0 ms |
3348 KB |
Output is correct |
12 |
Correct |
0 ms |
3348 KB |
Output is correct |
13 |
Correct |
0 ms |
3348 KB |
Output is correct |
14 |
Correct |
29 ms |
3348 KB |
Output is correct |
15 |
Correct |
0 ms |
3348 KB |
Output is correct |
16 |
Correct |
9 ms |
3348 KB |
Output is correct |
17 |
Correct |
6 ms |
3348 KB |
Output is correct |
18 |
Correct |
13 ms |
3348 KB |
Output is correct |
19 |
Correct |
103 ms |
3348 KB |
Output is correct |
20 |
Correct |
316 ms |
3348 KB |
Output is correct |
21 |
Correct |
19 ms |
3348 KB |
Output is correct |
22 |
Correct |
146 ms |
3348 KB |
Output is correct |
23 |
Correct |
9 ms |
3348 KB |
Output is correct |
24 |
Correct |
39 ms |
3348 KB |
Output is correct |
25 |
Correct |
319 ms |
3348 KB |
Output is correct |
26 |
Correct |
0 ms |
3348 KB |
Output is correct |
27 |
Correct |
0 ms |
3348 KB |
Output is correct |
28 |
Correct |
36 ms |
3348 KB |
Output is correct |
29 |
Correct |
0 ms |
3348 KB |
Output is correct |
30 |
Correct |
9 ms |
3348 KB |
Output is correct |
31 |
Correct |
6 ms |
3480 KB |
Output is correct |
32 |
Correct |
16 ms |
3644 KB |
Output is correct |
33 |
Correct |
116 ms |
3644 KB |
Output is correct |
34 |
Correct |
373 ms |
4172 KB |
Output is correct |
35 |
Correct |
19 ms |
4168 KB |
Output is correct |
36 |
Correct |
163 ms |
4160 KB |
Output is correct |
37 |
Correct |
0 ms |
3348 KB |
Output is correct |
38 |
Correct |
29 ms |
3348 KB |
Output is correct |
39 |
Correct |
0 ms |
3348 KB |
Output is correct |
40 |
Correct |
0 ms |
3348 KB |
Output is correct |
41 |
Correct |
0 ms |
3348 KB |
Output is correct |
42 |
Correct |
0 ms |
3348 KB |
Output is correct |