#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
const int maxk = 110;
int n, k;
int dp[maxn][maxk];
int arr[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
int prefmax = 0;
for(int i=0;i<=n;i++) {
for(int j=0;j<=k;j++) {
dp[i][j] = INT_MAX;
}
}
for(int i=1;i<=n;i++) {
cin>>arr[i];
prefmax = max(prefmax, arr[i]);
dp[i][1] = prefmax;
}
for(int d=2;d<=k;d++) {
stack<int>dps, arrs;
for(int i=1;i<=n;i++) {
int curr = dp[i-1][d-1];
while(!arrs.empty() && arrs.top() < arr[i]) {
curr = min(curr, dps.top());
arrs.pop();
dps.pop();
}
if(arrs.empty() || arrs.top() + dps.top() >= curr + arr[i]) {
arrs.push(arr[i]);
dps.push(curr);
}
if(i >= d) dp[i][d] = arrs.top() + dps.top();
}
}
cout<<dp[n][k]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
4736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |