Pagini recente » Cod sursa (job #1592402) | Cod sursa (job #1090827) | Cod sursa (job #1431910) | Cod sursa (job #3202434) | Cod sursa (job #370102)
Cod sursa(job #370102)
#include<cstdio>
#include<algorithm>
using namespace std;
int n,c,pret,maxx,o,i;
struct stoica
{
int a, b;
};
stoica v[1000];
bool comp(stoica x, stoica y)
{
return ( x.a < y.a) ;
}
int functie ( int pret)
{
int profit=0,maxxx=0;
if(v[1].b>=pret)
profit=pret-c;
else
profit=-c;
maxxx=profit;
for (int j=2;j<=n;j++)
{
/*
if ( v[j].b >= pret && (pret-c*(v[j].a-timp+1)>0)) { profit += pret-c*(v[j].a-timp+1); timp=v[j].a+1;}
if ( profit > maxxx ) maxxx=profit;
if ( profit <= 0) { profit=0; timp=v[j+1].a;}
if ( v[j].b >= pret && pret-c>maxxx) {maxxx=pret-c;profit=maxxx; timp=v[j].a+1;}*/
profit -= ( v[j].a - v[j-1].a -1) * c;
if(profit<0)
profit=0;
if ( v[j].b >= pret)
profit+=pret-c;
else
profit-=c;
if (profit>maxxx) maxxx=profit;
}
return maxxx;
}
int main()
{
freopen("carnati.in","r",stdin);
freopen("carnati.out","w",stdout);
scanf("%d%d", &n,&c);
for (i=1;i<=n;i++)
scanf("%d%d", &v[i].a, &v[i].b);
sort ( v +1 , v+n+1 , comp);
for (i=1;i<=n;i++)
{
o=functie ( v[i].b);
if ( o > maxx ) maxx=o;
}
printf("%d" , maxx);
return 0;
}