Sorting and dp