Cod sursa(job #142796)

Utilizator Binary_FireFlorin Pogocsan Binary_Fire Data 25 februarie 2008 11:37:07
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <stdio.h>
#include <string.h>

#define fin  "carnati.in"
#define fout "carnati.out"

const int Nmax = 2010;

struct inf
{
	int timp;
	long cst;
};

int N;
inf v[Nmax];

long C,best;
long a[Nmax];

inline long max(long a,long b) { return (a>b)?(a):(b); }

void quicksort(int st,int dr)
{
	int m,i,j;
	inf aux;

	m = v[(st + dr) / 2].timp;

	i = st; j = dr;

	do
	{
		while ( v[i].timp < m ) ++i;
		while ( v[j].timp > m ) --j;
		if ( i <= j )
		{
			aux = v[i];
			v[i] = v[j];
			v[j] = aux;
			++i;
			--j;
		}
	} while ( i < j );

	if ( i < dr ) quicksort(i,dr);
	if ( j > st ) quicksort(st,j);
}

void readsolve()
{
	int i,j;
	long pret,aux = 0;

	freopen(fin,"r",stdin);
	freopen(fout,"w",stdout);

	scanf("%i%ld",&N,&C);

	for ( i = 1; i <= N; ++i )
		scanf("%i%ld",&v[i].timp,&v[i].cst);

	quicksort(1,N);
    
	best = v[1].cst - C;

	v[0].timp = v[1].timp - 1;

	for ( i = 1; i <= N; ++i )
	{
        //printf("%i\n",v[i].timp);
		pret = v[i].cst;

		memset(a,0,sizeof(a));

		for ( j = 1; j <= N; ++j )
		{
		    if ( v[j].cst >= pret )
               aux = pret;
            else
               aux = 0;
            
            a[j] = max( a[j-1] - ( v[j].timp - v[j-1].timp ) * C + aux,aux-C);
            
            best = max(best,a[j]); 	
		}
	}

	printf("%ld\n",best);
}

int main()
{
	readsolve();
	return 0;
}