Where lies a strange country, lices a strange monster. The strange people in that strange country are persecuted by the strange monster. So teh strange people make a strange cannon to defeat the strange monster.
As far as we know, the strange cannon is powerful, but once it works, it should not work in next T hours (Eg. If it fired at time 1, then time 2 to time T it cannot work). When the strange monster get hit during the daylight, it get PD point hurt; it will get PN point hurt when it happens during the night. As the country is strange, the time of the daylight and night changes eveyday. In the ith day, the daylight starts at time 1 and ends at time T1[i], then from time (T1[i]+1) to time (T1[i]+T2[i]) is night.
Here comes a question: what is the maximal hurt point would the strange monster got after N days?
For each case, first line contains 4 integers: N (1<=N<=1000), T (1<=T<=100), PD, PN.
Then followed N lines, the (i+1)th line contains 2 integer T1[i], T2[i].
(PD, PN, T1[i], T2[i] are all non-negative and fit the 32bit integer)
1 2 5 20 5 6 10 2 1
Case 1: 65
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=700000;
__int64 f[maxn];//f[i]表示以i结尾且第i个小时打炮的最大伤害
//f[i]=max(f[k])+pd|pn;1<=k<=i-t;
bool d[maxn];
__int64 Q[maxn];
int I[maxn];
int main()
{
int ci,pl=1;scanf("%d",&ci);
while(ci--)
{
int n,t;
__int64 pd,pn;
scanf("%d%d%I64d%I64d",&n,&t,&pd,&pn);
__int64 extra=0;
int head=1,tail=0;
int m=0;//小时
for(int i=0;i<n;i++)
{
__int64 t1,t2;scanf("%I64d%I64d",&t1,&t2);
__int64 tmp;
if(t1>t+t) tmp=(t1-t-t)/t;
else tmp=0;
extra+=tmp*pd;
for(int j=0;j<t1-tmp*t;j++)
{
d[++m]=1;
}
if(t2>=t+t) tmp=(t2-t-t)/t;
else tmp=0;
extra+=tmp*pn;
for(int j=0;j<t2-tmp*t;j++)
{
d[++m]=0;
}
}
__int64 res=0;
for(int i=1;i<=m;i++)
{
if(i-t>=1) //f[i-t] 入队
{
while(head<=tail&&Q[tail]<=f[i-t]) tail--;//a[i]入队
tail++;
Q[tail]=f[i-t],I[tail]=i-t;
f[i]=Q[head]+(d[i]?pd:pn);
}
else f[i]=(d[i]?pd:pn);
res=max(res,f[i]);
}
printf("Case %d: %I64d\n",pl++,extra+res);
}
return 0;
}