Solving problem 164 (dynamic programming) + Adding template for cpp