Pagini recente » Cod sursa (job #2481949) | Cod sursa (job #667250) | Cod sursa (job #2052875) | Cod sursa (job #291625) | Cod sursa (job #370090)
Cod sursa(job #370090)
#include<cstdio>
#include<algorithm>
using namespace std;
int n,c,i,pret,maxx,o;
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,timp=v[1].a,maxxx=0;
for (int j=1;j<=n;j++)
{
if ( v[j].b >= pret ) { 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;}
}
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;
}