#C3621. Maximize Stock Profit
Maximize Stock Profit
Maximize Stock Profit
You are given a list of stock prices, where the i-th element represents the price of a given stock on day i. 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, return 0.
Formally, if you choose to buy at price \(p_{buy}\) and sell at price \(p_{sell}\) with \(sell > buy\), the profit is \(p_{sell} - p_{buy}\). You need to maximize this value.
Input and output: The input is read from stdin
and the output is printed to stdout
.
inputFormat
The first line contains an integer (n), the number of stock prices. The second line contains (n) space-separated integers representing the stock prices.
outputFormat
Output a single integer representing the maximum profit that can be achieved. If no profit is possible, output 0.## sample
6
7 1 5 3 6 4
5