Laundry
Time limit3sMemory limit128 MB
Assign clothespin colors to each friend so socks and shirts each get one color, friends share no color, and the number of colors used is minimized.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Binary search
- Solved
- No attempts yet
Problem
A few friends decided to do their laundry together. They are all very tidy, so on each day every friend wears one clean pair of socks and one clean shirt. All of their dirty socks and shirts have now been washed, and they want to hang everything up to dry with clothespins.
They agreed on the following rules:
- every sock is fastened to the line with a single clothespin;
- every shirt is fastened with three clothespins;
- all clothespins used on one person's socks must have the same color;
- all clothespins used on one person's shirts must have the same color;
- clothes belonging to different people must not use clothespins of the same color;
- subject to all of the above, they want to use as few distinct clothespin colors as possible.
If friend collected laundry for days, that friend has pairs of socks and shirts, and therefore needs clothespins for the socks (all of one color) and clothespins for the shirts (all of one color). A person may use the same color for both their socks and their shirts, or two different colors.
The friends have gathered all their clothespins and counted how many they have of each color. Help them find the smallest number of distinct colors they need to use.
Input
The first line contains two integers and () — the number of friends and the number of available clothespin colors.
The second line contains integers (), where is the number of days friend was collecting laundry.
The third line contains integers (), where is the number of clothespins of the -th color.
Output
Print a single integer: the minimum number of distinct clothespin colors needed to hang up all the laundry according to the rules. If it is impossible, print the single word NIE instead.
Notes
In the first example there are two friends. The first friend () needs clothespins for socks and for shirts; the second friend () needs for socks and for shirts. Since , the second friend can use the first color (which has clothespins) for both socks and shirts. The first friend can then use, for example, the second and the fourth colors (each with clothespins) — one for the socks and one for the shirts. Three colors are used in total.