Pagini recente » Cod sursa (job #2188362) | Cod sursa (job #1083735) | Cod sursa (job #1566027) | Cod sursa (job #925253) | Cod sursa (job #2129377)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("carnati.in");
ofstream fout("carnati.out");
long long n,i,j,nr,c,mx,prof,st,dr,x,h,r1,pret,rz;
struct om
{ int t,p; } v[2005];
bool comp(om x,om y)
{ return x.t<y.t; }
int main() {
fin>>n>>c;
for(i=1;i<=n;i++)
fin>>v[i].t>>v[i].p;
sort(v+1,v+n+1,comp);
pret=v[1].p;
prof=v[1].p-c;
st=1;
for(i=2;i<=n;i++)
{
r1=0;
mx=0;
for(j=i;j>=st;j--)
{
if(v[j].p>=v[i].p) r1+=v[i].p;
if(r1-(v[i].t-v[j].t+1)*c>mx)
{
mx=r1-(v[i].t-v[j].t+1)*c;
dr=j;
}
}
prof-=c*(v[i].t-v[i-1].t);
if(v[i].p>=pret) prof+=pret;
if(mx>=prof)
{
st=dr;
prof=mx;
pret=v[i].p;
}
if(prof>rz) rz=prof;
}
fout<<rz<<"\n";
}