Sequence Merging
Time limit1sMemory limit128 MB
Repeatedly replace an adjacent pair with its larger value and pay that value, ordering the merges to leave the smallest total cost.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Stack, Greedy
- Solved
- No attempts yet
Problem
You are given a sequence . In one step you can pick two adjacent numbers and and replace both of them with the single number . That step costs . Each step makes the sequence one shorter, so after steps a single number is left.
Given the sequence, compute the minimum total cost of reducing it to length 1.
Input
The first line contains the number of test cases ().
Each test case takes two lines. The first line contains the length of the sequence (). The second line contains the integers of the sequence, separated by single spaces. Every integer is between and inclusive.
Output
For each test case, print one line in the format Case #x: R, where is the test case number starting from 1 and is the minimum cost of reducing that sequence to length 1.