代码如下:
#include<bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1005;
int n,m,w;
int ap[N],bp[N],as[N],bs[N],f[N][N];
int hd,ed,q[N*2];
signed main(){
cin>>n>>m>>w;
rep(i,1,n) cin>>ap[i]>>bp[i]>>as[i]>>bs[i];
rep(i,0,n) rep(j,0,m) f[i][j]=-INF;
rep(i,1,n){
rep(j,0,as[i]) f[i][j]=-j*ap[i];
rep(j,0,m) f[i][j]=max(f[i][j],f[i-1][j]);
if(i<=w) continue;
int ls=i-w-1;
hd=1,ed=0;
rep(j,0,m){
while(hd<=ed&&q[hd]<j-as[i]) hd++;
while(hd<=ed&&f[ls][q[ed]]+q[ed]*ap[i]<=f[ls][j]+j*ap[i]) ed--;
q[++ed]=j;
if(hd<=ed) f[i][j]=max(f[i][j],f[ls][q[hd]]-(j-q[hd])*ap[i]);
}
hd=1,ed=0;
repr(j,m,0){
while(hd<=ed&&q[hd]>j+bs[i]) hd++;
while(hd<=ed&&f[ls][q[ed]]+q[ed]*bp[i]<=f[ls][j]+j*bp[i]) ed--;
q[++ed]=j;
if(hd<=ed) f[i][j]=max(f[i][j],f[ls][q[hd]]+(q[hd]-j)*bp[i]);
}
}
int ans=-INF;
rep(i,0,m) ans=max(ans,f[n][i]);
cout<<ans<<endl;
}
开WallExtra后不显示任何编译阶段疑似错误,本机开O2和洛谷开O2可以额外通过#5。