Book Allocation Problem

Can someone please explain the brute force approach to this problem with the code. I am not able to figure it out.