#include<bits/stdc++.h>
using namespace std;
const int MAX = 500005;
int n, k;
vector <int> V;
int T[MAX];
long long wyn = 0;
bool f(){
int maks=-1;
int id=-1;
for (int i=1; i<=n; i++){
if (!T[i] && maks < V[i]){
id = i;
maks = V[i];
}
}
if (id == -1) return 0;
if (id > 1){
if (V[id-1] < maks-k){
wyn += maks-k - (V[id-1]);
V[id-1] = maks-k;
}
}
if (id < n){
if (V[id+1] < maks-k){
wyn += maks-k - (V[id+1]);
V[id+1] = maks-k;
}
}
T[id] = 1;
return 1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
priority_queue <pair<int, int>> Q;
V.push_back(0);
int x;
int r=1;
for (int i=1; i<=n; i++){
cin >> x;
V.push_back(x);
Q.push({x, i});
}
while (true){
bool c = f();
if (c == 0){
cout << wyn << "\n";
return 0;
}
}
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 60 61 62 63 64 65 66 67 68 69 70 | #include<bits/stdc++.h> using namespace std; const int MAX = 500005; int n, k; vector <int> V; int T[MAX]; long long wyn = 0; bool f(){ int maks=-1; int id=-1; for (int i=1; i<=n; i++){ if (!T[i] && maks < V[i]){ id = i; maks = V[i]; } } if (id == -1) return 0; if (id > 1){ if (V[id-1] < maks-k){ wyn += maks-k - (V[id-1]); V[id-1] = maks-k; } } if (id < n){ if (V[id+1] < maks-k){ wyn += maks-k - (V[id+1]); V[id+1] = maks-k; } } T[id] = 1; return 1; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; priority_queue <pair<int, int>> Q; V.push_back(0); int x; int r=1; for (int i=1; i<=n; i++){ cin >> x; V.push_back(x); Q.push({x, i}); } while (true){ bool c = f(); if (c == 0){ cout << wyn << "\n"; return 0; } } return 0; } |
English