#K50822. Organize Books

    ID: 28950 Type: Default 1000ms 256MiB

Organize Books

Organize Books

You are given N books and a maximum reading level M. Each book is described by its title and its reading level. Your task is to organize the books as follows:

  • Books are first grouped by their reading level from 1 to M.
  • Within each level, the books are sorted in ascending alphabetical order based on their titles.

Formally, if a book has a reading level \(l\), then it should appear in the output after all books with levels \(l' l\). For books with the same level, let \(a\) and \(b\) be two titles, then \(a\) comes before \(b\) if and only if \(a < b\) lexicographically.

You need to read the input from stdin and write the sorted list of book titles to stdout, with each title on a new line.

inputFormat

The input is given via standard input and has the following format:

N M
Title1 Level1
Title2 Level2
... 
TitleN LevelN

Where:

  • N is an integer representing the number of books.
  • M is an integer representing the maximum reading level.
  • Each of the next N lines contains a string Title (without spaces) and an integer Level (between 1 and M) separated by whitespace.

outputFormat

Output the sorted book titles to standard output. Each title should be printed on a new line. The order must first follow the ascending order of reading levels (from 1 to M) and then lexicographically within each reading level.

## sample
5 3
HarryPotter 2
LordOfTheRings 1
TheHobbit 1
AliceInWonderland 3
MobyDick 2
LordOfTheRings

TheHobbit HarryPotter MobyDick AliceInWonderland

</p>