Amazon Interview Question

question related to graph and dp