#K48687. Maximum Profit

    ID: 28476 Type: Default 1000ms 256MiB

Maximum Profit

Maximum Profit

You are given a list of prices representing the price of a stock (or book) on consecutive days. Your task is to compute the maximum profit that can be achieved with a single buy and a subsequent sell transaction. If no profit can be achieved, output 0.

Formally, given a sequence of prices \(p_1, p_2, \ldots, p_n\), you need to find the maximum value of \(p_j - p_i\) for any \(1 \leq i < j \leq n\). If this value is negative, output \(0\) instead.

inputFormat

The input is given via stdin in the following format:

N
p1 p2 p3 ... pN

Where:

  • N (an integer) represents the number of days.
  • p1, p2, ..., pN are the stock prices on each day.

outputFormat

Output the maximum profit achievable using a single buy-sell transaction to stdout. If no profit can be made, output 0.

## sample
6
10 7 5 8 11 9
6