#K52902. Magical Filter

    ID: 29412 Type: Default 1000ms 256MiB

Magical Filter

Magical Filter

You are given a list of elements. Each element is represented by four pieces of data: its name, type, energy value, and healing value. The type is either energy or healing.

The task is to rearrange the elements based on the following rules:

  1. All the energy elements must appear before any healing elements.
  2. Among the energy elements, sort them in non-increasing order of their energy value. That is, if an energy element has a higher energy value, it should appear earlier.
  3. Among the healing elements, sort them in non-decreasing order of their healing value. That is, if an healing element has a lower healing value, it should appear earlier.

The output should be the names of the elements in the resulting order, printed in a single line separated by a space.

Here is a brief mathematical description of the ordering criteria:

\begin{align*} &\text{Let } E = { e_i \mid \text{type}(e_i)=\texttt{energy} } \quad \text{and} \quad H = { h_i \mid \text{type}(h_i)=\texttt{healing} } \[6pt] &\text{Sort } E \text{ such that } e_i \ge e_j \quad \forall e_i,e_j \in E \text{ with } i<j \[6pt] &\text{Sort } H \text{ such that } h_i \le h_j \quad \forall h_i,h_j \in H \text{ with } i<j \end{align*}

inputFormat

The input is read from standard input and has the following format:

  • The first line contains an integer n, the number of elements.

  • The following n lines each contain a description of one element in the format:

    name type energy_value healing_value
    

Here, name is a string with no spaces, type will be either energy or healing, and energy_value and healing_value are integers.

outputFormat

Print to standard output a single line that contains the names of the elements in the filtered order, separated by a single space.## sample

7
element1 energy 500 200
element2 healing 100 300
element3 energy 700 100
element4 healing 200 400
element5 energy 600 150
element6 healing 50 250
element7 energy 550 300
element3 element5 element7 element1 element6 element2 element4