Delicate use of next_permutation()