Max Rise
InterviewTime limit1sMemory limit512 MB
Given daily stock prices, find the maximum profit from one buy followed by one later sell, where selling on the same day gives zero.
Problem
Someone who can foresee the future knows exactly how the stock price of ANA Company will change over the next days. Based on this prediction, Jeonggyun wants to buy one share at a suitable time and sell it at a suitable time to earn the greatest possible profit.
Let the stock prices of ANA Company over the next days be . If Jeonggyun buys on day and sells on day , he earns . Since he has plenty of funds, he can buy no matter how high the price is, and if the situation does not work out, he can sell right after buying.
Given the prices of ANA Company over the next days, what is the maximum profit Jeonggyun can earn by buying one share at a suitable time and selling it at a suitable time?
Input
The first line contains an integer ().
The second line contains integers . Each () is the stock price of ANA Company on day .
Output
Print the maximum profit Jeonggyun can earn by buying one share at a suitable time and selling it at a suitable time.