Editorial for Merge Piles
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Approach
The important observation is that after several moves, every group always contains a contiguous range of the original piles.
Let be the minimum cost needed to merge piles
into one group.
If
, the range is already one group, so
.
For a range with at least two piles, consider the final merge made inside that range. Immediately
before that final merge, the range must be split into two adjacent merged groups:
and
for some
.
The cost is:
Try every possible split and take the minimum. Prefix sums let us find each range sum in
constant time.
Compute the values in increasing order of interval length, since shorter intervals are needed before longer intervals.
Time complexity is .
Solution (Python)
import sys
data = list(map(int, sys.stdin.buffer.read().split()))
n = data[0]
a = data[1:]
prefix = [0] * (n + 1)
for i in range(n):
prefix[i + 1] = prefix[i] + a[i]
dp = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for left in range(n - length + 1):
right = left + length - 1
total = prefix[right + 1] - prefix[left]
best = 10**30
for split in range(left, right):
cost = dp[left][split] + dp[split + 1][right] + total
if cost < best:
best = cost
dp[left][right] = best
print(dp[0][n - 1])
Comments