works, but slow ~10s for n=10k :(