Zillow Interview Question

a modified dynamic programming took me some time to figure out