http://www.spoj.com/problems/BEADS/