Consider an instance of this problem where you have two buckets of the same size, and Sum(B) = Sum(V). This means you need to distribute the vials equally over the two buckets, otherwise one will overflow and there won't be enough left for the other. This is called the partition problem, and it is known to be NP-complete.
Edit: Of course, NP-completeness doesn't mean the problem can't be solved, just that the running time will be exponential in the size of the input (in this case, the log2 of the biggest bucket size).
If we can find the smallest amount of liquid needed to fill a bucket (including spillage), solving the problem is a simple matter of doing this for each bucket, and removing the used vials from the set of available vials after each bucket.
We can do this by using dynamic programming:
- For a given bucket b, consider all the buckets of size 0 up to volume(b).
- The size 0 bucket obviously requires no liquid
- For each size s, find a vial v such that:
- The solution for s-volume(v) does not use v
- (The amount of liquid used for s-volume(v)) + volume(v) is minimized
- At the end of all this you will have the vials used to fill bucket b. Then you just remove those from the set of available vials, and move on to the next bucket.