#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
const int MOD = 1e9 + 7;
const int INF = 1e15;
const int N = 1e6;
int n, m, k, a, b, c, d, h, l, r, q, u, v, x, y;
void solve() {
cin >> n >> h;
vector<int> arr(n+1);
int maxh = 0;
for(int i = 1; i<=n; i++) {
cin >> arr[i];
maxh = max(maxh, arr[i]);
}
vector<vector<int>> dp(n+1, vector<int>(maxh+1));
for(int i = 1; i<=n; i++) {
for(int j = 0; j<=maxh; j++) {
r = j;
x = abs(arr[i]-j);
for(int k = i-1; k>=1; k--) {
if(abs(arr[k]-r) <= h) {
r = arr[k];
continue;
}
if(arr[k] < r-h) {
x += (r-h)-arr[k];
r -= h;
}
else {
x += arr[k]-(r+h);
r += h;
}
}
dp[i][j] = x;
// cout << dp[i][j] << " ";
}
// cout << "\n";
}
c = INF;
for(int j = 0; j<=maxh; j++) {
c = min(c, dp[n][j]);
}
cout << c;
}
int32_t main() {
ios::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int tc = 1;
// cin >> tc;
while(tc--) {
solve();
cout << "\n";
}
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... |