Compressing file requires units of free space. Writing its compressed version consumes units, and deleting the original releases units. The free space therefore increases by .
Consider the remaining file with the smallest . If it does not fit, no remaining file fits. If it fits, compressing it immediately cannot hurt: move it to the front of any successful order. Every file that originally preceded it now has more free space available, and after those files the amount of free space is unchanged.
Repeating this argument shows that sorting by in nondecreasing order is optimal. Ties may be resolved arbitrarily.