Google interview question

DP problem