Data Structures & AlgorithmsHardGATE CS · ICPC regional

A tree has vertices with values in brackets: A[-2], U[8], Y[5], G[0], Z[-3], V[6], T[1], J[-2]. The edges are A–U, U–Y, Y–G, G–Z, A–V, Y–T, Y–J. A path is any sequence of adjacent vertices with no repeated vertex (a single vertex is a path). What is the maximum possible sum of values along one path?

  1. A. 8
  2. B. 12
  3. C. 14
  4. D. 18

Correct answer: D. 18

A DP that combines the two best downward chains at each vertex finds 18. Paths restricted to a single ancestor-to-descendant line give at most 14, and paths that start at the root give 12.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic