Data Structures & AlgorithmsHardCodeforces Div2 B-D · ICPC regional

Ropes of lengths 4, 3, 2 and 6 must be joined pairwise into one rope; joining costs the sum of the two lengths and you may join any two ropes at each step. What is the minimum total cost?

  1. A. 29
  2. B. 31
  3. C. 30
  4. D. 15

Correct answer: A. 29

Always join the two shortest ropes (Huffman-style greedy with a min-heap). Joining the longest pairs first raises the total because the big rope is re-counted at each merge.

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

Exams that ask this topic