AcWing 282. 石子合并
原题链接
简单
作者:
将情怀讲泛滥的恶果
,
2021-04-07 20:03:40
,
所有人可见
,
阅读 314
if __name__ == "__main__":
n = int(input())
nums = [int(x) for x in input().split()]
total_sum = [0]
for i in range(1, n + 1):
total_sum.append(total_sum[-1] + nums[i - 1])
dp = [[0 for i in range(0, n + 1)] for x in range(0, n + 1)]
for l in range(2, n + 1):
for i in range(1, n - l + 2):
j = i + l - 1
dp[i][j] = 0x3f3f3f
for k in range(i, j):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + total_sum[j] - total_sum[i - 1])
print(dp[1][n])