K for the Price of One (Hard Version)

Problem Name:
K for the Price of One (Hard Version)
Site:CodeForces
Links:
Tags:
Problem setters: