#K71657. Maximum Stock Profit

    ID: 33580 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

You are given a sequence of daily stock prices. Your task is to calculate the maximum profit that can be achieved by buying on one day and selling on a later day. If no profit can be made, the answer should be 0.

More formally, if the stock prices for the days are given as 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), \] returning 0 if \(p_j - p_i \le 0\) for all \(i < j\).

inputFormat

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

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

outputFormat

Output a single integer to standard output (stdout) representing the maximum possible profit. If no profit is possible, output 0.

## sample
6
7 1 5 3 6 4
5

</p>