vacation algorithm in O(n log n)