This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int n, k;
int a[100005];
int dp[100005][105];
int main(){
cin >> n >> k;
int sum=0;
for(int i=1;i<=n;i++){
cin >> a[i];
sum+=a[i];
}
memset(dp,1e9+7,sizeof(dp));
dp[0][1]=0;
for(int i=1;i<=n;i++){
dp[i][1]=max(dp[i-1][1],a[i]);
}
dp[0][1]=1e9+7;
for(int i=2;i<=k;i++){
queue<int> pval, val;
for(int j=1;j<=n;j++){
int x = dp[j-1][i-1];
while(!pval.empty()&&a[pval.back()]<a[j]){
x=min(x,val.back());
pval.pop();
val.pop();
}
if(pval.size()){
dp[j][i]=dp[pval.back()][i];
}
pval.push(j);
val.push(min(dp[j][i-1],x));
dp[j][i]=min(dp[j][i],x+a[j]);
}
}
cout << dp[n][k] << endl;
return 0;
}
// 10 3 7 9 21 3 9 10 4
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |