#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) {} #endif #define int ll #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; #define cm(a, b) a = max((a), (b)) signed main() { cin.tie(0)->sync_with_stdio(0); int N, C; cin >> N >> C; vec<ll> a(N); vec<ll> w(N); rep (n, 0, N) { cin >> a[n] >> w[n]; --w[n]; } int i = 0; ll K = 500001; vec<ll> dp(K); multiset<ll> vs; while (i < N) { vec<pii> changes; int j; for (j = i; j < N && a[i] == a[j]; j++) { debug(j); ll v = dp[w[j]]; cm(v, a[j] + dp[w[j]]); if (vs.size()) { auto it = vs.rbegin(); bool b = true; if (*it == dp[w[j]]) { it++; if (it == vs.rend()) b = false; } if (b) cm(v, a[j] - C + *it); } changes.push_back({w[j], v}); } for (auto [a, b] : changes) { debug(a, b); auto it = vs.find(dp[a]); if (it != vs.end()) vs.erase(it); dp[a] = b; vs.insert(b); } i = j; } cout << *vs.rbegin() << "\n"; 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 68 69 70 71 72 | #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) {} #endif #define int ll #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; #define cm(a, b) a = max((a), (b)) signed main() { cin.tie(0)->sync_with_stdio(0); int N, C; cin >> N >> C; vec<ll> a(N); vec<ll> w(N); rep (n, 0, N) { cin >> a[n] >> w[n]; --w[n]; } int i = 0; ll K = 500001; vec<ll> dp(K); multiset<ll> vs; while (i < N) { vec<pii> changes; int j; for (j = i; j < N && a[i] == a[j]; j++) { debug(j); ll v = dp[w[j]]; cm(v, a[j] + dp[w[j]]); if (vs.size()) { auto it = vs.rbegin(); bool b = true; if (*it == dp[w[j]]) { it++; if (it == vs.rend()) b = false; } if (b) cm(v, a[j] - C + *it); } changes.push_back({w[j], v}); } for (auto [a, b] : changes) { debug(a, b); auto it = vs.find(dp[a]); if (it != vs.end()) vs.erase(it); dp[a] = b; vs.insert(b); } i = j; } cout << *vs.rbegin() << "\n"; return 0; } |