John is a clever salesman at Codeland and he fell into a debt stake and for this reason, he deciced to sell out secondhand cars but he needs your help to get highest profit. You are given an integer array that contains numbers which represent the price of buying and selling a car in a day. Your task is to help John to get highest profit.

Given the following prices array: `[5, 10, 15]`

You can buy a car at first day and second day, and you can sell them at third day. After these operations, you will get `15 - 5 = 10`

profit for first day and `15 - 10 = 5`

profit for the second day and in total make `10 + 5 = 15`

profit.

Given the following prices array: `[2, 4, 1, 3]`

You can buy a car at first day and sell it at second day (the profit is `4 - 2 = 2`

) and you can buy a car at the third day and sell it on the fourth day (the profit is `3 - 1 = 2`

). Total profit is `2 + 2 = 4`

Given the following prices array: `[5, 4, 3, 2, 1]`

In this case, you cannot get any profit so the answer is `0`

.

Contributed by Murat Sütunç

You are not signed in. You can only run/submit code after signing in. Create your free Codela account now!