#K39147. Maximum Stock Profit

    ID: 26356 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

You are given a sequence of stock prices, where the i-th price corresponds to the price of a stock on day (i). Your task is to determine the maximum profit achievable by buying on one day and selling on a later day. If no profit can be made, return 0.

Formally, if the prices are given as (p_1, p_2, \dots, p_n), you need to calculate [ \max_{1 \leq i < j \leq n} (p_j - p_i), ] with the understanding that if this maximum difference is negative, the answer is 0.

inputFormat

The input is given via standard input. The first line contains a single integer (n) representing the number of days (i.e., the number of stock prices). If (n > 0), the second line contains (n) space-separated integers denoting the stock prices for each day.

outputFormat

Output via standard output a single integer representing the maximum profit possible. If no profit can be achieved, output 0.## sample

6
7 1 5 3 6 4
5