#K2616. Maximum Stock Profit

    ID: 24777 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

You are given a sequence of stock prices over a number of days. Your task is to determine the maximum profit that could be achieved by buying on one day and selling on a later day. If no profit can be made, output 0.

Formally, given an array \(P = [p_1, p_2, \dots, p_n]\), you need to compute \[ \max_{1 \leq i < j \leq n} (p_j - p_i), \] with the understanding that if \(p_j \leq p_i\) for all \(i, j\) with \(i < j\), the answer is 0.

inputFormat

The first line contains a single integer \(n\) denoting 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 representing the maximum profit obtained by buying and selling the stock exactly once. If no profit is possible, output \(0\).

## sample
6
7 1 5 3 6 4
5