Chemical mixture
Practice
4 (46 votes)
Easy Medium
Greedy algorithm
Problem
68% Success 1669 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

View Russian Translation

A wild chemist wants to prepare his mysterious mixture.

The recipe is very simple, he has already mixed all the necessary powders and the only remaining thing is to add exactly m nanoliters of any liquids.

In his lab he has \(n_1\) bottles full of liquid called amulus of various capacities and \(n_2\) bottles full of water, also of various capacities. He knows the capacity of each single bottle and he is curious if he can use the bottles he has to produce the mixture. If he decides to use any bottle, he has to use all the liquid inside it.

Capacities of bottles of amulus are special, because for any two such bottles with capacities \(c_i\) and \(c_j\), the following fact is true:

either \(2 \cdot c_i \leq c_j\) or \(2 \cdot c_j \leq c_i\).

Help the chemist decide if he can prepare the mixture or not.

In one test file you have to handle T test cases.

Input format:

In the first line a single integer T is given denoting the number of test cases. Then descriptions of T test cases follow. In the first line of a description of a single test case there are three integers m, \(n_1\) and \(n_2\) ,denoting the number of nanoliliters of liquids needed for the mixture, the number of bottles of amulus available and the number of bottles of water available. In the second line there are \(n_1\) space separated integers denoting capacities of bottles of amulus. In the third line there are \(n_2\) integers denoting capacities of bottles of water.

Output format:

For each test case output either NO if the mixture cannot be produced, or output YES in the first line followed by the second line containing space separated capacities of bottles of amulus that can be used to achieve the goal in non-decreasing order of their capacities. If there are many possible solutions output the one with the greatest sum of capacities of used bottles of amulus, and in case when there are still many possible such solutions, print any of them.

Constraints:

\(1 \leq T \leq 10\)
\(1 \leq m \leq 10^{18}\)
\(1 \leq n_1 \leq 60\)
\(1 \leq n_2 \leq 15\)
\(1 \leq\) capacity of any bottle \(\leq 10^{17}\)
For any two bottles of amulus with capacities \(c_1\) and \(c_2\), either \(2 \cdot c_i \leq c_j\) or \(2 \cdot c_j \leq c_i\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
11 votes
Tags:
Basic ProgrammingOpenApprovedEasyMathamatics
Points:30
402 votes
Tags:
Brute-force searchEasy-MediumGreedy algorithm
Points:30
23 votes
Tags:
Dynamic ProgrammingMedium