Submission #333265

#TimeUsernameProblemLanguageResultExecution timeMemory
333265nandonathanielK blocks (IZhO14_blocks)C++14
100 / 100
192 ms40892 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...