SDE Path

Best Time to Buy and Sell Stock

Easy

Best Time to Buy and Sell Stock

Given daily stock prices, find the maximum profit achievable from a single buy and a single sell.

Input format

The first line contains N. The second line contains N daily prices (non-negative integers).

Output format

Print the maximum profit from one buy followed by one sell (0 if no profit is possible).

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
6
7 1 5 3 6 4
Expected output
5
Example 2
Input
5
7 6 4 3 1
Expected output
0