Minimum Cost to Make Arrays Identical
You are given two integer arrays arr and brr of length n, and an integer k. You can perform the following operations on arr any number of times:
- Split
arrinto any number of contiguous subarrays and rearrange these subarrays in any order. This operation has a fixed cost ofk. - Choose any element in
arrand add or subtract a positive integerxto it. The cost of this operation isx.
Return the minimum total cost to make arr equal to brr.
Example 1
Input
arr = [-7,9,5], brr = [7,-2,-5], k = 2Output
13By rearranging subarrays once and then adjusting elements by amounts 2, 7, and 2, the total cost is 13.
Example 2
Input
arr = [2,1], brr = [2,1], k = 0Output
0Since the arrays are already equal, no operations are needed, and the total cost is 0.
Constraints
- 1 <= arr.length == brr.length <= 10^5
- 0 <= k <= 2 * 10^10
- -10^5 <= arr[i] <= 10^5
- -10^5 <= brr[i] <= 10^5