Pergunta de entrevista da empresa SAP

Dynamic programming question to be solved on a text editor explaining the logical reasoning for the answer.