#K76112. K Most Frequent Words

    ID: 34571 Type: Default 1000ms 256MiB

K Most Frequent Words

K Most Frequent Words

You are given a list of n words and an integer k. Your task is to find the k most frequent words from the list. The words should be sorted primarily by their frequency in descending order and secondarily by lexicographical order if two words have the same frequency.

If k is greater than the number of unique words, simply output all the unique words ordered as described.

Note: All formulas are provided in \( \LaTeX \) format. For instance, if you need to reference the integers n and k, you can express them as \( n \) and \( k \) respectively.

Example:

Input:
6 2
apple banana apple orange banana orange

Output: apple banana

</p>

inputFormat

The input is given via stdin and consists of two lines:

  1. The first line contains two integers \( n \) and \( k \) separated by a space, where \( n \) is the number of words and \( k \) is the number of most frequent words to output.
  2. The second line contains \( n \) words separated by spaces.

outputFormat

The output should be a single line printed to stdout containing the k most frequent words separated by a single space. Words must be ordered by descending frequency, and if two words have the same frequency, they should be sorted in lexicographical order.

## sample
1 1
apple
apple