#K62077. Maximum Stock Trading Profit
Maximum Stock Trading Profit
Maximum Stock Trading Profit
You are given a list of stock prices where the ith element represents the stock price on day i. Your task is to determine the maximum profit that can be achieved by buying on one day and selling on a later day. If no profit is possible, return 0.
The formula for profit when buying at price \(p_b\) and selling at price \(p_s\) is:
[ \text{Profit} = p_s - p_b ]
You need to choose \(p_b\) and \(p_s\) such that \(p_b\) comes before \(p_s\) in the list and the profit is maximized.
Input/Output: The input is read from standard input and the output is written to standard output.
inputFormat
The first line contains an integer \(N\) representing the number of days. The second line contains \(N\) space-separated integers representing the stock prices on each day.
outputFormat
Output a single integer which is the maximum profit that can be achieved. If no profit is possible, print 0.
## sample6
7 1 5 3 6 4
5
</p>