#C9448. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a sequence of stock prices for several days. Your task is to compute the maximum profit that can be achieved by buying on one day and selling on a later day. If no profit can be achieved, output 0.
More formally, given a list of stock prices \(P = [p_1, p_2, \dots, p_n]\), you need to find the maximum value of \(p_j - p_i\) for \(1 \leq i < j \leq n\). If \(p_j - p_i\) is never positive, the maximum profit is 0.
The input will consist of multiple test cases. Each test case starts with an integer \(N\) (the number of days), followed by a line containing \(N\) integers representing the stock prices. A test case with \(N = 0\) indicates the end of input and should not be processed.
inputFormat
The input is given via standard input (stdin) and consists of multiple test cases. Each test case is described as follows:
- An integer \(N\) representing the number of days (prices).
- A line containing \(N\) space-separated integers representing the stock prices.
The input terminates when a test case begins with \(N = 0\), which should not be processed.
outputFormat
For each test case, output a single line with an integer indicating the maximum profit achievable. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5
7 6 4 3 1
4
1 2 3 4
0
5
0
3
</p>