#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;
#define LOCAL false
const int LIM = 5e5 + 7;
int n, c;
pair<int, int> vals[LIM];
ll best1=0;
ll best2=0;
ll curh=0;
ll best1c[LIM];
ll best2c[LIM];
ll curhc[LIM];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
if (LOCAL) {
ignore=freopen("io/in", "r", stdin);
ignore=freopen("io/out", "w", stdout);
}
cin >> n >> c;
rep1(i, n) cin >> vals[i].first >> vals[i].second;
reverse(vals+1, vals+1+n);
ll out = 0;
rep1(i, n) {
auto [h, col] = vals[i];
ll ans = -1;
if (h != curh) ans = max(ans, h+best1-c);
else ans = max(ans, h+best2-c);
if (h != curhc[col]) ans = max(ans, h+best1c[col]);
else ans = max(ans, h+best2c[col]);
if (ans > best1) {
if (h != curh) {
best2 = best1;
best1 = ans;
curh = h;
} else best1 = ans;
}
if (ans > best1c[col]) {
if (h != curhc[col]) {
best2c[col] = best1c[col];
best1c[col] = ans;
curhc[col] = h;
} else best1c[col] = ans;
}
out = max(out, ans);
}
cout << out << "\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 | #include <bits/stdc++.h> using namespace std; #define rep(a, b) for (int a = 0; a < (b); a++) #define rep1(a, b) for (int a = 1; a <= (b); a++) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; #define LOCAL false const int LIM = 5e5 + 7; int n, c; pair<int, int> vals[LIM]; ll best1=0; ll best2=0; ll curh=0; ll best1c[LIM]; ll best2c[LIM]; ll curhc[LIM]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (LOCAL) { ignore=freopen("io/in", "r", stdin); ignore=freopen("io/out", "w", stdout); } cin >> n >> c; rep1(i, n) cin >> vals[i].first >> vals[i].second; reverse(vals+1, vals+1+n); ll out = 0; rep1(i, n) { auto [h, col] = vals[i]; ll ans = -1; if (h != curh) ans = max(ans, h+best1-c); else ans = max(ans, h+best2-c); if (h != curhc[col]) ans = max(ans, h+best1c[col]); else ans = max(ans, h+best2c[col]); if (ans > best1) { if (h != curh) { best2 = best1; best1 = ans; curh = h; } else best1 = ans; } if (ans > best1c[col]) { if (h != curhc[col]) { best2c[col] = best1c[col]; best1c[col] = ans; curhc[col] = h; } else best1c[col] = ans; } out = max(out, ans); } cout << out << "\n"; return 0; } |
English