The present invention provide a compound having an orexin receptor antagonistic activity, which is expected to be useful as medicaments such as agents for the prophylaxis or treatment of sleep disorder, depression, anxiety disorder, panic disorder, schizophrenia, drug dependence, Alzheimer's disease and the like.The present invention relates to a compound represented by the formula (I):wherein each symbol is as defined in the specification, or a salt thereof.