cswork / OAOpen practice workspace

AmazonOA #375

Get Priorities after Execution

Estimated HardJudge-ready

Topics (AI-labelled)Stacks & HeapsSimulationHashing

In each round, find the largest priority p that occurs at least twice; stop if none exists or p=0. Take the first two items with that priority in the current queue, delete the first one, and reduce the second one to floor(p/2). The relative order of everything else is unchanged. Output the final queue.

Input

The first line contains n, and the second line contains n initial priorities. Original bounds: 1≤n≤100000, 1≤initial priority≤10^9; 0 may appear during processing, but 0s cannot be merged further.

Output

The first line is the final length; the second line is the final priorities in their original relative order.

Example 1

Input:

6
6 6 6 1 2 2

Output:

3
3 6 0

Example 2

Input:

4
4 4 2 1

Output:

1
0

Example 3

Input:

6
2 1 5 10 10 1

Output:

2
0 1

Example notes

The outputs are the 3 numbers 3 6 0, the single number 0, and the 2 numbers 0 1, respectively. In the original second example, the first intermediate queue should be 2 2 1; the 17 in the walkthrough of the third example is a typo, and the last original item stays 1 throughout.

Editorial

Loading solution…