Answered step by step
Verified Expert Solution
Question
1 Approved Answer
There are n hills of some height. On first hill there lives two friends and on nth hill there lives a monster. One day monster
There are n hills of some height. On first hill there lives two friends and on nth hill there lives a monster. One day monster took one of the two friends to his home (nth hill). On knowing this the other friend went to rescue his friend. To reach the nth hill the friend has to follow this process: if he is currently on hill i, he can jump to one of the following hills i+1,i+2,....i+k. Here, a cost of |h-h;| is incurred, where j is the hill to land on and h; and h; are heights of ith and jth hill respectively). You are asked to print the minimum possible total cost incurred by the time the friend reaches nth hill. Input: First line contains n and k. Second line contains n integers, the height of hills. Output: Print the minimum time required. Constraints All values in input are integers. 2sNs1045 1 sks100 1sh s10000 SAMPLE INPUT SAMPLE OUTPUT 30 5 3 10 30 40 50 20 Explanation If we follow the path 1 + 2 + 5, the total cost incurred would be 10-30+30-201=30
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started