-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDPC_B.cpp
More file actions
45 lines (39 loc) · 726 Bytes
/
EDPC_B.cpp
File metadata and controls
45 lines (39 loc) · 726 Bytes
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
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
long long h[100010];
long long dp[100010];
int main() {
int N, K;
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%lld", &h[i]);
}
for (int i = 0; i < N; i++) {
dp[i] = INF;
}
dp[0] = 0;
for (int i = 1; i < N; i++) {
for (int j = 1; j <= K; j++) {
if (i - j < 0) break;
chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
}
printf("%lld", dp[N - 1]);
}