#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=5e5;
ll val[MAXN+5];
int typ[MAXN+5];
ll od[MAXN+5];
void solve()
{
int n;
ll c;
cin >> n >> c;
for(int i=0;i<n;i++){
cin >> val[i] >> typ[i];
}
ll best=0;
vector<pair<ll,int>> to_update;
for(int i=n-1;i>=0;i--){
if(val[i]!=val[i+1]){
for(pair<ll,int> pr : to_update){
best=max(best,pr.first);
od[pr.second]=max(od[pr.second], pr.first);
}
to_update.clear();
}
ll res;
res=max(best-c+val[i], od[typ[i]]+val[i]);
//cout << res << " ";
to_update.push_back({res,typ[i]});
}
for(pair<ll,int> pr : to_update){
best=max(best,pr.first);
}
cout << best;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=5e5; ll val[MAXN+5]; int typ[MAXN+5]; ll od[MAXN+5]; void solve() { int n; ll c; cin >> n >> c; for(int i=0;i<n;i++){ cin >> val[i] >> typ[i]; } ll best=0; vector<pair<ll,int>> to_update; for(int i=n-1;i>=0;i--){ if(val[i]!=val[i+1]){ for(pair<ll,int> pr : to_update){ best=max(best,pr.first); od[pr.second]=max(od[pr.second], pr.first); } to_update.clear(); } ll res; res=max(best-c+val[i], od[typ[i]]+val[i]); //cout << res << " "; to_update.push_back({res,typ[i]}); } for(pair<ll,int> pr : to_update){ best=max(best,pr.first); } cout << best; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |
English