Pagini recente » Cod sursa (job #257280) | Cod sursa (job #1624219) | Cod sursa (job #1451339) | Cod sursa (job #3157741) | Cod sursa (job #893851)
Cod sursa(job #893851)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
const char infile[] = "carnati.in";
const char outfile[] = "carnati.out";
#define MAXN 2001
int N, C;
struct Carnat
{
int T;
int P;
};
Carnat A[MAXN];
int Result = 0;
int cmp(Carnat& a, Carnat& b)
{
return a.T <= b.T;
}
void citire()
{
fstream fin(infile, ios::in);
fin >> N >> C;
for(int i = 0; i < N; i++)
{
fin >> A[i].T;
fin >> A[i].P;
}
fin.close();
sort(A, A+N, cmp);
}
int pd(int price)
{
int profit = -1;
int last = 0;
for(int i = 0; i < N; i++)
{
if(profit == -1)
{
if(A[i].P >= price)
{
profit = price - C;
last = A[i].T;
}
}
else if(A[i].P < price)
{
continue;
}
else
{
if(profit < profit + price - (A[i].T - last)*C)
{
profit = profit + price - (A[i].T - last)*C;
last = A[i].T;
}
if(profit < price - C)
{
profit = price - C;
last = A[i].T;
}
}
}
return profit;
}
void solve()
{
for(int i = 0; i < N; i++)
{
Result = max(Result, pd(A[i].P));
}
}
void afisare()
{
fstream fout(outfile, ios::out);
fout << Result << "\n";
fout.close();
}
int main()
{
citire();
solve();
afisare();
}