can this be a general `IndexSeq[T]` with an additional `costFn(i: T, j: T, k: T): C` with an `Ordering[C]`? If you abstract in this way, we can be really sure that algorithm is correct.