Merge Stones Cost Problem
Merge Stones Cost Problem — ExecCode Hard DSA Practice
Solve the Merge Stones Cost problem on ExecCode. Free online hard DSA practice in DP. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Piles of stones in a row. A move takes exactly k consecutive piles and merges them into one whose size is the sum; the cost of the move equals that sum. Repeat until one pile remains. If impossible, return -1. Return minimum total cost, or -1. You are given: stones — pile sizes k — merge width >= 2 Return integer cost or -1. Cost is paid at every merge, including the last.
Examples
Input {'stones': [3, 2, 4, 1], 'k': 2}; Output 20. Input {'stones': [3, 2, 4, 1], 'k': 3}; Output -1. Input {'stones': [3, 5, 1, 2, 6], 'k': 3}; Output 25
Constraints
Always merge exactly k consecutive current piles Impossible iff (n-1) is not divisible by (k-1)
Practice Merge Stones Cost free on ExecCode. Browse DSA problems, topic map, and placement guides.