refer to other's elegant code, much like 8 queues problem