#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1006;
int h[MAXN];
bool ok[MAXN];
int n, k;
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
priority_queue<pair<int, int>> q;
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> h[i];
q.push({h[i], i});
}
h[0] = h[1]; h[n+1] = h[n];
ok[0] = true; ok[n+1] = true;
int ans = 0;
while(!q.empty()){
auto [height, cur] = q.top();
q.pop();
if(ok[cur]) continue;//juz obsluzylismy
ok[cur] = true;
for(int i = -1; i <= 1; i += 2){//patrzymy na sasiadow
if(!ok[cur + i]){//jest nizszy/nierozpatrzony
if(height - h[cur + i] > k){//jest za niski
ans += height - k - h[cur + i];
h[cur + i] = height - k;
q.push({h[cur + i], cur + i});
}
}
}
}
cout << ans;
}
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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 1006; int h[MAXN]; bool ok[MAXN]; int n, k; int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); priority_queue<pair<int, int>> q; cin >> n >> k; for(int i = 1; i <= n; i++){ cin >> h[i]; q.push({h[i], i}); } h[0] = h[1]; h[n+1] = h[n]; ok[0] = true; ok[n+1] = true; int ans = 0; while(!q.empty()){ auto [height, cur] = q.top(); q.pop(); if(ok[cur]) continue;//juz obsluzylismy ok[cur] = true; for(int i = -1; i <= 1; i += 2){//patrzymy na sasiadow if(!ok[cur + i]){//jest nizszy/nierozpatrzony if(height - h[cur + i] > k){//jest za niski ans += height - k - h[cur + i]; h[cur + i] = height - k; q.push({h[cur + i], cur + i}); } } } } cout << ans; } |
English