Schlemiel the Painter algorithm? Worst case O(n^2) when all the newest packages are extremely unpopular.