Pagini recente » Profil faranume | Carry | Medie | Diferente pentru preoni-2008/runda-1/5-8 intre reviziile 26 si 8 | Cod sursa (job #137218)
Cod sursa(job #137218)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N, Cst;
#define MAXN 2048
vector<int> P;
vector< pair<int, int> > client;
int bst[MAXN][MAXN];
//bst[i][j] = profitul maxim pentru un interval [k, i], k <= i daca costul unui carnat este j (normalizat)
int main()
{
freopen("carnati.in", "rt", stdin);
freopen("carnati.out", "wt", stdout);
scanf("%d %d", &N, &Cst);
for (int i = 0; i < N; i++)
{
int cT, cP;
scanf("%d %d", &cT, &cP);
client.push_back( make_pair( cT, cP ) );
P.push_back(cP);
}
sort( P.begin(), P.end() );
P.resize( unique( P.begin(), P.end() ) - P.begin() );
sort( client.begin(), client.end() );
int Max = -1;
for (int j = 0; j < (int)P.size(); j++)
{
if (client[0].second >= P[j])
bst[0][j] = P[j] - Cst;
else
bst[0][j] = -Cst;
if (bst[0][j] > Max)
Max = bst[0][j];
}
for (int i = 1; i < N; i++)
for (int j = 0; j < (int)P.size(); j++)
{
bst[i][j] = bst[i - 1][j] - (client[i].first - client[i - 1].first) * Cst;
if (client[i].second >= P[j])
bst[i][j] += P[j];
//intervalul [i, i]
int val = -Cst;
if (client[i].second >= P[j])
val += P[j];
if (val > bst[i][j])
bst[i][j] = val;
if (bst[i][j] > Max)
Max = bst[i][j];
}
printf("%d\n", Max);
return 0;
}