cswork / OAOpen practice workspace

VisaOA #5

Greater-Than-Neighbors Stabilization

Estimated EasyJudge-ready

Topics (AI-labelled)Simulation

Repeatedly filter the array simultaneously: keep the first and last elements, and the interior elements strictly greater than both their left and right neighbors; each round compares against the array as it was at the start of that round. Continue until a round makes no change, and return the final array.

Input

n (1≤n≤200000), followed by n integers (absolute value ≤10^9, site-added).

Output

Output the length of the final array and its elements.

Example 1

Input:

6
3 6 4 8 2 9

Output:

2 3 9

Example 2

Input:

1
1

Output:

1 1

Example 3

Input:

5
1 2 2 1 5

Output:

2 1 5

Editorial

Loading solution…