Cod sursa(job #495956)

Utilizator toniobFMI - Barbalau Antonio toniob Data 27 octombrie 2010 12:33:48
Problema Carnati Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
using namespace std;

ifstream in ("carnati.in");
ofstream out ("carnati.out");

const int N = 2002;
struct carnati {
	int t, b;
} v[N];
int n, c, profit;

int comp (carnati a, carnati b) {
	return a.t < b.t;
}

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

void calc_profit (int pret) {
	int profitc = (v[1].b >= pret) * pret - c;
	for (int i = 2; i <= n; ++i) {
		if (profitc < profitc + (v[i].b >= pret) * pret - c) {
			profitc = (v[i].b >= pret) * pret - c;
		} else {
			profitc += (v[i].b >= pret) * pret - (v[i].t - v[i - 1].t) * c;
		}
		profit = max (profit, profitc);
	}
}

void citire () {
	in >> n >> c;
	for (int i = 1; i <= n; in >> v[i].t >> v[i].b, ++i);
}

void exe () {
	sort (v + 1, v + n + 1, comp);
	for (int i = 1; i <= n; ++i) {
		calc_profit (v[i].b);
	}
}

void afisare () {
	out << profit;
}

int main () {
	citire ();
	
	exe ();
	
	afisare ();
	
	return 0;
}