Submission #646997

#TimeUsernameProblemLanguageResultExecution timeMemory
646997nandonathanielK blocks (IZhO14_blocks)C++14
0 / 100
1 ms340 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<int> st; for(int j=i;j<=N;j++){ while(!st.empty() && a[st.top()]<=a[j]){ st.pop(); } if(st.empty()){ dp[i][j]=dp[i-1][i-1]+a[j]; } else dp[i][j]=min(dp[i][st.top()],dp[i-1][st.top()]+a[j]); st.push(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...