Even Out Two Baskets
Two baskets hold the same number of fruits. first and second list the price of every fruit in each basket.
One swap trades a fruit of the first basket for a fruit of the second, and costs the lower of their two prices. You want both baskets to hold the same prices, each the same number of times.
Return the least total cost, or -1 if no series of swaps does it.
Example 1
Trade a 3 for an 8. That costs 3 and leaves 3, 5 and 8 in both baskets.
Example 2
Trading a 9 for a 20 directly costs 9. Cheaper: trade the 9 for the other 2, then that 2 for the 20, at 2 each.
Example 3
There is one 1 and one 3 in total, so they cannot be shared out evenly.
Constraints
1 <= len(first) == len(second) <= 301 <= price <= 10**9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.