Pagini recente » Profil M@2Te4i | Cod sursa (job #1430153) | Cod sursa (job #1962892) | Cod sursa (job #1567320) | Cod sursa (job #2072224)
#include <cstdio>
#include <algorithm>
using namespace std;
pair <int,int> v[2001];
int profit (int x,int pret){
if (x<pret)
return 0;
else return pret;
}
int main()
{
FILE *fin=fopen ("carnati.in","r");
FILE *fout=fopen ("carnati.out","w");
int n,i,j,pret,ok;
long long maxi,sc,c;
fscanf (fin,"%d%lld",&n,&c);
for (i=1;i<=n;i++)
fscanf (fin,"%d%d",&v[i].first,&v[i].second);
sort (v+1,v+n+1);
maxi=0;
for (i=1;i<=n;i++){
pret=v[i].second;
sc=0;
ok=1;
for (j=1;j<=n;j++){
if (sc==0 && ok==1){
if (profit (v[j].second,pret) && pret-c>=0){
sc=pret-c;
ok=0;
}
}
else if (sc-(v[j].first-v[j-1].first)*c+profit (v[j].second,pret) >= max(profit (v[j].second,pret)-c,1LL*0)){
sc=sc-(v[j].first-v[j-1].first)*c+profit (v[j].second,pret);
ok=0;
}
else {
sc=max(profit (v[j].second,pret)-c,1LL*0);
if (!sc)
ok=1;
}
if (maxi<sc)
maxi=sc;
//maxi=max(maxi,sc);
}
}
fprintf (fout,"%lld",maxi);
return 0;
}