Renovation
Time limit1sMemory limit1024 MB
Assign each needed nail to a longer or equal existing nail, or buy a new one, minimizing the number bought and then their total length.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Two pointers, Brute force
- Solved
- No attempts yet
Problem
Johanna is renovating her apartment. Because Johanna does not like leaving things to chance, she has planned in detail exactly how many nails she needs for the renovation. In total she needs nails with lengths . In her nail box she has nails with lengths .
If Johanna needs a nail of length , she can use a nail of length if , because she can cut the longer nail down until it is exactly as long as needed. She cannot, however, combine two short nails into a longer nail, or cut a nail more than once, since it has only one nail head.
Before Johanna starts the renovation, she wants to know:
- how many nails she needs to buy, and
- what lengths the nails she needs to buy should have.
She wants to buy as few nails as possible, and in addition wants to buy nails of as short a total length as possible.
Input
The first line contains two integers and , the number of nails Johanna needs and the number of nails Johanna has. The second line contains integers , the lengths of the nails Johanna needs. The third line contains integers , the lengths of the nails Johanna has.
Output
The program should first print one integer: the minimum number of nails Johanna needs to buy. On the next line, the program should print the lengths of the nails Johanna should buy, in ascending order.
Hint
In example 1, Johanna only needs to add three extra nails of lengths , , and .
In example 2, Johanna needs to buy one more nail of length , and also cut a nail of length down to . She could have bought a nail of length and cut the nail of length down to length , but then she would need to buy nails of a longer total length.