Submission #887117

#TimeUsernameProblemLanguageResultExecution timeMemory
887117maxFedorchukK blocks (IZhO14_blocks)C++14
100 / 100
146 ms81460 KiB
#include<bits/stdc++.h> using namespace std; const long long MXN=1e5+10; const long long MXK=1e2+10; const long long INF=1e18; long long dp[MXK][MXN]; long long a[MXN]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); long long n,k; cin>>n>>k; for(long long i=0;i<=k;i++) { for(long long j=0;j<=n;j++) { dp[i][j]=INF; } } for(long long i=1;i<=n;i++) { cin>>a[i]; } dp[1][1]=a[1]; for(int i=2;i<=n;i++) { dp[1][i]=max(dp[1][i-1],a[i]); } for(long long i=2;i<=k;i++) { stack < pair < long long , long long > > st; for(long long j=1;j<=n;j++) { long long tmp=dp[i-1][j-1]; while(!st.empty() && a[st.top().first]<a[j]) { tmp=min(tmp,st.top().second); st.pop(); } if(!st.empty() && i<=j) { dp[i][j]=dp[i][st.top().first]; } st.push({j,min(dp[i-1][j],tmp)}); if(i<=j) { dp[i][j]=min(dp[i][j],tmp+a[j]); } } } cout<<dp[k][n]<<"\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...