This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int MAX_N = 500;
const int MAX_H = 500;
int n, h;
int A[MAX_N];
int dp[MAX_N][MAX_H];
signed main(){
cin>>n>>h;
for(int i = 0; i<n; i++){
cin>>A[i];
}
for(int i = n; i>=0; i--){
for(int j = 0; j<MAX_H; j++){
if(i==n){
dp[i][j] = 0;
}
else{
dp[i][j] = INF;
for(int k = max(0LL,j-h); k<=min(MAX_H-1,j+h); k++){
dp[i][j] = min(dp[i][j], dp[i+1][k] + abs(k-A[i]));
}
}
}
}
int res= INF;
for(int i = 0; i<MAX_H; i++){
res= min(res, dp[0][i]);
}
cout<<res<<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... |