Pagini recente » Cod sursa (job #1635240) | Cod sursa (job #677842) | Cod sursa (job #2752019) | Cod sursa (job #1315131) | Cod sursa (job #142408)
Cod sursa(job #142408)
#include<stdio.h>
FILE*fin=fopen("carnati.in","r");
FILE*fout=fopen("carnati.out","w");
int n,p[2001],t[2001],c;
void rech(int nod,int dim)
{
int nd=nod,max=t[nod];
int st=nod<<1,dr=st+1;
if(t[st]>max&&st<=dim){max=t[st];nd=st;}
if(t[dr]>max&&dr<=dim){max=t[dr];nd=dr;}
if(nd!=nod)
{
t[nd]^=t[nod]^=t[nd]^=t[nod];
p[nd]^=p[nod]^=p[nd]^=p[nod];
rech(nd,dim);
}
}
void ord()
{
int i;
for(i=n/2;i>=1;i--)
rech(i,n);
int dim=n;
while(dim>1)
{
t[1]^=t[dim]^=t[1]^=t[dim];
p[1]^=p[dim]^=p[1]^=p[dim];
dim--;
rech(1,dim);
}
}
int main()
{
int i,j,pf;
long long maxl,sp,maxr,best;
fscanf(fin,"%d%d",&n,&c);
for(i=1;i<=n;i++)
fscanf(fin,"%d%d",&t[i],&p[i]);
fclose(fin);
ord();
best=0;
for(i=1;i<=n;i++)
{
pf=p[i];
sp=0;
maxr=0;
for(j=i;j>=1;j--)
{
if(p[j]>=pf) sp+=pf;
if(sp-(t[i]-t[j]+1)*c>maxr) maxr=sp-(t[i]-t[j]+1)*c;
}
sp=0;
maxl=0;
for(j=i+1;j<=n;j++)
{
if(p[j]>=pf) sp+=pf;
if(sp-(t[j]-t[i])*c>maxl) maxl=sp-(t[j]-t[i])*c;
}
if(maxl+maxr>best) best=maxl+maxr;
}
fprintf(fout,"%lld",best);
fclose(fout);
return 0;
}