Pagini recente » Cod sursa (job #1494154) | Cod sursa (job #2706195) | Cod sursa (job #223307) | Cod sursa (job #765459) | Cod sursa (job #370093)
Cod sursa(job #370093)
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=(1<<11);
struct carnat
{
int a,b;
};
carnat v[N];
int n,c;
bool comp(carnat x,carnat y)
{
return (x.a<y.a);
}
int pret(int x)
{
int profit=0,max=0;
if(v[1].b >= x)
profit = x-c;
else
profit = -c;
max = profit;
for (int i=2;i<=n;i++)
{
profit -= c*(v[i].a-v[i-1].a-1);
if(profit<0)
profit=0;
if(v[i].b>=x)
profit+=x-c;
else
profit-=c;
if(profit>max)
max=profit;
/*
if (profit>max)
max=profit;
if (v[i].b>=x)
profit=profit+ v[i].b-c*(v[i].a-incep+1) ;
if (profit<=0)
{
profit=0;
incep=v[i+1].a;
}
*/
}
return max;
}
int main()
{
freopen("carnati.in","r",stdin);
freopen("carnati.out","w",stdout);
scanf("%d%d",&n,&c);
for (int i=1;i<=n;i++)
scanf("%d%d",&v[i].a,&v[i].b);
sort(v+1,v+n+1,comp);
int max=0;
for (int i=1;i<=n;i++)
{
int p=pret(v[i].b);
if (p>max)
{
//printf("pret=%d, profit=%d\n",v[i].b,p);
max=p;
}
}
printf("%d",max);
return 0;
}