BugForge
DSA

Merge Log Files at Minimum Cost

Mediumheapgreedy 00:00

Merging two files of sizes a and b costs a + b and produces a file of size a + b. Return the minimum total cost to merge all files into one.

Signature

minMergeCost(sizes: int[]) → int
Example 1
sizes = [8,4,6,12]
output = 58
4+6=10, 8+10=18, 12+18=30 → 58
Example 2
sizes = [20]
output = 0
nothing to merge

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ sizes[i] ≤ 10^4
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).