#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int N = 5e5 + 5; pair<int, int> Blocks[N]; ll Best[N]; ll TempBest[N]; ll Solve(int n, int c) { ll sol = 0, temp = 0; int last = 1; for(int i = 1; i <= n; i++) { auto [a, w] = Blocks[i]; if(a != Blocks[i-1].first) { for(int j = last; j < i; j++) Best[Blocks[j].second] = TempBest[Blocks[j].second]; last = i; sol = max(sol, temp); temp = 0; } ll x = Best[w] + a; ll y = sol + a - c; TempBest[w] = max(x, y); temp = max({temp, x, y}); } return max(sol, temp); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, c, a, w; cin >> n >> c; for(int i = 1; i <= n; i++) cin >> Blocks[i].first >> Blocks[i].second; cout << Solve(n, c) << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; const int N = 5e5 + 5; pair<int, int> Blocks[N]; ll Best[N]; ll TempBest[N]; ll Solve(int n, int c) { ll sol = 0, temp = 0; int last = 1; for(int i = 1; i <= n; i++) { auto [a, w] = Blocks[i]; if(a != Blocks[i-1].first) { for(int j = last; j < i; j++) Best[Blocks[j].second] = TempBest[Blocks[j].second]; last = i; sol = max(sol, temp); temp = 0; } ll x = Best[w] + a; ll y = sol + a - c; TempBest[w] = max(x, y); temp = max({temp, x, y}); } return max(sol, temp); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, c, a, w; cin >> n >> c; for(int i = 1; i <= n; i++) cin >> Blocks[i].first >> Blocks[i].second; cout << Solve(n, c) << "\n"; return 0; } |