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>
using namespace std;
#define all(a) a.begin(),a.end()
#define ll long long
#define pb push_back
#define nl '\n'
#define popb pop_back()
#define sz size()
#define ld long double
#define ull unsigned long long
#define F first
#define S second
#define fix fixed<<setprecision
#define pii pair<int,int>
#define E exit (0)
#define int long long
const int inf=1e9;
int dp[100001][101];
signed main() {
//freopen("planting.in","r",stdin);
//freopen("planting.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
int c[n+1];
for (int i=1;i<=n;i++) {
cin>>c[i];
}
for (int i=0;i<=n;i++) {
for (int j=0;j<=k;j++) {
dp[i][j]=inf;
}
}
for (int i=1,mx=0;i<=n;i++) {
mx=max(mx,c[i]);
dp[i][1]=mx;
}
dp[0][0]=0;
for (int i=2;i<=k;i++) {
stack<pii>q;
for (int j=1;j<=n;j++) {
int mn=dp[j-1][i-1];
while(!q.empty()&&q.top().F<=c[j]) {
mn=min(mn,q.top().S);
q.pop();
}
if (q.empty()||mn+c[j]<=q.top().F+q.top().S) {
q.push({c[j],mn});
}
if (j<i) {
continue;
}
dp[j][i]=q.top().F+q.top().S;
}
}
cout<<dp[n][k];
}
# | 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... |