#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int K = 105;
const int INF = 0x3f3f3f3f;
int a[N];
int L[N], minF[N];
int dp[K][N];
int n, k;
int main() {
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
dp[1][0] = 0;
for (int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i - 1], a[i]);
for (int i = 2; i <= k; ++i) {
stack<pair<int, int> > S;
for (int j = i; j <= n; ++j) {
int minF = 1e8;
while (!S.empty() && a[S.top().second] <= a[j]) {
minF = min(minF, S.top().first);
S.pop();
}
dp[i][j] = min(dp[i][S.empty() ? 0 : S.top().second], minF + a[j]);
S.push(make_pair(minF, j));
}
}
cout << dp[k][n] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
41292 KB |
Output is correct |
2 |
Correct |
18 ms |
41328 KB |
Output is correct |
3 |
Correct |
21 ms |
41292 KB |
Output is correct |
4 |
Incorrect |
18 ms |
41268 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
41292 KB |
Output is correct |
2 |
Correct |
20 ms |
41292 KB |
Output is correct |
3 |
Correct |
20 ms |
41308 KB |
Output is correct |
4 |
Incorrect |
20 ms |
41284 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
41292 KB |
Output is correct |
2 |
Correct |
18 ms |
41328 KB |
Output is correct |
3 |
Correct |
21 ms |
41292 KB |
Output is correct |
4 |
Incorrect |
18 ms |
41268 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
41292 KB |
Output is correct |
2 |
Correct |
18 ms |
41328 KB |
Output is correct |
3 |
Correct |
21 ms |
41292 KB |
Output is correct |
4 |
Incorrect |
18 ms |
41268 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |