Google interview question

Graphs and DP mostly with some twists