Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int n, k;
- int niza[100005];
- int dp[100005];
- int jump(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, jump(i + j) + abs(niza[i] - niza[i + j]));
- }
- }
- dp[i] = result;
- return result;
- }
- int main() {
- cin >> n >> k;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- dp[i] = -1;
- }
- cout << jump(0) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement