Data Structures & AlgorithmsVery hardICPC regional · GATE CS

Piles of stones [3, 5, 1, 2, 6] are in a row. Each move merges two ADJACENT piles into one, at a cost equal to their combined number of stones. What is the minimum total cost to merge everything into a single pile?

  1. A. 41
  2. B. 37
  3. C. 17
  4. D. 38

Correct answer: B. 37

Interval DP: the last merge splits the row into a left block and a right block, and each block is merged optimally first; cost adds the total stones in the interval. Always merging the cheapest adjacent pair is a greedy trap.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic