#K60237. Maximum Profit

    ID: 31042 Type: Default 1000ms 256MiB

Maximum Profit

Maximum Profit

You are given the price of a stock on n consecutive days. Your task is to compute the maximum profit that can be achieved by buying the stock on one day and selling it on a later day.

Formally, if the price on day i is denoted as pi, you need to maximize the profit defined by the formula \( p_j - p_i \) for \( j > i \). If no profit can be achieved, output 0.

Input/Output:

The first line of input contains an integer n which represents the number of days. The second line contains n space-separated integers where the ith integer represents the stock price on day i.

Output a single integer which is the maximum profit possible.

inputFormat

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

  • The first line contains a single integer \( n \) (\( 0 \leq n \leq 10^5 \)), which is the number of days.
  • The second line contains \( n \) space-separated integers, representing the stock prices for each day. If \( n = 0 \), the second line will be empty.

outputFormat

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

## sample
6
7 1 5 3 6 4
5