Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(true);
- int n,k;
- cin>>n>>k;
- int x[n];
- int dp[n + 1];
- for(int i = 1; i <= n; i++) {
- dp[i] = 2e9;
- }
- dp[0] = 0;
- for(int i=0;i<n;i++)cin>>x[i];
- for(int i=1;i<=k && i<n;i++)
- {
- dp[i]=abs(x[0]-x[i]);
- }
- for(int i=k;i<n;i++)
- {
- for(int j=1;j<=k;j++)
- {
- dp[i]=min(dp[i], dp[i-j]+abs(x[i]-x[i-j]));
- }
- }
- cout<<dp[n-1];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement