525. Frog and Stone

0

Easy

What is the most efficient way for a frog to jump from stone to stone, incurring the least amount of cost, and reach the final stone in a sequence of N stones, each with a specific height?
There are N stones, numbered 1,2,…,N. For each i (1≤i≤N), the height of Stone i is h
i
.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
If the frog is currently on Stone i, jump to Stone i+1 or Stone i+2. Here, a cost of

**hi−hj ∣ is incurred, where j is the stone to land on. Find the minimum possible total cost incurred before the frog reaches Stone N.****Input Format**

**First Line:It consists of n i.e size of array**

Second Line:It consists of n space separated integers

Second Line:It consists of n space separated integers

**Output Format**

**Find the minimum possible total cost.**

**Example**

**Input**

**4 10 30 40 20**

**Output**

**30**

**Constraints**

**2≤N≤10^5 1≤h i ≤10^4**

**Loading...**

**View Submissions**

**Console**