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 arr into any number of contiguous subarrays and rearrange these subarrays in any order. This operation has a fixed cost of k.
  • Choose any element in arr and add or subtract a positive integer x to it. The cost of this operation is x.

Return the minimum total cost to make arr equal to brr.

Example 1
Inputarr = [-7,9,5], brr = [7,-2,-5], k = 2
Output13
By rearranging subarrays once and then adjusting elements by amounts 2, 7, and 2, the total cost is 13.
Example 2
Inputarr = [2,1], brr = [2,1], k = 0
Output0
Since 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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate