eolymp
bolt
Try our new interface for solving problems
Problems

Stock Maximize

Stock Maximize

Your algorithms have become so good at predicting the market that now you know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next number of days. Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with the optimum trading strategy? \InputFile The first line contains the number of predicted prices $n~(1 \le n \le 50000)$ for WOT. The next line contains $n$ integers $p_i~(1 \le p_i \le 10^5)$, each is a predicted stock price on the $i$-th day. \OutputFile Print the maximum profit that can be obtained in the market.
Time limit 1 second
Memory limit 128 MiB
Input example #1
4
1 4 2 3 
Output example #1
4
Input example #2
9
5 2 6 9 1 6 5 8 3
Output example #2
26