[Solution] Delicious Queries CodeChef Solution
Problem
Chef has prepared a menu for his new restaurant. The menu consists of dishes whose flavour is given by , where denotes the flavour of the dish.
Chef has some rules in his restaurant-
- A dish is served only if the customer has already eaten all the dishes preceding it on the menu.
- Each dish will only be served once.
Aman, the richest man in the area, stops by the restaurant one day and inquires about the deliciousness of a dinner that he may have there. Aman has a favourite ingredient which is a prime number and he wants to eat dishes with the most flavour that contains this ingredient. A dish with flavour contains the ingredient only if is a divisor of .
In order for Aman to patronise Chef's restaurant on a regular basis, he agrees to reorder his menu to fulfil Aman's wishes. He decides that he will reorder the dishes that contain Aman's favourite ingredient while leaving the position of other dishes unchanged. For example, if and then Chef can reorder his menu into or into but not into since the first dish with flavour does not contain as is not a divisor of .
Aman wants to know the deliciousness of his meal, which is the total sum of flavours of all dishes if he eats dishes (first dishes of the menu) and his favourite ingredient is . Can you help Chef find the maximum deliciousness after reordering his menu?
You have to answer queries and each query will contain the favourite ingredient and the number of dishes Aman
wants to eat . Each query is independent, i.e. changes made to the menu in one query do not affect the next one.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains one integer — the number of dishes.
- The next line contains space-separated integers - the flavours of the dishes
- The third line contains — the number of queries.
- Each of the next lines contains two space-separated integers and — the favourite ingredient of Aman and the number of dishes he will eat.
Output Format
For each test case, output lines, the line containing the answer to the query.
Explanation:
Test case : The menu is and we have queries.
- The first query has and . Chef can reorder the menu to . Now Aman will eat dishes with flavour , and . Deliciousness will be
- The second query has and . Chef cannot reorder the menu and it remains the same. Now Aman will eat dishes with flavour , , and . Deliciousness will be
Test case : The menu is .
- One optimal arrangement for the first query is
- One optimal arrangement for the second query is
No comments:
Post a Comment