Pagini recente » Cod sursa (job #135009) | Cod sursa (job #467248) | Cod sursa (job #2672120) | Cod sursa (job #860532) | Cod sursa (job #629138)
Cod sursa(job #629138)
#include <fstream>
#include <algorithm>
using namespace std;
struct client
{
int t,p;
};
client v[2001];
int c,n;
int max(int x, int y)
{
if(x>y) return x;
else return y;
}
int profit (int pret)
{
int sc=0, smax=-2000000000, prc;
for(int i=1; i<=n; i++)
{
if(v[i].p>=pret)
prc=pret-c;
else
prc=-c;
sc=max(sc-(v[i].t-v[i-1].t-1)*c, 0)+prc;
if(sc>smax)
smax=sc;
}
return smax;
}
bool cmp (client x, client y)
{
return x.t<y.t;
}
int main()
{
int p , profitmax=0;
ifstream in("carnati.in");
ofstream out("carnati.out");
in>>n>>c;
for(int i=1; i<=n; i++)
in>>v[i].t>>v[i].p;
sort(&v[1],&v[n+1],cmp);
for(int i=1; i<=n; i++)
{
p=profit(v[i].p);
if(p>profitmax)
profitmax=p;
}
out<<profitmax;
return 0;
}