#K51022. Product Ranking Engine

    ID: 28995 Type: Default 1000ms 256MiB

Product Ranking Engine

Product Ranking Engine

In this problem, you are given information about several products and their reviews. For each product, you will receive its name, the number of reviews, and then the review texts. Your task is to rank these products based on the following criteria:

  • Positive Reviews: The product with a higher number of reviews containing the word good has a higher rank.
  • Negative Reviews: If two products have the same number of positive reviews, the product with fewer reviews containing the word bad is ranked higher.
  • Alphabetical Order: If both criteria above are the same, the products should be sorted in lexicographical (alphabetical) order.

You may express the ranking criteria using the following mathematical notation:

For two products A and B with positive counts PA and PB, and negative counts NA and NB, product A is ranked higher than B if

\( P_A > P_B \) or \( (P_A = P_B \text{ and } N_A < N_B) \) or \( (P_A = P_B \text{ and } N_A = N_B \text{ and } A < B) \).

The input is read from standard input and the output, which is the sorted list of product names, must be printed to standard output with one product per line.

inputFormat

The first line of the input contains a single integer n representing the number of products. Each product is described by the following lines:

  • A line containing the product name (a string).
  • A line containing an integer k which denotes the number of reviews for the product.
  • k lines, each containing a review (a string).

All input is given via standard input (stdin).

outputFormat

Print the names of the products in order of their ranking (one per line) to standard output (stdout).

## sample
3
apple
2
this apple is good
good quality apple
banana
3
bad banana
good banana
bad quality
cherry
1
the cherry is good
apple

cherry banana

</p>