Trim loss minimization under a given number of open stacks