Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cmath>
- using namespace std;
- int niza[1000005];
- int dp[100005];
- int n;
- int k;
- int mini(int a,int b)
- {
- int m=0;
- if(a<b)
- {
- m=a;
- }else
- {
- m=b;
- }
- return m;
- }
- int f(int frog_at)
- {
- if(frog_at==(n-1))
- {
- return 0;
- }
- if(dp[frog_at] != -1) {
- return dp[frog_at];
- }
- int result=2e9;
- for(int i=1;i<=k;i++)
- {
- if(frog_at+i<n)
- {
- result=mini(result,f(frog_at+i)+ abs(niza[frog_at]-niza[frog_at+i]));
- }
- }
- return dp[frog_at] = result;
- }
- int main()
- {
- cin>>n>>k;
- for(int i=0;i<n;i++)
- {
- cin>>niza[i];
- dp[i] = -1;
- }
- cout<<f(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement