Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional

A tree has vertices with values in brackets: J[0], N[0], I[8], Q[3], K[-2]. The edges are J–N, N–I, N–Q, I–K. 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. 10
  4. D. 11

Correct answer: D. 11

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic