Maximum profit earned by buying and selling shares

EASY

Given a list containing future prediction of share prices, find maximum profit that can be earned by buying and selling shares any number of times with constraint that a new transaction can only start after previous transaction is complete.

For example,
Stock Prices: 1 3 2 1 7

Buy on day 1 and sell on day 2
Buy on day 4 and sell on day 5

Total profit earned is 7

Example #1

Input

1 3 2 1 7

Output

8

Solution