Do you know the binpacking problem? It's about storing all your stuff in a minimum number of bins. The problem is NP-complete and there is quite a number of heuristics to study.
Let's try! Add some elements (e.g., 1, 1, 9, 9) and cycle through the algorithms.

Ooops. Angular.js bootstrap failed. Do you block Javascript?