Cod sursa(job #490852)

Utilizator gramatovici_paulGramatovici Paul gramatovici_paul Data 8 octombrie 2010 15:53:41
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include<stdio.h>
#include<algorithm>

using namespace std;

const int inf=2000000000;
int n,sal;
struct client
{
	int t,p;
};

client v[1501];
void citire()
{
	scanf("%d%d",&n,&sal);
	for (int i=1;i<=n;++i)
		scanf("%d%d",&v[i].t,&v[i].p);
}
int val(int pret)
{
	int sc,smax,u,i,deficit;
	sc=0;
	smax=-inf;
	u=-1;
	for (i=1;i<=n;++i)
	{
		if (v[i].p<pret)
			continue;
		if (u!=-1)
			deficit=(v[i].t-u-1)*sal;
		else
			deficit=0;
		if (sc<deficit)
			sc=0;
		else
			sc-=deficit;
		sc=pret+sc-sal;
		if (sc>smax)
			smax=sc;
		u=v[i].t;
	}
	return smax;
}
bool comp(client x, client y)
{
	return x.t<y.t;
}
int main()
{
	freopen("carnati.in","r",stdin);
	freopen("carnati.out","w",stdout);
	citire();
	int i,x,max=-inf;	
	sort(&v[1],&v[1+n],comp);
	for (i=1;i<=n;++i)
	{
		x=val(v[i].p);
		if (max<x) max=x;
	}
	printf("%d",max);
	return 0;
}