added dp solution for beads