Cod sursa(job #370088)

Utilizator pirvupirvu tudor pirvu Data 30 noiembrie 2009 09:57:47
Problema Carnati Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#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 ( profit > maxxx ) maxxx=profit;
		
		if (  v[j].b >= pret ) { profit += pret-c*(v[j].a-timp+1); timp=v[j].a+1;}
		
		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;
}