#C7081. Rearrange Books by Genre

    ID: 50913 Type: Default 1000ms 256MiB

Rearrange Books by Genre

Rearrange Books by Genre

You are given N books, each with a unique book ID and an associated genre. Your task is to rearrange the books so that all books belonging to the same genre are grouped together. The relative order of the books within each genre must be preserved.

This problem can be formulated mathematically as follows:

Let \(B = [b_1, b_2, \dots, b_N]\) be the list of book IDs and \(G = [g_1, g_2, \dots, g_N]\) the corresponding list of genres. You need to output an array \(R\) such that for any two books \(b_i\) and \(b_j\) with the same genre (i.e. \(g_i = g_j\)), if \(i < j\) then \(b_i\) appears before \(b_j\) in \(R\), and books of different genres are grouped by their first occurrence order.

Example:
For \(N=7\), book IDs: [101, 102, 103, 104, 105, 106, 107] and genres: [SciFi, Romance, SciFi, Mystery, Romance, SciFi, Mystery], the output should be [101, 103, 106, 102, 105, 104, 107].

inputFormat

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

N
id_1 id_2 ... id_N
genre_1 genre_2 ... genre_N

Here, N is the number of books. The second line contains N integers representing the book IDs. The third line contains N strings representing the corresponding genres of each book.

outputFormat

Output the rearranged list of book IDs in one line, separated by a space. The output should be written to standard output.

## sample
7
101 102 103 104 105 106 107
SciFi Romance SciFi Mystery Romance SciFi Mystery
101 103 106 102 105 104 107