#C329. Maximum Stock Profit

    ID: 46700 Type: Default 1000ms 256MiB

Maximum Stock Profit

Maximum Stock Profit

You are given the number of days and the stock prices on each day. Your task is to determine the maximum profit that can be achieved by making a single buy and then a sell. Note that you must buy before you sell. If no profit can be achieved, the answer should be 0.

Formally, given a sequence of prices \( p_1, p_2, \ldots, p_N \), you need to compute \( \max_{1 \leq i < j \leq N} (p_j - p_i) \), with the understanding that if the maximum difference is negative, you should return 0.

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 representing the stock prices on each day.

outputFormat

Output a single integer which represents the maximum profit that can be achieved. If no profit is possible, output 0.

## sample
6
7 1 5 3 6 4
5

</p>