use deque instead of heapq