#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vi vector<long long>
#define all(x) x.begin(), x.end()
#define endl "\n"
void solution();
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
void solution() {
ll n, h;
cin >> n >> h;
vi a(n);
for (ll &z : a) cin >> z;
ll mx = *max_element(all(a));
h = min(h, mx);
vi dp(mx+1);
for (int i = 0; i < n; i++) {
vi ndp(mx+1);
deque<ll> m;
int p = 0;
for (int j = 0; j <= mx; j++) {
while (m.size() > 0 && m.front() < j-h) m.pop_front();
while (p <= j+h && p <= mx) {
m.push_back(p);
p++;
while (m.size() > 0 && dp[m.front()] > dp[m.back()]) m.pop_front();
}
ndp[j] = abs(j-a[i]) + dp[m.front()];
}
dp = move(ndp);
}
cout << *min_element(all(dp)) << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |