It is a very difficult problem if the box sizes are smaller or if there are more pies to pack.
For this particular problem, we first calculate the total weight of pies:
4 apple pies at 700 g = 2800 g
8 banoffee pies at 725 g =5800 g
3 custaard pies at 600 g = 1800 g
5 rhubarb pies at 675 g = 3375 g
for a total of 13775 g
The minimum number of boxes needed will be 13775/4000=3.44, or
at least 4 boxes will be required.
We also note that 4 boxes will hold 16000 g, and we only need to pack 13775g, so there is a lot of margin.
We can proceed to do the mechanical way, fill in order of apple, banoffee, custard, and finally rhubarb pies.
Following table shows how we fill, naively:
box content Remaining to pack
Box A B C R Weight A B C R
0 4 8 3 5 (original order)
1 4 1 0 0 3525 0 7 3 5
2 0 5 0 0 3625 0 2 3 5
3 0 2 3 0 3250 0 0 0 5
4 0 0 0 5 3375 0 0 0 0
Total weight 13775 g
So that does the job, 4 boxes (minimum) with none exceeding 4000 g to pack a total of 13775 g of pies.