Rmax algorithm