Under construction

Still being built. Things may change

Max Rise

Interview

Time limit1sMemory limit512 MB

Summary
Given daily stock prices, find the maximum profit from one buy followed by one later sell, where selling on the same day gives zero.
Level

Easy3 of 10

Topics
Array, Greedy
Solved
No attempts yet

Problem

Someone who can foresee the future knows exactly how the stock price of ANA Company will change over the next NN days. Based on this prediction, Jeonggyun wants to buy one share at a suitable time and sell it at a suitable time to earn the greatest possible profit.

Let the stock prices of ANA Company over the next NN days be a1,a2,...,aNa_1, a_2, ..., a_N. If Jeonggyun buys on day ii and sells on day jj, he earns aj−aia_j - a_i. Since he has plenty of funds, he can buy no matter how high the price is, and if the situation does not work out, he can sell right after buying.

Given the prices of ANA Company over the next NN days, what is the maximum profit Jeonggyun can earn by buying one share at a suitable time and selling it at a suitable time?

Input

The first line contains an integer NN (1≤N≤2000001 \le N \le 200000).

The second line contains integers a1,a2,...,aNa_1, a_2, ..., a_N. Each aia_i (1≤ai≤1091 \le a_i \le 10^9) is the stock price of ANA Company on day ii.

Output

Print the maximum profit Jeonggyun can earn by buying one share at a suitable time and selling it at a suitable time.

Examples3

  1. Example 1

    Input
    5
    4 2 3 1 5
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    3 2 1
    
    Expected output
    0
    
  3. Example 3

    Input
    4
    7 1 2 6
    
    Expected output
    5