#include <cstdio>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
int main() {
int n, c; scanf(" %d %d", &n, &c);
map<int, LL> prev;
set<pair<LL, int>, greater<> > prevR;
map<int, LL> curr;
int lastA = 0;
for (int i=0; i<n; ++i) {
int a, w; scanf(" %d %d", &a, &w);
if (a > lastA) {
lastA = a;
for (const auto& el: curr) {
auto it = prev.find(el.first);
if (it == prev.end()) {
prev.insert(el);
prevR.insert(make_pair(el.second, el.first));
} else {
prevR.erase(make_pair(it->second, it->first));
it->second = max(it->second, el.second);
prevR.insert(make_pair(it->second, it->first));
}
}
curr = map<int, LL>();
}
LL res = a;
// printf("Maybe %d\n", a);
if (prev.contains(w)) {
// printf("Maybe also %lld\n", prev[w] + a);
res = max(res, prev[w] + a);
}
if (!prevR.empty()) {
// printf("Maybe but %lld\n", prevR.begin()->first + a - c);
res = max(res, prevR.begin()->first + a - c);
}
// printf("After %d computed %d -> %lld\n", a, w, res);
auto it = curr.find(w);
if (it != curr.end()) {
it->second = max(it->second, res);
} else {
curr[w] = res;
}
}
LL res = 0;
for (const auto& el: prev) {
res = max(res, el.second);
}
for (const auto& el: curr) {
res = max(res, el.second);
}
printf("%lld\n", res);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <cstdio> #include <vector> #include <map> #include <set> using namespace std; typedef long long LL; int main() { int n, c; scanf(" %d %d", &n, &c); map<int, LL> prev; set<pair<LL, int>, greater<> > prevR; map<int, LL> curr; int lastA = 0; for (int i=0; i<n; ++i) { int a, w; scanf(" %d %d", &a, &w); if (a > lastA) { lastA = a; for (const auto& el: curr) { auto it = prev.find(el.first); if (it == prev.end()) { prev.insert(el); prevR.insert(make_pair(el.second, el.first)); } else { prevR.erase(make_pair(it->second, it->first)); it->second = max(it->second, el.second); prevR.insert(make_pair(it->second, it->first)); } } curr = map<int, LL>(); } LL res = a; // printf("Maybe %d\n", a); if (prev.contains(w)) { // printf("Maybe also %lld\n", prev[w] + a); res = max(res, prev[w] + a); } if (!prevR.empty()) { // printf("Maybe but %lld\n", prevR.begin()->first + a - c); res = max(res, prevR.begin()->first + a - c); } // printf("After %d computed %d -> %lld\n", a, w, res); auto it = curr.find(w); if (it != curr.end()) { it->second = max(it->second, res); } else { curr[w] = res; } } LL res = 0; for (const auto& el: prev) { res = max(res, el.second); } for (const auto& el: curr) { res = max(res, el.second); } printf("%lld\n", res); return 0; } |
English