Cod sursa(job #370094)

Utilizator Teodor94Teodor Plop Teodor94 Data 30 noiembrie 2009 10:09:15
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#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;
	}
	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)
			max=p;
	}
	printf("%d",max);
	return 0;
}