#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) <<
'\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
const int MAXN = 5e5;
const ll INF = 1e18;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
ll n, c; cin >> n >> c;
vector<ll> a(n);
vector<int> w(n);
rep(i, n)cin >> a[i] >> w[i];
reverse(all(a));
reverse(all(w));
ll res = -INF;
vector<ll> v(MAXN +1, -INF);
ll best = -INF;
vector<ll> dp(n, -INF);
int j = 0;
dp[0] = a[0];
res = a[0];
fwd(i, 1, n){
while(a[j] > a[i]){
best = max(best, dp[j]);
v[w[j]] = max(v[w[j]], dp[j]);
j++;
}
dp[i] = max(best - c, v[w[i]]) + a[i];
dp[i] = max(dp[i], a[i]);
res = max(res, dp[i]);
}
deb(dp);
cout << res << "\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 | #include <bits/stdc++.h> #include <iomanip> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; } #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define deb(...) 0 #endif const int MAXN = 5e5; const ll INF = 1e18; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); ll n, c; cin >> n >> c; vector<ll> a(n); vector<int> w(n); rep(i, n)cin >> a[i] >> w[i]; reverse(all(a)); reverse(all(w)); ll res = -INF; vector<ll> v(MAXN +1, -INF); ll best = -INF; vector<ll> dp(n, -INF); int j = 0; dp[0] = a[0]; res = a[0]; fwd(i, 1, n){ while(a[j] > a[i]){ best = max(best, dp[j]); v[w[j]] = max(v[w[j]], dp[j]); j++; } dp[i] = max(best - c, v[w[i]]) + a[i]; dp[i] = max(dp[i], a[i]); res = max(res, dp[i]); } deb(dp); cout << res << "\n"; return 0; } |
English