#include <cstdio>
#define MAXN 1000
int a[MAXN];
bool done[MAXN] = {false};
int main () {
int n, k;
scanf("%d%d", &n, &k);
for (int i=0; i < n; i++)
scanf("%d", &a[i]);
int res=0;
for (int i=0; i < n; i++) {
int biggest_ind=-1;
int biggest_a;
for (int j=0; j < n; j++)
if (!done[j] && (biggest_ind == -1 || biggest_a < a[j])) {
biggest_ind = j;
biggest_a = a[j];
}
if (biggest_ind > 0 && biggest_a - a[biggest_ind-1] > k) {
int diff = biggest_a - a[biggest_ind-1] - k;
res += diff;
a[biggest_ind-1] += diff;
}
if (biggest_ind < n-1 && biggest_a - a[biggest_ind+1] > k) {
int diff = biggest_a - a[biggest_ind+1] - k;
res += diff;
a[biggest_ind+1] += diff;
}
done[biggest_ind] = true;
}
printf("%d\n", res);
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 | #include <cstdio> #define MAXN 1000 int a[MAXN]; bool done[MAXN] = {false}; int main () { int n, k; scanf("%d%d", &n, &k); for (int i=0; i < n; i++) scanf("%d", &a[i]); int res=0; for (int i=0; i < n; i++) { int biggest_ind=-1; int biggest_a; for (int j=0; j < n; j++) if (!done[j] && (biggest_ind == -1 || biggest_a < a[j])) { biggest_ind = j; biggest_a = a[j]; } if (biggest_ind > 0 && biggest_a - a[biggest_ind-1] > k) { int diff = biggest_a - a[biggest_ind-1] - k; res += diff; a[biggest_ind-1] += diff; } if (biggest_ind < n-1 && biggest_a - a[biggest_ind+1] > k) { int diff = biggest_a - a[biggest_ind+1] - k; res += diff; a[biggest_ind+1] += diff; } done[biggest_ind] = true; } printf("%d\n", res); return 0; } |
English