#K46157. Top N Employees

    ID: 27914 Type: Default 1000ms 256MiB

Top N Employees

Top N Employees

Given a list of employees where each employee is represented as a pair of a name and a salary, your task is to output the names of the top N employees. The employees should be sorted primarily by their salary in descending order, and if two or more employees have the same salary, they should be sorted in ascending alphabetical order by name.

The problem can be mathematically described as follows:

Let \( E = \{(name_i, salary_i)\}_{i=1}^m \} be the set of employees and let \( N \) be a positive integer such that \( N \leq m \). Then, we need to select a subset \( R \subseteq E \) of size \( N \) satisfying:

[ R = \text{first } N \text{ elements of } E' \quad \text{where } E' \text{ is sorted by } (salary, name) \text{ as } \left( -salary_i, name_i \right). ]

Your solution must read the input from standard input (stdin) and produce the output to standard output (stdout). Ensure that your solution handles tie cases correctly as described above.

inputFormat

The input consists of multiple lines:

  • The first line contains a single integer \( m \) (the number of employees).
  • The next \( m \) lines each contain a string and an integer, representing the name of the employee and their salary, respectively. The name and salary are separated by a space.
  • The last line contains an integer \( N \) (the number of top employees to output).

For example:

5
Alice 50000
Bob 60000
Charlie 50000
David 70000
Eve 60000
2

outputFormat

Output a single line containing the names of the top \( N \) employees, separated by a single space, in the order specified.

For the sample input above, the expected output is:

David Bob
## sample
5
Alice 50000
Bob 60000
Charlie 50000
David 70000
Eve 60000
2
David Bob