#K64127. Maximum Stock Trading Profit

    ID: 31907 Type: Default 1000ms 256MiB

Maximum Stock Trading Profit

Maximum Stock Trading Profit

You are given a sequence of daily stock prices. Your task is to determine the maximum profit that can be achieved by buying on one day and selling on a later day. In other words, if the prices are given as an array \(P = [p_1, p_2, \dots, p_n]\), you need to compute:

\(\max_{1 \le i < j \le n} (p_j - p_i)\)

If no profit can be made, return 0.

Note: The buying day must come before the selling day.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains a single integer \(n\) (number of days).
  • The second line contains \(n\) space-separated integers representing the stock prices for each day.

outputFormat

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

## sample
6
7 1 5 3 6 4
5