#C10785. Top Selling Products by Category

    ID: 40028 Type: Default 1000ms 256MiB

Top Selling Products by Category

Top Selling Products by Category

You are given a list of n products. Each product is associated with a category and a sales figure. Your task is to determine, for each unique product category, the product that achieved the highest sales. In the event of a tie (i.e. when two products in the same category have the same number of sales), choose the product with the smaller product ID.

The product IDs are assigned in the order of appearance starting from 1.

Note: The output should list the categories in increasing order.

The problem can be mathematically formulated as: $$ result = \{ (c, i) \mid c \in C, \; i = \min\{ j \mid sales_j = \max_{k: category_k=c}(sales_k) \} \} $$ where C represents the set of unique categories.

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. The first line contains a single integer n which denotes the number of products.
  2. The second line contains n space-separated integers representing the category of each product.
  3. The third line contains n space-separated integers representing the sales figures for each product.

outputFormat

Output to standard output (stdout) the result as follows:

  1. The first line should contain a single integer denoting the number of unique categories.
  2. Each of the following lines should display two space-separated integers: the category and the product ID of the best-selling product in that category. The categories must be printed in increasing order.## sample
8
1 2 1 3 2 2 1 3
100 200 50 300 400 150 250 100
3

1 7 2 5 3 4

</p>