Pagini recente » Cod sursa (job #1155143) | Cod sursa (job #2728200) | Cod sursa (job #284209) | Cod sursa (job #3192524) | Cod sursa (job #2409124)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("carnati.in");
ofstream g("carnati.out");
struct variabila{
int time, money;
};
typedef long long int ll;
const int Nmax = 2005;
variabila v[Nmax];
int best[Nmax];
int n, price, ans;
bool cmp(const variabila &a, const variabila &b){
return a.time < b.time;
}
int main(){
int i,j,x,value;
f >> n >> price;
for(i = 1 ; i <= n; i++)
f >> v[i].time >> v[i].money;
sort(v + 1, v + n + 1, cmp);
v[0].money = v[0].time = -Nmax;
ans = 0;
for(i = 1 ; i <= n ; i++){
x = v[i].money;
for(j = 1 ; j <= n ; j++){
if(v[j].money >= x){
value = best[j-1] - (v[j].time - v[j-1].time) * price + x;
best[j] = max(value, x - price);
}else{
value = best[j-1] - (v[j].time - v[j-1].time) * price;
best[j] = max(value, -price);
}
ans = max(ans, best[j]);
}
}
g << ans;
return 0;
}