#C13037. Maximum Stock Profit

    ID: 42531 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

You are given a sequence of stock prices listed in chronological order. Your task is to determine the maximum profit that can be obtained by performing exactly one buy and one sell transaction. Note that the buy transaction must occur before the sell transaction.

Formally, given a list of prices \(P = [p_1, p_2, \dots, p_n]\), you need to find the maximum value of \(p_j - p_i\) such that \(1 \leq i < j \leq n\). If it is impossible to make a profit, output 0. In other words, compute:

[ \text{maxProfit} = \max_{1 \leq i < j \leq n} (p_j - p_i) \quad,\quad \text{if } p_j - p_i > 0; \quad \text{otherwise } 0. ]

The solution should read input from stdin and print the result to stdout.

inputFormat

The input is provided via standard input (stdin) with the following format:

  • The first line contains a single integer \(n\) representing the number of stock prices.
  • The second line contains \(n\) space-separated integers representing the stock prices in chronological order.

If \(n = 0\), it implies that the list is empty and the output should be 0.

outputFormat

Output a single integer to standard output (stdout) which indicates the maximum profit that can be made from one transaction. If no profit is possible, output 0.

## sample
6
7 1 5 3 6 4
5