#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
vector<int> tab(n+2);
tab[0] = 1e9+7;
tab[n+1] = 1e9+7;
priority_queue<pair<int,int>> pq;
for(int i = 1; i <= n; i++){
cin >> tab[i];
pq.push({tab[i], i});
}
int ans = 0;
while(!pq.empty()){
auto [x, i] = pq.top();
pq.pop();
if(x != tab[i])continue;
if(tab[i-1] < tab[i] - k){
ans += tab[i] - k - tab[i-1];
tab[i-1] = tab[i] - k;
pq.push({tab[i-1], i-1});
}
if(tab[i+1] < tab[i] - k){
ans += tab[i] - k - tab[i+1];
tab[i+1] = tab[i] - k;
pq.push({tab[i+1], i+1});
}
}
cout << ans << '\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 | #include <bits/stdc++.h> typedef long long ll; using namespace std; int main(){ cin.tie(0)->sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> tab(n+2); tab[0] = 1e9+7; tab[n+1] = 1e9+7; priority_queue<pair<int,int>> pq; for(int i = 1; i <= n; i++){ cin >> tab[i]; pq.push({tab[i], i}); } int ans = 0; while(!pq.empty()){ auto [x, i] = pq.top(); pq.pop(); if(x != tab[i])continue; if(tab[i-1] < tab[i] - k){ ans += tab[i] - k - tab[i-1]; tab[i-1] = tab[i] - k; pq.push({tab[i-1], i-1}); } if(tab[i+1] < tab[i] - k){ ans += tab[i] - k - tab[i+1]; tab[i+1] = tab[i] - k; pq.push({tab[i+1], i+1}); } } cout << ans << '\n'; return 0; } |
English