Hide

Problem G
Stocks Mania

You’ve made some money by winning CodePSU!! Now you want to automatically invest this money in the stock market but sadly you don’t have a lot of financial knowledge or the time to follow the stock market and buy and sell at the correct times. You want quick short term purchases for an immediate return(on the very same day) on investment to make a quick dollar. Being the winner of CodePSU you know better than to do this mundane work manually and write an algorithm to do your biddings for you. You choose your favorite stock and decide to continuously buy and sell it throughout the day as the stock market rises and falls, but you only buy one stock at a time and monitor the market hourly.

Input

The first line of input is the number of hours your algorithm was running, collecting data points and active in the market, $n$ where ($1 \leq n \leq 24$) (upper limit of hours). The next line is a space separated string of $n$ integers of the prices it collected throughout the day. Buy and sell accordingly to make the maximum profit, all prices $p$ are real numbers $p$ ($1 \leq p \leq 1\, 000$), prices cannot be negative.

Output

If a profit is made simply print the profit.

If the market is bad and you weren’t able to make any money do not leave the answer in negative values, you must sell all stocks you buy on the same day.

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

Please log in to submit a solution to this problem

Log in