给你两个长度相等的整数数组 nums
和 cost
,和一个整数 k
。
你可以将 nums
分割成多个子数组。第 i
个子数组由元素 nums[l..r]
组成,其代价为:
(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])
。注意,i
表示子数组的顺序:第一个子数组为 1,第二个为 2,依此类推。
返回通过任何有效划分得到的 最小 总代价。
子数组 是一个连续的 非空 元素序列。
示例 1:
输入: nums = [3,1,4], cost = [4,6,6], k = 1
输出: 110
解释:
将nums
分割为子数组 [3, 1]
和 [4]
,得到最小总代价。
[3,1]
的代价是 (3 + 1 + 1 * 1) * (4 + 6) = 50
。[4]
的代价是 (3 + 1 + 4 + 1 * 2) * 6 = 60
。示例 2:
输入: nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
输出: 985
解释:
将nums
分割为子数组 [4, 8, 5, 1]
,[14, 2, 2]
和 [12, 1]
,得到最小总代价。
[4, 8, 5, 1]
的代价是 (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525
。[14, 2, 2]
的代价是 (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250
。[12, 1]
的代价是 (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210
。
提示:
1 <= nums.length <= 1000
cost.length == nums.length
1 <= nums[i], cost[i] <= 1000
1 <= k <= 1000
dp[i]
is the minimum cost to split the array suffix starting at i
.k * total_cost_of_the_subarray
. This is because when we increase i
to (i + 1)
, the cost increase is just the suffix sum of the cost array.