#include <cstdio>
#ifdef LOCAL
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#else
#define dbg(...)
#endif
using namespace std;
int hs[1000];
int totalzwir = 0;
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &hs[i]);
if (i == 0) continue;
if (hs[i-1] - hs[i] > k) {
// Current one is too low and needs to be raised up to hs[i-1] - k.
totalzwir += hs[i-1] - hs[i] - k;
hs[i] = hs[i-1] - k;
}
int curidx = i;
while (curidx > 0 && hs[curidx] - hs[curidx-1] > k) {
// current one is too high, and the previous needs to be raised.
totalzwir += hs[curidx] - hs[curidx-1] - k;
hs[curidx-1] = hs[curidx] - k;
curidx--;
}
}
printf("%d\n", totalzwir);
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 | #include <cstdio> #ifdef LOCAL #define dbg(...) fprintf(stderr, __VA_ARGS__) #else #define dbg(...) #endif using namespace std; int hs[1000]; int totalzwir = 0; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", &hs[i]); if (i == 0) continue; if (hs[i-1] - hs[i] > k) { // Current one is too low and needs to be raised up to hs[i-1] - k. totalzwir += hs[i-1] - hs[i] - k; hs[i] = hs[i-1] - k; } int curidx = i; while (curidx > 0 && hs[curidx] - hs[curidx-1] > k) { // current one is too high, and the previous needs to be raised. totalzwir += hs[curidx] - hs[curidx-1] - k; hs[curidx-1] = hs[curidx] - k; curidx--; } } printf("%d\n", totalzwir); return 0; } |
English