cswork / OAOpen practice workspace

MicrosoftOA #15

Construct Maximum Balanced Circle

Estimated HardJudge-ready

Topics (AI-labelled)HashingGreedyTwo Pointers

Choose as many people as possible and arrange them in a circle so that adjacent heights differ by at most 1; the first and last people are also adjacent. Output any valid circle of maximum size; any rotation and direction is allowed.

Input

The first line contains n (1..200000), and the second line contains n heights (1..200000).

Output

On the first line output the number of people k; on the second line output k heights. You may only use the people actually available in the input; any maximum valid circle is accepted.

Example 1

Input:

7
4 3 5 1 2 2 1

Output:

5
3 2 1 1 2

Example 2

Input:

5
3 7 5 1 5

Output:

2
5 5

Example 3

Input:

3
5 1 4

Output:

2
5 4

Example notes

Example 1: you can choose the 5 people 1, 1, 2, 2, 3 and arrange them as 1 2 3 2 1; the taller height 3 has only one person, so it cannot serve as an inner link of a larger circle.

Example 2: the two 5s form a valid circle, and the other heights are at least 2 apart, so the maximum size is 2.

Example 3: 4 and 5 are adjacent and form a 2-person circle; 1 cannot join, so the size is 2.

Editorial

Loading solution…