ORIGINAL CODING QUESTION
Minimum log merge cost
Merge any two positive log sizes for cost equal to their sum; the merged log has that size. Repeat until one log remains. Return the minimum total cost.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n integer values. Print one integer as described.
Constraints
1 ≤ n ≤ 200; 1 ≤ size ≤ 100.
Example 1
Input
3 1 2 3
Output
9
Merge 1+2 for 3, then 3+3 for 6; total 9.
Example 2
Input
1 5
Output
0
No merge is needed.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
An early merge cost is counted again in later merges.
Hint 2
Merge the two smallest current logs first.
Hint 3
A min-heap efficiently maintains the changing sizes.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →