#C5169. Categorize and Sort Books

    ID: 48788 Type: Default 1000ms 256MiB

Categorize and Sort Books

Categorize and Sort Books

You are given a list of books. Each book is defined by three fields: its genre (a string), its title (a string), and the number of pages (an integer). Your task is to group the books by genres and sort the books in each genre in ascending order according to their pages.

The input is given in a specific format and you are required to output the result as a JSON object. The keys of this object are the genres and the corresponding value is an array of books (each represented as a JSON object with fields title and pages), sorted by the number of pages.

Formally, if you denote the number of pages by \(p\), then for every genre \(g\), the list \(L_g = [b_1, b_2, \ldots, b_k]\) must satisfy:

[ b_1.p \leq b_2.p \leq b_3.p \leq \cdots \leq b_k.p ]

inputFormat

The first line of input contains an integer \(n\) representing the number of books. The next \(n\) lines each contain data for one book in the following format:

genre;title;pages

Here, the fields are separated by a semicolon (;). The field pages is an integer.

outputFormat

Output a single line containing a JSON object. The JSON object has genres as keys, and each key maps to an array of book objects. Each book object has two fields: title (a string) and pages (an integer). The books in each array must be sorted in ascending order by their number of pages.

## sample
5
Fiction;Book A;200
Non-Fiction;Book B;150
Fiction;Book C;100
Fiction;Book D;300
Non-Fiction;Book E;100
{"Fiction":[{"title":"Book C","pages":100},{"title":"Book A","pages":200},{"title":"Book D","pages":300}],"Non-Fiction":[{"title":"Book E","pages":100},{"title":"Book B","pages":150}]}