Pagini recente » Cod sursa (job #1055267) | Cod sursa (job #2477327) | Cod sursa (job #2349883) | Cod sursa (job #1041948) | Cod sursa (job #367575)
Cod sursa(job #367575)
#include<fstream>
#include<algorithm>
using namespace std;
ifstream in("carnati.in");
ofstream out("carnati.out");
const int N = 1<<11;
struct client
{
short int t;
int p;
};
client v[N];
int n,c;
inline int max(int x,int y)
{
return x>y ? x : y;
}
void citire()
{
in>>n>>c;
for(short int i=1 ; i<=n ; ++i)
in>>v[i].t>>v[i].p;
in.close();
}
bool cmp(const client &x, const client &y)
{
return x.t < y.t;
}
int profit(int pret)
{
int i,sc,smax;
if(v[1].p>=pret)
sc=smax=pret-c;
else
sc=smax=-c;
for(i=2 ; i<=n ; ++i)
{
if(sc < (v[i].t-v[i-1].t-1)*c)
sc=0;
else
sc-=(v[i].t-v[i-1].t-1)*c;
if(v[i].p>=pret)
sc+=pret;
sc-=c;
smax = max(smax,sc);
}
return smax;
}
int calcul()
{
int maxx=0;
short int i;
for(i=1 ; i<=n ; ++i)
maxx=max(maxx,profit(v[i].p));
return maxx;
}
int main()
{
citire();
sort(v+1,v+n+1,cmp);
out<<calcul()<<"\n";
out.close();
return 0;
}