Cover them all problem

solved but getting tle for large n how can I optimised it?