Heterocyclic corrosion inhibitors for sweet and sour environments