The city aquarium keeps a flat staff roster instead of a management tree: every keeper has a unique staff id, a display name, a monthly stipend, and the id of the senior keeper who signs off on their schedule (or 0 if they answer to no one). An old payroll migration froze several senior keepers' pay at outdated levels, so today a number of keepers who report to a supervisor are actually paid more than that supervisor. Given the full roster, list every keeper whose stipend is strictly greater than the stipend of the senior keeper who supervises them.
n, the number of keepers on the roster.n lines describes one keeper as four space-separated tokens: id name stipend supervisorId.
id is the keeper's unique staff id.name is the keeper's display name (letters and digits only, no spaces).stipend is the keeper's monthly stipend.supervisorId is the staff id of the keeper who supervises them, or 0 if they have no supervisor.n lines may appear in any order (not necessarily sorted by id).Print the name of every keeper whose stipend is strictly greater than their supervisor's stipend, one name per line, ordered by increasing staff id. A keeper with supervisorId = 0 never qualifies. If no keeper qualifies, print nothing.
supervisorId is either 0 or the id of a different keeper on the roster.supervisorId = 0).name has between 1 and 20 characters.Example 1
Input
5 1 Nora 30000 3 2 Meg 90000 3 3 Priya 80000 0 4 Omar 45000 3 5 Kira 15000 1
Expected
Meg
Explanation
Priya (id 3) has no supervisor and earns 80000. Nora (30000) and Omar (45000) both report to Priya but earn less than her, so neither qualifies. Meg (90000) also reports to Priya but earns more than her supervisor, so Meg qualifies. Kira (15000) reports to Nora (30000) and earns less, so she doesn't qualify either. Only Meg is printed.
Example 2
Input
6 1 Ana 50000 0 2 Bo 40000 1 3 Cy 60000 1 4 Dee 60000 3 5 Eli 65000 3 6 Fay 20000 2
Expected
Cy Eli
Explanation
Ana (id 1) has no supervisor and earns 50000. Bo (40000) and Cy (60000) both report to Ana; Bo earns less so he doesn't qualify, but Cy earns more than Ana and qualifies. Dee (60000) and Eli (65000) both report to Cy (60000): Dee's stipend only ties Cy's, which is not strictly greater, so Dee doesn't qualify, while Eli's 65000 is strictly greater and qualifies. Fay (20000) reports to Bo (40000) and earns less. Sorted by id, the qualifying keepers are Cy (id 3) then Eli (id 5).
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →