Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cmath>
- #include <math.h>
- #include <vector>
- #include <queue>
- using namespace std;
- int n, k;
- int arr[100005];
- int dp[100005];
- int frog(int i) {
- if(i >= n - 1) {
- return 0;
- }
- if(dp[i] != -1) {
- return dp[i];
- }
- int result = 2e9;
- for(int j = 1; j <= k; j++) {
- if(i + j < n) {
- result = min(result, frog(i + j) + abs(arr[i] - arr[i + j]));
- }
- }
- return dp[i] = result;
- }
- int main()
- {
- cin >> n >> k;
- for(int i = 0; i < n; i++) {
- cin >> arr[i];
- }
- memset(dp, -1, sizeof dp);
- cout << frog(0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement