#K54572. Find Top K Oldest Employees

    ID: 29783 Type: Default 1000ms 256MiB

Find Top K Oldest Employees

Find Top K Oldest Employees

You are given a list of employees along with their ages. Your task is to find the top K oldest employees and print them in descending order of age. In case two or more employees have the same age, they should be listed in the same order as they appear in the input.

The problem requires you to read the employee data from stdin and output the result to stdout. Use proper sorting techniques to ensure the stable ordering for tie cases.

Mathematically, if we denote each employee as a tuple \((name_i, age_i)\), you are to return a list of tuples sorted by \(age\) such that if \(age_i = age_j\), the employee with the smaller index in the input comes first.

inputFormat

The first line contains an integer \(N\), the number of employees.

The next \(N\) lines each contain a string and an integer, representing the name and age of an employee respectively. The name and age are separated by spaces.

The last line contains an integer \(K\), the number of oldest employees to output.

You must read the input from stdin.

outputFormat

Output exactly \(min(K, N)\) lines. Each line should contain the name and age of an employee separated by a space, listed in descending order of age. For employees with equal ages, maintain their original order from the input.

The output should be written to stdout.

## sample
5
Alice 45
Bob 50
Charlie 35
David 45
Eve 32
3
Bob 50

Alice 45 David 45

</p>