------
## 最终结论
```python
def minCost(stones, K):
    N = len(stones)
    if (N - 1) % (K - 1) != 0:
        return -1

    # Initialize dp array and prefix_sum array
    dp = [[float('inf')] * N for _ in range(N)]
    prefix_sum = [0] * N

    # Fill the prefix_sum array
    prefix_sum[0] = stones[0]
    for i in range(1, N):
        prefix_sum[i] = prefix_sum[i - 1] + stones[i]

    # Fill the dp array
    for length in range(K, N + 1):  # length of the interval
        for i in range(N - length + 1):  # starting index of the interval
            j = i + length - 1  # ending index of the interval
            total_stones = prefix_sum[j] - (prefix_sum[i - 1] if i > 0 else 0)
            for m in range(i, j, K - 1):  # possible split points
                dp[i][j] = min(dp[i][j], dp[i][m] + dp[m + 1][j])

            if (length - 1) % (K - 1) == 0:  # Can we merge this whole interval?
                dp[i][j] += total_stones

    return dp[0][N - 1]

# Example test cases
print(minCost([3, 2, 4, 1], 2))   # Output: 20
print(minCost([3, 2, 4, 1], 3))   # Output: -1
print(minCost([3, 5, 1, 2, 6], 3)) # Output: 25
```