620. Buy and Sell Stock - III

0

Hard

You are given an array of stock prices. Your task is to determine the maximum profit you can make by buying and selling the stock at most two times. The transactions must not overlap, meaning you can only buy the second stock after selling the first one.

Input Format

The first line of input contains the number of elements N in the array. The second line contains N space-separated elements denoting the price of the stock on the i'th day.

Output Format

Print the maximum profit on a single line.

Example

Input

8 3 3 5 0 0 3 1 4

Output

6

Constraints

N<=100000
Loading...

View Submissions

Console