#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, k;
cin >> n >> k;
vector<ll> h(n), s(n);
for(ll i = 0; i < n; ++i){
cin >> h[i];
s[i] = h[i];
}
ll wyn = 0;
vector<bool> on(n, 1);
for (ll i = 0; i < n; ++i){
ll p = -1;
for (int j = 0; j < n; ++j){
if (on[j]){
if (p == -1 or h[p] < h[j]) p = j;
}
}
ll a = -1, b = h[p], c = -1;
on[p] = 0;
if (p > 0){
a = h[p - 1];
}
if (p < n - 1){
c = h[p + 1];
}
if (b + k < max(a, c)){
wyn += max(a, c) - b - k;
b += max(a, c) - b - k;
h[p] = b;
}
if (a + k < b and a != -1){
wyn += b - k - a;
a += b - k - a;
h[p - 1] = a;
}
if (c + k < b and c != -1){
wyn += b - k - c;
c += b - k - c;
h[p + 1] = c;
}
}
cout << wyn << endl;
}
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 | #include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <string> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, k; cin >> n >> k; vector<ll> h(n), s(n); for(ll i = 0; i < n; ++i){ cin >> h[i]; s[i] = h[i]; } ll wyn = 0; vector<bool> on(n, 1); for (ll i = 0; i < n; ++i){ ll p = -1; for (int j = 0; j < n; ++j){ if (on[j]){ if (p == -1 or h[p] < h[j]) p = j; } } ll a = -1, b = h[p], c = -1; on[p] = 0; if (p > 0){ a = h[p - 1]; } if (p < n - 1){ c = h[p + 1]; } if (b + k < max(a, c)){ wyn += max(a, c) - b - k; b += max(a, c) - b - k; h[p] = b; } if (a + k < b and a != -1){ wyn += b - k - a; a += b - k - a; h[p - 1] = a; } if (c + k < b and c != -1){ wyn += b - k - c; c += b - k - c; h[p + 1] = c; } } cout << wyn << endl; } |
English