Rewrite DP solution