Pagini recente » Cod sursa (job #1982680) | Cod sursa (job #1783902) | Cod sursa (job #1805429) | Cod sursa (job #1289003) | Cod sursa (job #495959)
Cod sursa(job #495959)
#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 ((v[i].b >= pret) * pret - c > profitc + (v[i].b >= pret) * pret - (v[i].t - v[i - 1].t) * 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;
}