#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll dp[500001];
bool b[500001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, c;
cin >> n >> c;
int h = -1;
vector<int> cur;
ll maxx = 0;
for (int i = 0; i < n; i++) {
int a, col; cin >> a >> col;
if (a == h) {
if (!b[col]) {
b[col] = true;
cur.push_back(col);
dp[col] = max(dp[col]+a, maxx+a-c);
}
}
else {
while(!cur.empty()) {
b[cur.back()] = false;
maxx = max(maxx, dp[cur.back()]);
cur.pop_back();
}
h = a;
b[col] = true;
cur.push_back(col);
dp[col] = max(dp[col]+a, maxx+a-c);
}
}
while(!cur.empty()) {
b[cur.back()] = false;
maxx = max(maxx, dp[cur.back()]);
cur.pop_back();
}
cout << maxx << '\n';
}
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 | #include <bits/stdc++.h> typedef long long ll; using namespace std; ll dp[500001]; bool b[500001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, c; cin >> n >> c; int h = -1; vector<int> cur; ll maxx = 0; for (int i = 0; i < n; i++) { int a, col; cin >> a >> col; if (a == h) { if (!b[col]) { b[col] = true; cur.push_back(col); dp[col] = max(dp[col]+a, maxx+a-c); } } else { while(!cur.empty()) { b[cur.back()] = false; maxx = max(maxx, dp[cur.back()]); cur.pop_back(); } h = a; b[col] = true; cur.push_back(col); dp[col] = max(dp[col]+a, maxx+a-c); } } while(!cur.empty()) { b[cur.back()] = false; maxx = max(maxx, dp[cur.back()]); cur.pop_back(); } cout << maxx << '\n'; } |
English