#include <bits/stdc++.h>
#define inf INT_MAX
#define longlonginf LONG_LONG_MAX
#define mod 998244353
#define MAXN 5000
#define pll pair<ll,ll>
#define ll long long
#define deb(x) cerr<<"[ "<<#x<<" = "<<x<<" ]";
#define yes() cout<<"YES\n";
#define no() cout<<"NO\n";
using namespace std;
ll n,m,k,q,x;
ll h;
ll ans = 0;
string subtask;
ll sub3 = 0;
void solve(){
cin>>n>>k;
ll dp[MAXN+5][MAXN+5];
ll a[n];
for(int i = 0 ; i < n ; i++){
cin>>a[i];
}
for(int i = 0 ; i <= MAXN ; i++){
dp[0][i] = abs(a[0]-i);
}
for(int i = 1 ; i < n ; i++){
multiset<ll> s;
for(int j = 0 ; j < k ; j++){
s.insert(dp[i-1][j]);
}
for(int j = 0 ; j <= MAXN ; j++){
if( j + k <= MAXN ) s.insert(dp[i-1][j+k]);
if( j - k > 0 ) s.erase(s.lower_bound(dp[i-1][j-k-1]));
dp[i][j] = abs(a[i]-j) + *s.begin();
//cout<<i<<" "<<j<<" : "<<dp[i][j]<<"\n";
}
}
ans = longlonginf;
for(int i = 0 ; i <= MAXN ; i++){
ans = min(ans,dp[n-1][i]);
}
cout<<ans<<"\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
//cin>>T;
for(int i = 0 ; i < T ; i++){
//cout<<"Case #"<<i+1<<": ";
solve();
}
return 0;
}
/*
not i but x
logical operator
wrong example/proof
thoroughly
wrong variables
thinking it wrong
bruh just try some test case
capitals ;-;
wrong data structure lol
count memory usement
corner case
oversized array
orders
statements
size initializer
while con
map -> array
wrong digits??
swapped variables??
check if theres any variabled
that got declared twice
find some pattern
name collision
constraints??!
mod !!
resets
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
196280 KB |
Output is correct |
2 |
Correct |
68 ms |
196344 KB |
Output is correct |
3 |
Correct |
62 ms |
196296 KB |
Output is correct |
4 |
Runtime error |
395 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
196340 KB |
Output is correct |
2 |
Correct |
71 ms |
196360 KB |
Output is correct |
3 |
Correct |
64 ms |
196360 KB |
Output is correct |
4 |
Correct |
65 ms |
196360 KB |
Output is correct |
5 |
Correct |
62 ms |
196320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
196288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1063 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
196280 KB |
Output is correct |
2 |
Correct |
68 ms |
196344 KB |
Output is correct |
3 |
Correct |
62 ms |
196296 KB |
Output is correct |
4 |
Runtime error |
395 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
196280 KB |
Output is correct |
2 |
Correct |
68 ms |
196344 KB |
Output is correct |
3 |
Correct |
62 ms |
196296 KB |
Output is correct |
4 |
Runtime error |
395 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
196280 KB |
Output is correct |
2 |
Correct |
68 ms |
196344 KB |
Output is correct |
3 |
Correct |
62 ms |
196296 KB |
Output is correct |
4 |
Runtime error |
395 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
196280 KB |
Output is correct |
2 |
Correct |
68 ms |
196344 KB |
Output is correct |
3 |
Correct |
62 ms |
196296 KB |
Output is correct |
4 |
Runtime error |
395 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
196280 KB |
Output is correct |
2 |
Correct |
68 ms |
196344 KB |
Output is correct |
3 |
Correct |
62 ms |
196296 KB |
Output is correct |
4 |
Runtime error |
395 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |