#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const int N = 5e5 + 2;
ll DP[N];
vector<pii> blocks;
queue<pii> buff;
void Print(int n){
for(int i = 0 ; i < n ; i++) cerr << DP[i] << " ";
cerr << '\n';
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
ll n, c, a, w, best = 0;
cin >> n >> c;
for(int i = 0 ; i < n ; i++){
cin >> a >> w;
blocks.push_back({a, w});
}
blocks.push_back({-1, -1});
for(int i = n - 1 ; i >= 0 ; i--){
if(blocks[i].first != blocks[i + 1].first){
while(!buff.empty()){
DP[buff.front().second] = buff.front().first;
best = max(buff.front().first, best);
buff.pop();
}
}
a = blocks[i].first;
w = blocks[i].second;
buff.push({max(DP[w], best - c) + a, w});
//Print(n);
}
while(!buff.empty()){
DP[buff.front().second] = buff.front().first;
best = max(buff.front().first, best);
buff.pop();
}
cout << best << '\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 45 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; const int N = 5e5 + 2; ll DP[N]; vector<pii> blocks; queue<pii> buff; void Print(int n){ for(int i = 0 ; i < n ; i++) cerr << DP[i] << " "; cerr << '\n'; } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); ll n, c, a, w, best = 0; cin >> n >> c; for(int i = 0 ; i < n ; i++){ cin >> a >> w; blocks.push_back({a, w}); } blocks.push_back({-1, -1}); for(int i = n - 1 ; i >= 0 ; i--){ if(blocks[i].first != blocks[i + 1].first){ while(!buff.empty()){ DP[buff.front().second] = buff.front().first; best = max(buff.front().first, best); buff.pop(); } } a = blocks[i].first; w = blocks[i].second; buff.push({max(DP[w], best - c) + a, w}); //Print(n); } while(!buff.empty()){ DP[buff.front().second] = buff.front().first; best = max(buff.front().first, best); buff.pop(); } cout << best << '\n'; } |
English