AlgoUniversity
  • Go Back
Discussion
Mountain Heights :

Author

Shikhar Mehrotra

Difficulty Level : Easy

Submissions : 281

Asked In : Walmart

Marks :10

: 2 | : 1

There are a series of $$$n$$$ connected mountains whose heights are represented in an array $$$arr[]$$$. A person is initially at zero height. The energy required for a person to climb up a mountain is difference between height of that mountain and person current height. No energy is needed to go down any mountain of any height (i.e lesser height than your current height).Calculate the total energy required by a person to climb all the mountains.

Input

The first line contains an $$$(1 \le n \le 10^{6})$$$ representing the size of array.

The second line contains $$$n$$$ space separated integers $$$(0 \le arr_i \le 10^{9}) $$$ where $$$(0\le i < n)$$$

Output

Print the single integer representing the total energy required by the person to climb all the mountains.

Example

Input
7
2 3 5 11 4 7 10
Output
17

Note

In first test case

size of array =7

$$$arr[]=[2,3,5,11,4,7,10]$$$

The person is initially at height=0.

To climb the 1st mountain of height =2 he required (2-0) = 2 units of energy.

To go to next hill of height=3 he requires (3-2) =1 unit of energy.

For 3rd and 4th hill, he requires (5-3) =2 units and (11-5)=6 units of energy resp.

Now to get to 5th hill, he does not require any energy as he is already at a height greater than the fifth height.

And for 6th and 7th hill, he requires (7-4)=3 units and (10-7)=3 units of energy resp.

Total energy = 2+1+2+6+0+3+3=17

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.