#P1160. Student Queue Arrangement

    ID: 13694 Type: Default 1000ms 256MiB

Student Queue Arrangement

Student Queue Arrangement

A teacher in a school arranges $N$ students, numbered from $1$ to $N$, into a line following these steps:

  1. Place student $1$ into the queue.
  2. For each student $i$ from $2$ to $N$, the teacher chooses one of the students already in the queue (with numbers in $[1, i-1]$) and instructs student $i$ to stand either immediately to the left or to the right of the chosen student.
  3. After all $N$ students have joined the queue, the teacher removes $M$ students (the remaining students preserve their relative order).

Your task is to simulate this process and output the final order of the student numbers from left to right.

inputFormat

The input consists of several parts:

  • The first line contains two integers $N$ and $M$, where $N$ is the total number of students and $M$ is the number of students to remove.
  • The next $N-1$ lines each contain an integer and a character. For the $i$-th of these lines (corresponding to student $i+1$), the integer $j$ ($1 \leq j \leq i$) indicates the student in the current queue relative to whom the new student is positioned, and the character (either L or R) indicates whether the new student stands to the left or right of student $j$, respectively.
  • The final line contains $M$ distinct integers, representing the numbers of the students to remove from the queue.

You can assume that all operations are valid.

outputFormat

Output a single line with the numbers of the remaining students in the queue, in order from left to right, separated by spaces.

sample

5 2
1 R
1 L
2 L
1 R
1 4
3 5 2