LogiQuest

256. Stock Trading with Cooldown

MediumState Machine DP

You may complete as many transactions as you like, but after selling you must wait one day before buying again. Print the maximum profit.

Input format

Line 1: n Line 2: n prices

Output format

The maximum profit.

Example 1

Input

5
1 2 3 0 2

Output

3

Example 2

Input

1
1

Output

0

Constraints

1 ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…