#include <bits/stdc++.h>
using namespace std;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,h;
cin >> n >> h;
int arr[n];
int mxval = 0;
for(int &i : arr){
cin >> i;
mxval=max(mxval,i);
}
int dp[n][mxval+1];
for(int i = 0;i<=mxval;i++){
dp[0][i]=abs(i-arr[0]);
}
for(int i = 1;i<n;i++){
multiset<int>s;
for(int j = 0;j<min(h,mxval);j++){
s.insert(dp[i-1][j]);
}
for(int j = 0;j<=mxval;j++){
if(j+h<=mxval){
s.insert(dp[i-1][j+h]);
}
if(j-h-1>=0){
s.erase(s.find(dp[i-1][j-h-1]));
}
dp[i][j]=abs(arr[i]-j)+(*(s.begin()));
}
}
cout << *min_element(dp[n-1],dp[n-1]+mxval+1);
return 0;
}
# | 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... |