An efficient solution will be to run a binary search on output. We will run binary search on how many topics each student will get and get the optimized result.
There may be other valid solutions to this problem.