Problems > Trade Every Rise

Trade Every Rise

Difficulty: 1300 3000 ms 128 MB

greedy

Same prices, but you may buy and sell as often as you like (hold at most one share). Print the maximum total profit.

Input: the first line has N; the second line has N prices.

Output: the best cumulative profit.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ pi ≤ 109

Sample Tests

Input 1
6
7 1 5 3 6 4
Output 1
7
Input 2
3
5 5 5
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.