#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
using namespace std;
#ifdef LOCAL
#include "C:\Users\Dell\Downloads\template\template\icpc-notebook\Utilities\debug.h"
#else
#define debug(...) 42
#endif
const int mn = 5e5 + 5, N = 5e3 + 5, mod = 1e9 + 7, inf = 2e18;
int n, h, a[mn], dp[N][N];
namespace sub12567{
bool check(){
int r = 0;
for(int i = 1; i <= n; i++){
r = max(r, a[i]);
}
return (r <= 5000 && n <= 5000);
}
void sol(){
fill(&dp[0][0], &dp[0][0] + N * N, inf);
for(int i = 0; i <= 5000; i++) dp[0][i] = 0;
for(int i = 1; i <= n; i++){
deque <int> dq;
for(int j = 0; j < min(5000ll, h); j ++){
while(!dq.empty() && dp[i - 1][dq.back()] > dp[i - 1][j]) dq.pop_back();
dq.push_back(j);
}
for(int j = 0; j <= 5000; j++){
while(!dq.empty() && dq.front() < max(0ll, j - h)) dq.pop_front();
while(j + h <= 5000 && !dq.empty() && dp[i - 1][dq.back()] > dp[i - 1][j + h]) dq.pop_back();
if(j + h <= 5000) dq.push_back(j + h);
dp[i][j] = dp[i - 1][dq.front()] + abs(a[i] - j);
// if(j <= 10){
// debug(i, j, dp[i][j], dp[i - 1][dq.front()], abs(a[i] - j));
// debug(dq);
// }
}
}
int res = inf;
for(int i = 0; i <= 5000; i ++) res = min(res, dp[n][i]);
cout << res << '\n';
}
};
namespace sub3{
bool check(){
return (h == 0);
}
void sol(){
sort(a + 1, a + n + 1);
int res = 0;
for(int i = 1; i <= n; i++){
res += abs(a[i] - a[(n + 1) >> 1]);
}
cout << res << '\n';
}
};
namespace sub4{
bool check(){
return (n <= 10 && h <= 2);
}
int res = inf;
void backtrack(int id, int cur, int val){
if(id == n){
res = min(res, val);
return;
}
for(int i = - h; i <= h; i++){
backtrack(id + 1, cur + i, val + abs(cur + i - a[id + 1]));
}
}
void sol(){
for(int id = 1; id <= n; id ++){
for(int i = - h; i <= h; i++){
backtrack(1, a[id] + i, abs(a[id] + i - a[1]));
}
}
cout << res << '\n';
}
};
void solve() {
cin >> n >> h;
for(int i = 1; i <= n; i++) cin >> a[i];
if(sub4::check()) sub4::sol();
else if(sub12567::check()) sub12567::sol();
else if(sub3::check()) sub3::sol();
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
#define task "Kawabata"
if (fopen(task".INP", "r")) {
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
// Don't wanna lose anymore T_T
// Never let me go - Kazuo Ishiguro
컴파일 시 표준 에러 (stderr) 메시지
safety.cpp: In function 'int main()':
safety.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |