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>
using namespace std;
const int MAXK=105,MAXN=100005;
int dp[MAXK][MAXN],a[MAXN];
typedef pair<int,int> pii;
//https://codeforces.com/blog/entry/18866
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int N,K;
cin >> N >> K;
for(int i=1;i<=N;i++)cin >> a[i];
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<pii> st;
for(int j=i;j<=N;j++){
int minDP=dp[i-1][j-1];
while(!st.empty() && a[st.top().first]<=a[j]){
minDP=min(minDP,st.top().second);
st.pop();
}
int val=1e9;
if(!st.empty())val=dp[i][st.top().first];
dp[i][j]=min(val,minDP+a[j]);
st.push({j,minDP});
}
}
cout << dp[K][N] << '\n';
return 0;
}
# | 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... |