#include <bits/stdc++.h>
using namespace std;
const int mxn = 100 * 1000 + 3;
int a[mxn], p[mxn], dp[mxn][103], n, k, mx = 0;
signed main() {
std::ios::sync_with_stdio(0);
std::cout.tie(0);
std::cin.tie(0);
#ifdef saarang
freopen("/home/saarang/Documents/cp/input.txt", "r", stdin);
freopen("/home/saarang/Documents/cp/output.txt", "w", stdout);
#endif
cin >> n >> k;
vector<array<int, 2>> l(n + 1), r(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
l[i][0] = r[i][0] = i;
l[i][1] = r[i][1] = i;
mx = max(mx, a[i]);
dp[1][i] = mx;
}
stack<int> s;
for(int i = 1; i <= n; i++) {
while(!s.empty() && a[s.top()] <= a[i])
s.pop();
l[i][0] = s.empty() ? 1 : s.top() + 1;
s.push(i);
}
while(!s.empty()) s.pop();
for(int i = n; i; i--) {
while(!s.empty() && a[s.top()] <= a[i])
s.pop();
r[i][0] = s.empty() ? n : s.top() - 1;
s.push(i);
}
// for(int i = 1; i <= n; i++)
// cout << l[i][0] << ' ' << r[i][0] << '\n';
sort(l.begin() + 1, l.end());
sort(r.begin() + 1, r.end());
for(int i = 2; i <= k; i++) {
deque<array<int, 2>> dq;
for(int x = 1; x <= n; x++) {
auto &[id, j] = l[x];
while(!dq.empty() && dq.back()[0] >= dp[i - 1][j])
dq.pop_back();
dq.push_back({dp[i - 1][j], j});
while(!dq.empty() && dq.front()[1] < id)
dq.pop_front();
dp[i][j] = dq.empty() ? 1e9 : (a[j] + dq.front()[0]);
}
}
// for(int i = 1; i <= k; i++) {
// for(int j = 1; j <= n; j++)
// cout << dp[i][j] << ' ';
// cout << '\n';
// }
cout << dp[k][n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |