More than £33m will be spent making coal tips safer around Wales in an attempt to protect villages.