Opening Day 2
InterviewTime limit1sMemory limit256 MB
Given wok sizes, each cooking uses one or two distinct woks and produces the sum of their sizes; find the minimum number of cookings summing to exactly N.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Haebin loves jajangmyeon so much that he opened a Chinese restaurant that sells nothing else. He is ambidextrous, so a single cooking can use up to two woks at the same time.

Haebin hates waste, so every wok he uses is filled to its exact capacity. Cooking with one wok of size produces exactly bowls, and cooking with two woks of sizes and at the same time produces exactly bowls. A single cooking cannot use the same wok twice, but if Haebin owns two or more woks of the same size, he can use two of them together. Every wok is available again for the next cooking.
The order is bowls, and the bowls produced by all of the cookings must add up to exactly . Write a program that finds the smallest number of cookings that fills the order.
Input
The first line has the number of bowls ordered and the number of woks Haebin owns , separated by a space. (, )
The second line has the wok sizes , separated by spaces. () Haebin may own several woks of the same size.
Output
Print the minimum number of cookings needed to fill the whole order. If no combination of cookings produces exactly bowls, print -1.
Hint
Consider making 6 bowls with one wok of size 1 and one wok of size 3. Cook 3 bowls in the size 3 wok, then cook 3 more bowls in the same wok, and 6 bowls are done in two cookings. A wok can be reused in every later cooking, but only two different woks can go into one cooking.