final solution for treasure problem