A DP solution to problem 6.
Detailed analysis will be provided later.