Google interview question

dp plus graph approach qsn