This page is still under construction.

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

Genetic engineering

Time limit1sMemory limit128 MB

Summary
Delete the fewest elements so the rest splits into blocks of k equal values, and print the lexicographically smallest among the longest such genomes.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Segment tree
Solved
No attempts yet

Problem

Byteotian paleoarchaeologists recently dug up a few lumps of amber with ancient mosquitoes trapped inside. Analysis of the samples showed the insects came from the Jurassic period, so they had most likely fed on the large reptiles that ruled the Byteotian lands. That gave the geneticists an idea: recover byteoraptor genetic material from the blood inside a mosquito.

Like every Byteotian organism, a byteoraptor genome is a chain of byteo-aminoacids. For simplicity the types of byteo-aminoacids are numbered with natural numbers. A genome carries redundancy. Every type is repeated kk times, so the length of a correct genome is always a multiple of kk. In other words, split a genome from the front into blocks of kk consecutive byteo-aminoacids and each block holds a single type.

The geneticists isolated a chain of nn byteo-aminoacids from the blood of one mosquito. The chain may not be a valid genome, because they suspect foreign byteo-aminoacids contaminated it. They now want to delete as few byteo-aminoacids as possible so that a normal genome remains. The byteo-aminoacids that survive keep their original order. When several answers have the same length, they look for the genome that comes first in lexicographical order.

Two chains l1l_1 and l2l_2 of the same length are compared like this. Find the first position where they differ, and the chain whose byteo-aminoacid carries the smaller number at that position comes first.

Input

The first line has the length nn of the extracted chain and the redundancy degree kk of a correct genome (1≤n≤1,000,0001 \le n \le 1{,}000{,}000, 2≤k≤1,000,0002 \le k \le 1{,}000{,}000).

The second line has the types g1,…,gng_1, \ldots, g_n of the byteo-aminoacids along the chain, in order (1≤gi≤1,000,0001 \le g_i \le 1{,}000{,}000).

Output

On the first line print the greatest length mm of a correct genome that can be made by deleting byteo-aminoacids from the chain (0≤m≤n0 \le m \le n).

On the second line print the types of that genome in order, separated by single spaces. When several answers of length mm exist, print the one that comes first in lexicographical order. If m=0m = 0, that is, if no non-empty correct genome can be made at all, leave the second line empty.

Examples3

  1. Example 1

    Input
    16 3
    3 2 3 1 3 1 1 2 4 2 1 1 2 2 2 2
    
    Expected output
    9
    1 1 1 2 2 2 2 2 2
    
  2. Example 2

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

    Input
    6 2
    1 2 2 3 3 1
    
    Expected output
    4
    2 2 3 3