Cod sursa(job #137846)

Utilizator za_wolfpalianos cristian za_wolf Data 17 februarie 2008 15:20:32
Problema Carnati Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define NMAX 2051
using namespace std;
long in,sf,p1,p2,s,cost,x[NMAX],t[NMAX],c[NMAX],n,m,j,k,l,i,a;
struct kkt
{
	long T,C;
};
kkt q[NMAX];
int cmpf_1(const kkt a,const kkt b)
{
	return a.T<b.T;
}
int main()
{
	freopen("carnati.in","r",stdin);
	freopen("carnati.out","w",stdout);
	scanf("%ld%ld",&n,&k);
	for (i=1;i<=n;i++)
	{
		scanf("%ld%ld",&t[i],&c[i]);
		q[i].T=t[i];
		q[i].C=c[i];
	}
	sort(q+1,q+n+1,cmpf_1);
	for (i=1;i<=n;i++)
	{
		t[i]=q[i].T;
		c[i]=q[i].C;
	}
/*	a=1;
	m=n;
	while (a)
	{
		a=0;
		for (i=1;i<m;i++)
			if (t[i]>t[i+1])
			{
				a=t[i];
				t[i]=t[i+1];
				t[i+1]=a;
				a=c[i];
				c[i]=c[i+1];
				c[i+1]=a;
				a=1;
			}
		m--;
	}
	m=0; */
/*	for (i=1;i<=n;i++)
	{
		if (t[i]==t[i+1])
		{
			j=i;
			while (t[j]==t[j+1])
			{
				c[i]+=c[j+1];
				c[j+1]=0;
				j++;
			}
			i=j-1;
		}
	} */
	s=-10000000;
	for (i=1;i<=n;i++)
	{
		in=i;
		sf=i;
		cost=c[i];
		x[i]=c[i]-k;
		a=c[i];
		p1=i;
		p2=i;
		while (sf<n)
		{
			sf++;
			if (c[sf]>=cost&&t[sf]!=t[sf-1])
				x[sf]=x[sf-1]-(t[sf]-t[sf-1])*k+cost;
			else
				x[sf]=x[sf-1]-(t[sf]-t[sf-1])*k;
			if (x[sf]>a)
			{
				a=x[sf];
				p2=sf;
			}
		}
		a=c[i];
		while (in>1)
		{
			in--;
			if (c[in]>=cost&&t[in]!=t[in+1])
				x[in]=x[in+1]-(t[in+1]-t[in])*k+cost;
			else
				x[in]=x[in+1]-(t[in+1]-t[in])*k;
			if (x[in]>a)
			{
				a=x[in];
				p1=in;
			}
		}
		if (p1!=i&&p2!=i)
		{
			if (s<x[p1]+x[p2]-cost+k)
				s=x[p1]+x[p2]-cost+k;
		}  else
		if (p1==i&&p2!=i)
		{
			if (s<x[p1])
				s=x[p1];
		} else
		if (p1!=i&&p2==i)
		{
			if (s<x[p2])
				s=x[p2];
		} else
		if (p1==i&&p2==i)
			if (s<x[i])
				s=x[i];


		memset(x,0,sizeof(x));
	}
	printf("%ld\n",s);

	return 0;
}