Pagini recente » Cod sursa (job #2998767) | Cod sursa (job #532412) | Cod sursa (job #1251542) | Borderou de evaluare (job #1497334) | Cod sursa (job #2704173)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("carnati.in");
ofstream fout("carnati.out");
struct carnaciori{
long long int time,price;
}v[2010];
bool cmp(carnaciori a,carnaciori b){
if(a.time<=b.time)
return true;
return false;
}
int main()
{
long long int i,n,sellerPrice,totalMax=0,currentIncome=0,currentPrice,waitingTime=0,added;
fin>>n>>sellerPrice;
for(i=1;i<=n;i++){
fin>>v[i].time>>v[i].price;
}
sort(v+1,v+n+1,cmp);
for(int k=1;k<=n;k++){
currentPrice=v[k].price;
currentIncome=0;
waitingTime=v[1].time;
added=0;
for(i=1;i<=n;i++){
if(currentPrice<=v[i].price){
currentIncome+=currentPrice;
added=1;
}
if(currentIncome-(v[i].time-waitingTime)*sellerPrice<=0){
if(added==1){
currentIncome-=currentPrice;
added=0;
}
if(currentIncome>totalMax)
totalMax=currentIncome;
currentIncome=0;
waitingTime=v[i+1].time;
}
else{
currentIncome-=(v[i].time-waitingTime)*sellerPrice;
waitingTime=v[i].time;
}
if(currentIncome>totalMax)
totalMax=currentIncome;
}
if(currentIncome>totalMax)
totalMax=currentIncome;
}
if(totalMax-sellerPrice<=0)
fout<<"0";
else
fout<<totalMax-sellerPrice<<endl;
return 0;
}