#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> ii;
const int N = 1e5 + 7, M = 107;
int n, k, a[N], L[N], dp[N][M];
stack<int> st;
int T[N][M];
void upd(int i, int j, int v) {
for (; i <= n; i += i & -i) T[i][j] = min(T[i][j], v);
}
int get(int i, int l, int r) {
int res = 2e9;
while (l <= r) {
if (r - (r & -r) >= l) {
res = min(res, T[r][i]);
r -= r & -r;
}
else {
res = min(res, dp[r][i]);
--r;
}
}
return res;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
memset(T, 0x3f, sizeof T);
dp[0][1] = 0;
for (int i = 1; i <= n; i++) {
dp[i][1] = max(dp[i - 1][1], a[i]);
upd(i, 1, dp[i][1]);
}
for (int i = 1; i <= n; i++) {
while (st.size() && a[st.top()] <= a[i]) st.pop();
L[i] = (st.empty() ? 1 : st.top());
if (st.empty()) {
for (int j = 2; j <= min(i, k); j++) {
dp[i][j] = get(j - 1, 1, i) + a[i];
upd(i, j, dp[i][j]);
}
}
else {
for (int j = 2; j <= min(i, k); j++) {
dp[i][j] = min(dp[L[i]][j], dp[L[i] - 1][j - 1] + a[L[i]]);
upd(i, j, dp[i][j]);
}
}
st.push(i);
// f(i, j) = min(f(k - 1, j - 1) + max(a[k]..a[i]))
}
cout << dp[n][k];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
84088 KB |
Output is correct |
2 |
Correct |
34 ms |
84056 KB |
Output is correct |
3 |
Correct |
32 ms |
84044 KB |
Output is correct |
4 |
Correct |
33 ms |
84084 KB |
Output is correct |
5 |
Incorrect |
34 ms |
83968 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
84044 KB |
Output is correct |
2 |
Correct |
35 ms |
83948 KB |
Output is correct |
3 |
Correct |
36 ms |
84052 KB |
Output is correct |
4 |
Correct |
35 ms |
84004 KB |
Output is correct |
5 |
Incorrect |
34 ms |
84040 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
84088 KB |
Output is correct |
2 |
Correct |
34 ms |
84056 KB |
Output is correct |
3 |
Correct |
32 ms |
84044 KB |
Output is correct |
4 |
Correct |
33 ms |
84084 KB |
Output is correct |
5 |
Incorrect |
34 ms |
83968 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
84088 KB |
Output is correct |
2 |
Correct |
34 ms |
84056 KB |
Output is correct |
3 |
Correct |
32 ms |
84044 KB |
Output is correct |
4 |
Correct |
33 ms |
84084 KB |
Output is correct |
5 |
Incorrect |
34 ms |
83968 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |