#C8251. Maximum Stock Profit

    ID: 52213 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

Given the daily prices of a stock, determine the maximum profit that can be achieved by making at most one transaction (i.e., buy once and sell once). If no profit can be gained, return 0. The profit is computed using the formula:

\(\text{profit} = p_j - p_i\) for some \(i < j\)

where \(p_i\) and \(p_j\) are the stock prices on day \(i\) and day \(j\) respectively.

inputFormat

The input is read from stdin and consists of two lines:

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

outputFormat

The output is written to stdout and should be a single integer representing the maximum profit that can be achieved. If no transaction is profitable, output 0.

## sample
6
7 1 5 3 6 4
5