#P4481. Minimum Cost of Merging Sequence
Minimum Cost of Merging Sequence
Minimum Cost of Merging Sequence
Given a sequence \(A\) of n integers and two integers \(L\) and \(R\), you are allowed to perform merge operations as follows: select an integer \(K\) satisfying \(L \le K \le R\) and merge \(K\) adjacent elements. The cost of a merge is equal to the sum of the \(K\) elements, and the merged element has a weight equal to that sum.
Your task is to compute the minimum total cost required to merge the entire sequence into a single element. If it is impossible to merge the sequence into one element following these rules, output -1
.
Note: When merging a subarray with total sum \(S\) into one element, an additional cost of \(S\) is incurred.
inputFormat
The first line contains an integer \(T\) (\(T \le 10\)), the number of test cases. For each test case:
- The first line contains three integers \(n\), \(L\), and \(R\) (\(1 \le n \le 300\) and \(1 \le L \le R \le n\)).
- The second line contains \(n\) integers representing the sequence \(A\).
outputFormat
For each test case, output a single integer on a new line – the minimum total cost required to merge the sequence into one element, or -1
if it is not possible.
sample
1
3 2 3
1 2 3
6
</p>