#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include <cassert>
using namespace std;
#define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++)
int a[1000];
int main() {
int n, k;
scanf("%d%d", &n, &k);
FOR(i, n) {
scanf("%d", &a[i]);
}
long long res = 0;
for (int i = 1; i<n; i++) {
int tgt = a[i-1] - k;
if (a[i] < tgt) {
res += tgt - a[i];
a[i] = tgt;
}
}
for (int i = n - 2; i >= 0; i--) {
int tgt = a[i+1] - k;
if (a[i] < tgt) {
res += tgt - a[i];
a[i] = tgt;
}
}
printf("%lld\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 38 39 40 | #include <algorithm> #include <iostream> #include <vector> #include <list> #include <cassert> using namespace std; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) int a[1000]; int main() { int n, k; scanf("%d%d", &n, &k); FOR(i, n) { scanf("%d", &a[i]); } long long res = 0; for (int i = 1; i<n; i++) { int tgt = a[i-1] - k; if (a[i] < tgt) { res += tgt - a[i]; a[i] = tgt; } } for (int i = n - 2; i >= 0; i--) { int tgt = a[i+1] - k; if (a[i] < tgt) { res += tgt - a[i]; a[i] = tgt; } } printf("%lld\n", res); return 0; } |
English