#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[2][MAXN+5];
k = min(k,(ll)5000);
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[0][j]);
}
for(int j = 0 ; j <= MAXN ; j++){
if( j + k <= MAXN ) s.insert(dp[0][j+k]);
if( j - k > 0 ) s.erase(s.lower_bound(dp[0][j-k-1]));
dp[1][j] = abs(a[i]-j) + *s.begin();
//cout<<i<<" "<<j<<" : "<<dp[i][j]<<"\n";
}
for(int j = 0 ; j <= MAXN ; j++){
dp[0][j] = dp[1][j];
}
}
ans = longlonginf;
for(int i = 0 ; i <= MAXN ; i++){
ans = min(ans,dp[0][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 |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2059 ms |
1364 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
125 ms |
340 KB |
Output is correct |
14 |
Correct |
80 ms |
372 KB |
Output is correct |
15 |
Correct |
150 ms |
612 KB |
Output is correct |
16 |
Correct |
184 ms |
392 KB |
Output is correct |
17 |
Correct |
172 ms |
412 KB |
Output is correct |
18 |
Correct |
145 ms |
396 KB |
Output is correct |
19 |
Correct |
86 ms |
368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
125 ms |
340 KB |
Output is correct |
14 |
Correct |
80 ms |
372 KB |
Output is correct |
15 |
Correct |
150 ms |
612 KB |
Output is correct |
16 |
Correct |
184 ms |
392 KB |
Output is correct |
17 |
Correct |
172 ms |
412 KB |
Output is correct |
18 |
Correct |
145 ms |
396 KB |
Output is correct |
19 |
Correct |
86 ms |
368 KB |
Output is correct |
20 |
Correct |
157 ms |
612 KB |
Output is correct |
21 |
Correct |
230 ms |
468 KB |
Output is correct |
22 |
Correct |
64 ms |
340 KB |
Output is correct |
23 |
Correct |
109 ms |
596 KB |
Output is correct |
24 |
Correct |
190 ms |
536 KB |
Output is correct |
25 |
Correct |
136 ms |
596 KB |
Output is correct |
26 |
Correct |
202 ms |
420 KB |
Output is correct |
27 |
Correct |
85 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
125 ms |
340 KB |
Output is correct |
14 |
Correct |
80 ms |
372 KB |
Output is correct |
15 |
Correct |
150 ms |
612 KB |
Output is correct |
16 |
Correct |
184 ms |
392 KB |
Output is correct |
17 |
Correct |
172 ms |
412 KB |
Output is correct |
18 |
Correct |
145 ms |
396 KB |
Output is correct |
19 |
Correct |
86 ms |
368 KB |
Output is correct |
20 |
Correct |
157 ms |
612 KB |
Output is correct |
21 |
Correct |
230 ms |
468 KB |
Output is correct |
22 |
Correct |
64 ms |
340 KB |
Output is correct |
23 |
Correct |
109 ms |
596 KB |
Output is correct |
24 |
Correct |
190 ms |
536 KB |
Output is correct |
25 |
Correct |
136 ms |
596 KB |
Output is correct |
26 |
Correct |
202 ms |
420 KB |
Output is correct |
27 |
Correct |
85 ms |
340 KB |
Output is correct |
28 |
Correct |
900 ms |
412 KB |
Output is correct |
29 |
Correct |
686 ms |
408 KB |
Output is correct |
30 |
Correct |
1994 ms |
600 KB |
Output is correct |
31 |
Correct |
1522 ms |
624 KB |
Output is correct |
32 |
Correct |
1550 ms |
436 KB |
Output is correct |
33 |
Execution timed out |
2029 ms |
516 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |