// PA2026, @mjm, r2c-dos
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
inline int nextInt() { int n; scanf("%d", &n); return n; }
int solve() {
int n = nextInt();
int k = nextInt();
vector<int> org(n);
vector<int> best(n);
vector<int> done(n, 0);
priority_queue<pair<int, int>> q;
for (int i = 0; i < n; ++i) {
org[i] = best[i] = nextInt();
q.push({ best[i], i });
}
while (!q.empty()) {
int i = q.top().second;
q.pop();
if (done[i] > 0)
continue;
done[i] = 1;
int x = best[i];
int y = x - k;
if (i > 0 && y > best[i - 1]) {
best[i - 1] = y;
q.push({ y, i - 1 });
}
if (i + 1 < n && y > best[i + 1]) {
best[i + 1] = y;
q.push({ y, i + 1 });
}
}
int res = 0;
for (int i = 0; i < n; ++i) {
//printf("%d: %d => %d\n", i, org[i], best[i]);
res += best[i] - org[i];
}
return res;
}
int main() {
int res = solve();
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | // PA2026, @mjm, r2c-dos #include <cstdio> #include <vector> #include <queue> using namespace std; inline int nextInt() { int n; scanf("%d", &n); return n; } int solve() { int n = nextInt(); int k = nextInt(); vector<int> org(n); vector<int> best(n); vector<int> done(n, 0); priority_queue<pair<int, int>> q; for (int i = 0; i < n; ++i) { org[i] = best[i] = nextInt(); q.push({ best[i], i }); } while (!q.empty()) { int i = q.top().second; q.pop(); if (done[i] > 0) continue; done[i] = 1; int x = best[i]; int y = x - k; if (i > 0 && y > best[i - 1]) { best[i - 1] = y; q.push({ y, i - 1 }); } if (i + 1 < n && y > best[i + 1]) { best[i + 1] = y; q.push({ y, i + 1 }); } } int res = 0; for (int i = 0; i < n; ++i) { //printf("%d: %d => %d\n", i, org[i], best[i]); res += best[i] - org[i]; } return res; } int main() { int res = solve(); printf("%d\n", res); return 0; } |
English