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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    long long k;
    if (!(cin >> n >> k)) return 0;
    vector<long long> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    vector<long long> L(n);
    L[0] = a[0];
    for (int i = 1; i < n; ++i)
        L[i] = max(a[i], L[i - 1] - k);
    vector<long long> R(n);
    R[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; --i)
        R[i] = max(a[i], R[i + 1] - k);
    long long total_trucks = 0;
    for (int i = 0; i < n; ++i) {
        long long target_height = max(L[i], R[i]);
        total_trucks += (target_height - a[i]);
    }
    cout << total_trucks << "\n";
    return 0;
}