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 ll long long
#define ull unsigned ll
#define pb push_back
#define epb emplace_back
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define inf 1e9+1
#define linf 1e18+1
using namespace std;
int dp[100001][101];
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n;cin>>n;
int k;cin>>k;
int a[n+1];
for(int i=1;i<=n;i++){
cin>>a[i];
}
dp[0][0]=0;
for(int i=0;i<=n;i++){
for(int j=0;j<=k;j++)dp[i][j]=inf;
}
dp[0][0]=0;
for(int j=1;j<=k;j++){
stack<pii>st;
for(int i=1;i<=n;i++){
int cur=dp[i-1][j-1];
while(!st.empty()){
int x=st.top().f;
if(a[x]>=a[i])break;
int y=st.top().s;
cur=min(y,cur);
st.pop();
}
if(!st.empty()){
int tp=st.top().f;
dp[i][j]=min({dp[i][j],dp[tp][j],cur+a[i]});
}
else{
dp[i][j]=min(dp[i][j],cur+a[i]);
}
st.push({i,cur});
}
}
cout<<dp[n][k]<<' ';
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... |