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 fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3qplfh5 ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
using vll=vector<long long>;
const int inf=1e9;
int main(){
_3qplfh5;
int n,l,r,k;
cin>>n>>l>>r>>k;
vi a(n);
rep(i,n){
cin>>a[i];
}
l--,r--;
int m=r-l+1;
vec(vi) dp(m+1,vi(k+1,inf)),nedp;
// m is wall
rep(j,m+1){
dp[j][0]=0;
}
drep(i,l){
nedp=dp;
rep(cost,k+1){
vi pdp(m+1,inf);
pdp[m]=dp[m][cost];
drep(j,m){
pdp[j]=dp[j][cost];
pdp[j]=min(pdp[j],pdp[j+1]);
}
drep(j,m){
int necost=cost+j+l-i;
if(necost>k) continue;
nedp[j][necost]=min(nedp[j][necost],pdp[j+1]+a[i]-a[j+l]);
}
}
swap(dp,nedp);
}
vec(vi) fdp=dp;
{
rep(j,k+1){
rep(i,m+1){
if(i) fdp[i][j]=min(fdp[i][j],fdp[i-1][j]);
if(j) fdp[i][j]=min(fdp[i][j],fdp[i][j-1]);
}
}
}
vec(vi) odp=dp;
dp=vec(vi)(m+1,vi(k+1,inf));
rep(j,m+1){
dp[j][0]=0;
}
crep(i,r+1,n){
nedp=dp;
rep(cost,k+1){
vi pdp(m+1,inf);
pdp[0]=dp[0][cost];
crep(j,1,m){
pdp[j]=dp[j][cost];
pdp[j]=min(pdp[j],pdp[j-1]);
}
rep(j,m){
int necost=cost+i-(j+l);
if(necost>k) continue;
nedp[j][necost]=min(nedp[j][necost],(j==0?0:pdp[j-1])+a[i]-a[j+l]);
}
}
swap(dp,nedp);
}
int ans=inf;
rep(j,m+1){
rep(cost,k+1){
ans=min(ans,odp[j][cost]);
ans=min(ans,dp[j][cost]);
}
}
rep(j,m){
vi mi(k+1,inf);
mi[0]=0;
drep(i,j){
rep(cost,k+1){
mi[cost]=min(mi[cost],odp[i][cost]);
}
}
crep(cost,1,k+1){
mi[cost]=min(mi[cost-1],mi[cost]);
}
crep(j1,j,m){
rep(cost,k+1){
ans=min(ans,dp[j1][cost]+mi[k-cost]);
}
}
}
crep(i,l,r+1){
ans+=a[i];
}
cout<<ans<<"\n";
//
return 0;
}
# | 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... |