DP for section1.1 beads3.cpp