-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatcoder_dp_B.cpp
More file actions
executable file
·54 lines (47 loc) · 1.16 KB
/
atcoder_dp_B.cpp
File metadata and controls
executable file
·54 lines (47 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll ,ll>>
#define vvi vector<vector<ll>>
#define map map<ll ,ll>
#define umap unordered_map<ll , ll>
#define endl "\n"
#define set set<ll>
#define setC set<char>
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define fastcout ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define testcases int tt = 1 ; cin >> tt ; for(int i = 1 ; i <= tt ; ++i)
#define codejam cout << "Case #"<<i <<": ";
#define FOR(i,a,n) for(int i = a ; i <= n ; ++i)
#define FORback(i,n,a) for(int i = n ; i >= a ; --i)
typedef long long int ll;
void solv(){
ll n , k;
cin >> n >> k;
vi arr(n+1 );
FOR(i , 1 , n){
cin >> arr[i];
}
vll dp(n+1 , INT_MAX);
dp[1] = 0;
FOR(i , 2 , k+1){
dp[i] = abs(arr[i] - arr[1]);
}
FOR(i , k+2 , n){
FORback(j , i-1 , i-k){
dp[i] = min(abs(arr[i] - arr[j])+dp[j] , dp[i]);
}
}
cout << dp[n] << endl;
// FOR(i , 1 , n){
// cout << dp[i] << " ";
// }
}
int main(){
fast ;
// testcases{
// codejam ;
solv();
// }
}