Optimized Monte Carlo method for glasses