Maybe I'm misunderstanding the problem, but can't you just get all the substrings made up of 0s, sort them by length and sum the lenghts of the first
k
strings + the number of 1s already present in the string?