How is the list generated in the first place? Does its order otherwise matter? Do you know the value of S as the list is being built, or only once it has been fully built?
S