Under construction

Still being built. Things may change

Tower

Interview

Time limit1sMemory limit1024 MB

Summary
Repeatedly delete every monochrome run of at least K cubes from a tower until none remain, then print the surviving cubes from top to bottom.
Level

Medium5 of 10

Topics
Stack, Simulation
Solved
No attempts yet

Problem

Jonas likes playing with colored cubes. He stacked NN cubes one on top of another to build a tower. Jonas is unhappy with the tower. He wants to rebuild it so that no long monochrome runs of cubes remain.

To do this, Jonas removes all maximal monochrome contiguous runs of at least KK cubes from the tower.

For example, take the tower below and K=3K = 3. Jonas removes the long run of green cubes. The result is the new tower below.

If the tower has several runs to remove, all of them are removed at the same time.

After the tower is rebuilt, new long runs of the same color may appear. In that case Jonas again finds all monochrome runs of length at least KK, removes them, and rebuilds the tower without them.

In this tower the run of red cubes has length K=3K = 3, so Jonas rebuilds the tower again.

This rebuilding process can repeat for a long time. Find what the tower looks like after all rebuilds are finished.

Input

The first line contains two integers NN, the height of the tower, and KK.

The next NN lines contain the colors of the cubes in the tower, given as integers. The first line gives c1c_1, the color of the top cube. The second line gives c2c_2, the color of the cube below the first one. In general, line ii gives cic_i, the color of the cube below cube i−1i-1. The last line gives cNc_N, the color of the bottom cube.

Output

The first line contains N′N', the height of the tower after all rebuilds.

The next N′N' lines contain c1′,c2′,…,cN′′c'_1, c'_2, \dots, c'_{N'}, one per line, the colors of the cubes from the top of the tower to the bottom.

Constraints

1≤N,K,ci≤1 000 0001 \le N, K, c_i \le 1\,000\,000

Examples3

  1. Example 1

    Input
    7 3
    1
    2
    2
    2
    2
    1
    1
    
    Expected output
    0
    
  2. Example 2

    Input
    6 3
    2
    2
    2
    2
    1
    1
    
    Expected output
    2
    1
    1
    
  3. Example 3

    Input
    5 2
    1
    1
    2
    1
    1
    
    Expected output
    1
    2