#C12661. Rearrange List with Limited Occurrences

    ID: 42113 Type: Default 1000ms 256MiB

Rearrange List with Limited Occurrences

Rearrange List with Limited Occurrences

Given a list of integers and a positive integer (k), rearrange the list so that each integer appears at most (k) times while preserving the relative order of its first appearance. The challenge is to implement the solution in such a way that the input is read from standard input (stdin) and the output is written to standard output (stdout).

inputFormat

The input consists of two lines. The first line contains a sequence of integers separated by spaces (which can be empty, corresponding to an empty list). The second line contains one integer (k) (where (k \ge 0)), representing the maximum number of times any integer is allowed to appear in the output list.

outputFormat

Print the rearranged list of integers on a single line with each number separated by a space. If the result is an empty list, print an empty line.## sample

1 1 1 2 3 3 3 3 4 4
2
1 1 2 3 3 4 4