#C13899. Maximum Stock Profit

    ID: 43487 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

Given a list of stock prices over n days, your task is to calculate the maximum profit obtainable by buying on one day and selling on a subsequent day. Formally, if the stock prices are given by an array \(prices\), you need to find the maximum value of \(prices[j] - prices[i]\) for any \(i < j\). If no profit is possible, output 0.

Note: You are allowed to complete at most one transaction (i.e. buy and then sell once).

Example:

  • For input prices [7, 1, 5, 3, 6, 4], the maximum profit is \(6 - 1 = 5\).
  • For input prices [7, 6, 4, 3, 1], no profit is possible so the answer is 0.

inputFormat

The first line contains an integer \(n\), representing the number of days. The second line contains \(n\) space-separated integers, where the \(i\)-th integer represents the stock price on day \(i\).

outputFormat

Output a single integer which is the maximum profit obtainable. If no profit is achievable, output 0.

## sample
6
7 1 5 3 6 4
5

</p>