#K8351. Genre Sum for Books
Genre Sum for Books
Genre Sum for Books
You are given a collection of books, each with a unique ID and an associated genre. Your task is to compute the sum of book IDs for each queried genre. Formally, for each genre ( g ) queried, output ( S_g = \sum_{i: genre_i = g} book_id_i ). If a genre does not appear, the result is 0.
Input is provided via standard input and output must be produced through standard output.
inputFormat
The input consists of several lines:
1. The first line contains an integer ( n ) representing the number of books.
2. The second line contains ( n ) space-separated integers, the book IDs.
3. The third line contains ( n ) space-separated strings, representing the genres corresponding to the books.
4. The fourth line contains an integer ( q ) representing the number of queries.
5. The next ( q ) lines each contain a single string, representing the genre to query.
outputFormat
Output a single line with ( q ) space-separated integers. Each integer is the sum of book IDs for the corresponding queried genre.## sample
5
10 20 30 40 50
a b a b c
3
a
b
c
40 60 50