implemented queue with max algorithm