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 pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
int main() {
int n,K;
cin>>n>>K;
int a[n+1];
for(int i = 1; i <= n; i++) cin>>a[i];
int dp[n+1][K+1];
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= K; j++) {
dp[i][j] = inf;
}
}
dp[0][0] = 0;
dp[1][1] = a[1];
for(int i = 2; i <= n; i++) {
dp[i][1] = max(dp[i-1][1],a[i]);
}
for(int j = 2; j <= K; j++) {
stack<pii> st;
for(int i = 1; i <= n; i++) {
int cur = dp[i - 1][j - 1];
while(!st.empty() && a[st.top().first] <= a[i]) {
cur = min(cur,st.top().second);
st.pop();
}
if(!st.empty()) dp[i][j] = dp[st.top().first][j];
dp[i][j] = min(dp[i][j],cur + a[i]);
st.push({i,cur});
}
}
cout<<dp[n][K]<<endl;
}
# | 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... |