#B2160. Patient Appointment Order

    ID: 11242 Type: Default 1000ms 256MiB

Patient Appointment Order

Patient Appointment Order

You are given a list of patients registered for a doctor's appointment. Your task is to order these patients according to the following rules:

  • Patients aged \(\geq 60\) (elderly) are given priority over those aged less than 60.
  • Among the elderly, patients are sorted in descending order of age. If two patients have the same age, they keep their original registration order.
  • Non-elderly patients are arranged in the order they registered.

Output the names of patients in the order they should be seen.

inputFormat

The first line contains an integer \(n\) (\(1 \leq n \leq 10^5\)) representing the number of patients. The following \(n\) lines each contain a patient's name (a string without spaces) and age (an integer), separated by a space. The patients are given in the order of their registration.

outputFormat

Output \(n\) lines, each containing the name of a patient. The names must be printed in the order that the patients should be seen according to the described rules.

sample

4
Alice 65
Bob 30
Carol 70
Dave 25
Carol

Alice Bob Dave

</p>