1 #include<bits/stdc++.h>
2 using namespace std;
3 #define LL long long
4 #define MAXN 500010
5 struct node
6 {
7 int p1,p2;
8 }MX;
9 int n,a[MAXN],sum[MAXN],mx[MAXN][19],mxi[MAXN][19],Heap1[2*MAXN+10],Heap2[2*MAXN+10],Heap3[2*MAXN+10],Heap4[2*MAXN+10],Heap5[2*MAXN+10],SIZE;
10 int read()
11 {
12 int s=0,fh=1;char ch=getchar();
13 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();}
14 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();}
15 return s*fh;
16 }
17 void ST()
18 {
19 int i,j;
20 for(i=1;i<=n;i++){mx[i][0]=sum[i];mxi[i][0]=i;}
21 for(j=1;(1<<j)<=n;j++)
22 {
23 for(i=1;i+(1<<j)-1<=n;i++)
24 {
25 if(mx[i][j-1]>=mx[i+(1<<(j-1))][j-1])mx[i][j]=mx[i][j-1],mxi[i][j]=mxi[i][j-1];
26 else mx[i][j]=mx[i+(1<<(j-1))][j-1],mxi[i][j]=mxi[i+(1<<(j-1))][j-1];
27 }
28 }
29 }
30 node Get(int l,int r)
31 {
32 int j;
33 node c1;
34 for(j=0;(1<<j)<=(r-l+1);j++);j--;
35 if(mx[l][j]>=mx[r-(1<<j)+1][j]){c1.p1=mx[l][j];c1.p2=mxi[l][j];}
36 else {c1.p1=mx[r-(1<<j)+1][j];c1.p2=mxi[r-(1<<j)+1][j];}
37 return c1;
38 }
39 void Swap(int root,int now)
40 {
41 swap(Heap1[root],Heap1[now]);
42 swap(Heap2[root],Heap2[now]);
43 swap(Heap3[root],Heap3[now]);
44 swap(Heap4[root],Heap4[now]);
45 swap(Heap5[root],Heap5[now]);
46 }
47 void Push1(int k1,int k2,int k3,int k4,int k5)
48 {
49 int now,root;
50 Heap1[++SIZE]=k1;Heap2[SIZE]=k2;Heap3[SIZE]=k3;Heap4[SIZE]=k4;Heap5[SIZE]=k5;now=SIZE;
51 while(now>1)
52 {
53 root=now/2;
54 if(Heap4[root]>=Heap4[now])return;
55 Swap(root,now);
56 now=root;
57 }
58 }
59 void Pop1(int k)
60 {
61 int now,root;
62 Heap1[k]=Heap1[SIZE];Heap2[k]=Heap2[SIZE];Heap3[k]=Heap3[SIZE];Heap4[k]=Heap4[SIZE];Heap5[k]=Heap5[SIZE--];root=k;
63 while(root<=SIZE/2)
64 {
65 now=root*2;
66 if(now<SIZE&&Heap4[now+1]>Heap4[now])now++;
67 if(Heap4[root]>=Heap4[now])return;
68 Swap(root,now);
69 root=now;
70 }
71 }
72 int main()
73 {
74 int k,l,r,i,L,R,k1,k2,k3,k4,k5;
75 node cc1,cc2;
76 LL ans=0;
77 n=read();k=read();l=read();r=read();
78 for(i=1;i<=n;i++){a[i]=read();sum[i]=sum[i-1]+a[i];}
79 ST();
80 for(i=1;i<=n-l+1;i++)
81 {
82 L=i+l-1;R=min(i+r-1,n);
83 node cc=Get(L,R);
84 Push1(i,L,R,cc.p1-sum[i-1],cc.p2);
85 }
86 while(k--)
87 {
88 k1=Heap1[1];k2=Heap2[1];k3=Heap3[1];k4=Heap4[1];k5=Heap5[1];Pop1(1);
89 ans+=k4;
90 if(k2<=(k5-1))cc1=Get(k2,k5-1);
91 if((k5+1)<=k3)cc2=Get(k5+1,k3);
92 if(k2<=(k5-1))Push1(k1,k2,k5-1,cc1.p1-sum[k1-1],cc1.p2);
93 if((k5+1)<=k3)Push1(k1,k5+1,k3,cc2.p1-sum[k1-1],cc2.p2);
94 }
95 printf("%lld",ans);
96 fclose(stdin);
97 fclose(stdout);
98 return 0;
99 }