Pagini recente » Cod sursa (job #700331) | Cod sursa (job #107480) | Cod sursa (job #2177425) | Cod sursa (job #1435570) | Cod sursa (job #2404775)
#include <set>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("carnati.in");
ofstream out("carnati.out");
int n, c, maxp = 0;
pair<int, int> cust_array[2004], *cust = &cust_array[2];
int main() {
in>>n>>c;
cust[-2].first = -1000001;
cust[-1].first = -1000000;
cust[n + 1].first = 1000000;
cust[n + 2].first = 1000001;
for(int x = 0; x<n; x++) {
in>>cust[x].first>>cust[x].second;
}
sort(cust, cust + n);
for(int x = 0; x<n; x++) {
int l = x, r = x, gainl = 0, gainr = 0, maxl = 0, maxr = 0, len = 1;
while(cust[l - 1].first == cust[l].first) {
l--;
len += (cust[l].second >= cust[x].second);
}
while(cust[r].first == cust[r + 1].first) {
r++;
len += (cust[r].second >= cust[x].second);
}
for(int nl = l - 1, prev = l; nl>=0; nl--) {
gainl -= c*(cust[prev].first - cust[nl].first);
if(cust[nl].second >= cust[x].second)
gainl += cust[x].second;
maxl = max(maxl, gainl);
prev = nl;
}
for(int nr = r + 1, prev = r; nr<n; nr++) {
gainr -= c*(cust[nr].first - cust[prev].first);
if(cust[nr].second >= cust[x].second)
gainr += cust[x].second;
maxr = max(maxr, gainr);
prev = nr;
}
maxp = max(maxp, len*cust[x].second + maxl + maxr - c);
}
out<<maxp;
return 0;
}