#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N=500005;
struct node{
int left,right,val,start,id;
bool operator <(const node &x)const{return x.val>val;}
}dp[N][40];
priority_queue<node>q;
int n,k,l,r,a[N],sum[N],log[N];
long long ans;
inline void pre_st()
{
log[1]=0;
for(int i=2,t=1;i<=n;i++)
{
while((1<<t)<=i)t++;
log[i]=t-1;
}
for(int i=1;i<=n;i++)dp[i][0].val=sum[i],dp[i][0].id=i;
for(int j=1;(1<<j)<=n;j++)for(int i=1;i+(1<<j)-1<=n;i++)
if(dp[i][j-1].val<dp[i+(1<<(j-1))][j-1].val)dp[i][j]=dp[i+(1<<(j-1))][j-1];
else dp[i][j]=dp[i][j-1];
}
inline node query(int x,int y)
{
int m=log[y-x+1];
if(dp[x][m].val<dp[y-(1<<m)+1][m].val)return dp[y-(1<<m)+1][m];
return dp[x][m];
}
inline void pus(int ll,int rr,int head)
{
if(ll>rr) return;
node sub=query(ll,rr);
sub.val-=sum[head-1],sub.left=ll,sub.right=rr,sub.start=head;
q.push(sub);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>k>>l>>r;
for(int i=1;i<=n;i++)cin>>a[i],sum[i]=sum[i-1]+a[i];
pre_st();
for(int i=1;i<=n;i++)
{
if(i+l-1>n)continue;
int l1=min(n,i+l-1),r1=min(n,i+r-1);
pus(l1,r1,i);
}
while(k--)
{
node now=q.top(),nex,ney; q.pop();
ans+=now.val;
pus(now.left,now.id-1,now.start);
pus(now.id+1,now.right,now.start);
}
cout<<ans<<endl;
}
转载于:https://www.cnblogs.com/Ace-MYX/p/10364011.html