#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
int main() {
#ifndef ONLINE_JUDGE
//freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
vector<ll> nums(n);
for_(i, 0, n) cin >> nums[i];
vector<ll> prev(n), curr(n, 1e15);
prev[n-1] = nums[n-1];
for (int i = n-2; i >= 0; i--) prev[i] = max(nums[i], prev[i+1]);
for_(l, 1, k) {
stack<vector<ll>> s; // {max val index, min prev val till next max}
//curr[n-l-1] = nums[n-l-1] + prev[n-l];
//s.push({n-l-1, prev[n-l]});
for (int i = n-2; i >= 0; i--) {
ll mn = prev[i+1], ans = prev[i+1]+nums[i];
while (s.size()) {
if (nums[s.top()[0]] <= nums[i]) {
mn = min(mn, s.top()[1]);
s.pop();
} else {
ans = min(ans, curr[s.top()[0]]);
break;
}
}
s.push({i, mn});
curr[i] = min(ans, nums[i] + mn);
}
swap(prev, curr);
curr.assign(n, 1e15);
}
cout << prev[0] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
388 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
768 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
22 ms |
2304 KB |
Output is correct |
4 |
Correct |
216 ms |
2304 KB |
Output is correct |
5 |
Correct |
640 ms |
4884 KB |
Output is correct |
6 |
Correct |
25 ms |
4728 KB |
Output is correct |
7 |
Correct |
294 ms |
5496 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
52 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
15 ms |
640 KB |
Output is correct |
17 |
Correct |
6 ms |
1664 KB |
Output is correct |
18 |
Correct |
19 ms |
1664 KB |
Output is correct |
19 |
Correct |
178 ms |
1756 KB |
Output is correct |
20 |
Correct |
518 ms |
3328 KB |
Output is correct |
21 |
Correct |
22 ms |
3456 KB |
Output is correct |
22 |
Correct |
237 ms |
3328 KB |
Output is correct |
23 |
Correct |
12 ms |
3456 KB |
Output is correct |
24 |
Correct |
58 ms |
3448 KB |
Output is correct |
25 |
Correct |
523 ms |
3576 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
48 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
13 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
1664 KB |
Output is correct |
32 |
Correct |
18 ms |
1792 KB |
Output is correct |
33 |
Correct |
173 ms |
1664 KB |
Output is correct |
34 |
Correct |
481 ms |
3328 KB |
Output is correct |
35 |
Correct |
21 ms |
3456 KB |
Output is correct |
36 |
Correct |
217 ms |
3456 KB |
Output is correct |
37 |
Correct |
2 ms |
768 KB |
Output is correct |
38 |
Correct |
74 ms |
832 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |