Under construction

Still being built. Things may change

Gratitude

Interview

Time limit3sMemory limit512 MB

Summary
Count how often each line appears across 3N notes, then output the K most frequent, breaking ties by which appeared most recently.
Level

Medium4 of 10

Topics
Hash map, Sorting, String, Implementation
Solved
No attempts yet

Problem

Ben heard about studies by Emmons and McCullough that suggest that intentionally practicing gratitude has a lasting effect on people's happiness. Since he wants to be happy too, he decided that at the end of each day he will think back over the past day and write down three things he is thankful for, one thing per line. At the end of NN days in which he practiced this exercise, he was curious to know which things appear the most on his list. Help Ben get the KK things he was grateful for most frequently.

Input

The input begins with one line containing two space-separated integers, NN and KK, in that order. Then follow 3N3N lines containing Ben's notes from NN days. You may assume that the three lines that correspond to the same day contain no repetitions. That is, if you partition the input into NN chunks of 33 consecutive lines, no chunk contains two identical lines.

Output

The output should represent the list of things that Ben is grateful for, ordered by frequency of appearance in Ben's list (with the most frequent item first). In case of two items with equal frequency, the most recent item should appear first. That is, in case of a tie in the number of appearances, the item whose last appearance is later in the input should appear earlier in the output. Finally, if there are more than KK different items in Ben's list, your output should contain only the KK first items (according to the required order).

Constraints

  • 1≤K≤3N≤100 0001 \le K \le 3N \le 100\,000
  • Each input line contains at most 5050 (ASCII) characters.

Examples2

  1. Example 1

    Input
    2 2
    Supportive parents
    Being able to solve a hard problem
    Good food
    Fun game with friends
    Good food
    Being healthy
    
    Expected output
    Good food
    Being healthy
    
  2. Example 2

    Input
    2 6
    Supportive parents
    Being able to solve a hard problem
    Good food
    Fun game with friends
    Good food
    Being healthy
    
    Expected output
    Good food
    Being healthy
    Fun game with friends
    Being able to solve a hard problem
    Supportive parents