#C2526. Maximum Stock Profit

    ID: 45852 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

You are given a series of stock prices for consecutive days. Your task is to determine the maximum profit achievable by buying on one day and selling on a later day. If no profit is possible, output 0.

More formally, given a sequence of prices \(P = [p_1, p_2, \dots, p_n]\), you need to find the maximum difference \(p_j - p_i\) for \(1 \leq i < j \leq n\). If no such positive difference exists, return 0.

Input/Output Requirements:

  • The input is read from standard input (stdin).
  • The output is printed to standard output (stdout).

inputFormat

The input consists of two lines:

The first line contains an integer (n) representing the number of days. The second line contains (n) space-separated integers, where each integer represents the stock price on that day.

outputFormat

Output a single integer which is the maximum profit obtainable. If no profit can be achieved, output 0.## sample

6
7 1 5 3 6 4
5