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;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1e9 + 7;
ll dp[5005][10005];
void solve(){
ll n,h; cin>>n>>h;
ll a[n+1];
ll mx=0;
forp(i,1,n){
cin>>a[i];
mx=max(mx,a[i]);
}
forp(i,1,n){
forp(j,0,mx){
dp[i][j]=INT_MAX;
}
}
forp(i,1,n){
forp(j,0,mx){
forp(k,j,min(j+h,mx)){
dp[i][j] = min(dp[i][j], dp[i-1][k]+abs(a[i]-j));
}
forn(k,j,max(0ll,j-h)){
dp[i][j] = min(dp[i][j], dp[i-1][k]+abs(a[i]-j));
}
}
}
ll mn=INT_MAX;
forp(i,0,mx){
mn=min(mn,dp[n][i]);
}
cout<<mn<<newl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1; //cin>>t;
while(t--)solve();
}
# | 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... |