Cod sursa(job #1220195)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 16 august 2014 19:45:40
Problema Carnati Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

#define MAX(a,b) ((a) > (b) ? (a) : (b))

struct pk{
	int time,gain;
};
int n,c,rez[2005];
pk a[2005];

void solve(){
	for(int i = 0; i < n; i++)
		rez[i] = -c;
	int newJ;
	for(int i = 0; i < n; i++){
		newJ = -1;
		for(int j = 0; j < n; j++){
			if(a[i].gain <= a[j].gain){
				int idx = newJ == -1 ? j : newJ;
				int g = a[i].gain - abs(a[j].time - a[idx].time) * c;
				if(rez[i] < rez[i] + g){
					rez[i] += g;
					newJ = j;
				}

				/*rez[i] = MAX(rez[i],rez[i] + c + a[i].gain - (abs(a[j].time - a[i].time) + 1) * c);	
				if(j > i)*/
			}
		}
	}
	int max = -(1 << 30);
	for(int i = 0; i < n; i++)
		max = rez[i] > max ? rez[i] : max;
	printf("%d\n",max);

}
int cmp(pk a, pk b)
{
    return a.time < b.time;
}
int main(){
	freopen("carnati.in","r",stdin);
	freopen("carnati.out","w",stdout);

	scanf("%d%d",&n,&c);
	int g,t;
	for(int i = 0; i < n; i++){
		scanf("%d%d",&t,&g);
		a[i].gain = g;
		a[i].time = t;
	}
	sort(a,a+n,cmp);
	solve();

	return 0;
}