This page is still under construction.

Parts of this page are still being built. What you see may change.

Key Insertion

Time limit1sMemory limit512 MB

Summary
Simulate the recursive Insert operation on an infinite array for N keys and print the final occupancy up to the largest filled cell.
Level

Hard8 of 10

Topics
Union-find, Implementation, Array, Simulation
Solved
No attempts yet

Problem

As an employee of the Macrohard Company, you have been asked to implement a new data structure for storing integer keys.

The keys are kept in a special ordered collection that behaves like an array AA with infinitely many cells, numbered starting from 11. Initially every cell is empty. The collection supports a single operation, Insert(L,K)\text{Insert}(L, K), where LL is a cell index and KK is a positive integer.

Insert(L,K)\text{Insert}(L, K) is defined recursively:

  • If cell A[L]A[L] is empty, set A[L]←KA[L] \leftarrow K.
  • If cell A[L]A[L] is already occupied, first perform Insert(L+1,A[L])\text{Insert}(L+1, A[L]), and then set A[L]←KA[L] \leftarrow K.

You are given NN indices L1,L2,…,LNL_1, L_2, \ldots, L_N. Starting from the empty array, perform the operations Insert(L1,1)\text{Insert}(L_1, 1), Insert(L2,2)\text{Insert}(L_2, 2), …\ldots, Insert(LN,N)\text{Insert}(L_N, N) in this order, and output the final contents of the array.

Input

The first line contains two integers NN and MM: the number of Insert operations and the largest cell index that may be used in an operation (1≤N≤1310721 \le N \le 131072, 1≤M≤1310721 \le M \le 131072).

The second line contains NN integers L1,L2,…,LNL_1, L_2, \ldots, L_N describing the operations to perform (1≤Li≤M1 \le L_i \le M).

Output

Output the contents of the array after all of the operations have been performed. On the first line, print WW, the largest index of a non-empty cell. On the second line, print the WW integers A[1],A[2],…,A[W]A[1], A[2], \ldots, A[W], using 00 for empty cells.

Examples2

  1. Example 1

    Input
    5 4
    3 3 4 1 3
    
    Expected output
    6
    4 0 5 2 3 1
    
  2. Example 2

    Input
    5 1
    1 1 1 1 1
    
    Expected output
    5
    5 4 3 2 1