An extension of the Polyak convexity principle with application to nonconvex optimization