AlgoLounge

Rearranging Fruits

hard

You have two baskets with n fruits each. basket1[i] and basket2[i] are the costs of the fruits in them. You want the two baskets to be equal: once both are sorted by cost, they must be exactly the same array.

To get there, you may repeat the following move as many times as you like:

  • Pick an index i in basket1 and an index j in basket2, and swap those two fruits.
  • The swap costs min(basket1[i], basket2[j]).

Return the minimum total cost to make the baskets equal, or -1 if it can't be done.

The answer can be larger than 2^31 − 1, so use a 64-bit integer (long / long long) in Java, C++ and C.

Examples

Example 1

Input:  basket1 = [3,3,8,8], basket2 = [1,1,8,8]
Output: 1

Swap a 3 from basket1 with a 1 from basket2 for min(3, 1) = 1. Both baskets become [1,3,8,8] once sorted.

Example 2

Input:  basket1 = [1,2], basket2 = [2,3]
Output: -1

There's only one fruit costing 1, so it can never be in both baskets.

Example 3

Input:  basket1 = [10,10,10,10], basket2 = [20,20,20,20]
Output: 20

Swap a 10 with a 20 twice, paying 10 each time.

Example 4

Input:  basket1 = [1,10,10,10,10], basket2 = [1,20,20,20,20]
Output: 4

Swapping a 10 and a 20 directly costs 10. It's cheaper to go through the 1s: swap basket1's 1 with a 20 (cost 1), then swap a 10 with basket2's 1 (cost 1). That moves one 10 and one 20 for a cost of 2. Doing it twice costs 4.

Example 5

Input:  basket1 = [2,1], basket2 = [1,2]
Output: 0

Sorted, both baskets are already [1,2].

Constraints

  • basket1.length == basket2.length
  • 1 <= basket1.length <= 10^5
  • 1 <= basket1[i], basket2[i] <= 10^9
TypeScript
Loading editor…
List of test cases (33)
Case 1
basket1 = [4,2,2,2]
basket2 = [1,4,1,2]
Case 2
basket1 = [2,3,4,1]
basket2 = [3,2,5,1]
Case 3
basket1 = [1]
basket2 = [1]
Case 4
basket1 = [1]
basket2 = [2]
Case 5
basket1 = [5,5]
basket2 = [7,7]
Case 6
basket1 = [3,3,8,8]
basket2 = [1,1,8,8]
Case 7
basket1 = [10,10,10,10]
basket2 = [20,20,20,20]
Case 8
basket1 = [1,10,10,10,10]
basket2 = [1,20,20,20,20]
Case 9
basket1 = [2,1]
basket2 = [1,2]
Case 10
basket1 = [6,6,6,6]
basket2 = [6,6,6,6]
Case 11
basket1 = [1,1,2,2]
basket2 = [3,3,4,4]
Case 12
basket1 = [5,8,15,7]
basket2 = [5,7,8,15]
Case 13
basket1 = [4,4,4,4,3]
basket2 = [5,5,5,5,3]
Case 14
basket1 = [1,1,1,1]
basket2 = [1,1,1,2]
Case 15
basket1 = [84,80,43,8,80,88,43,14,100,88]
basket2 = [32,32,42,68,68,100,42,84,14,8]
Case 16
basket1 = [999999999,999999999,999999999,999999999,999999999,999999999,999999999,999999999,999999998,999999998]
basket2 = [999999997,999999997,999999997,999999997,999999997,999999997,999999997,999999997,999999998,999999998]
Case 17
basket1 = [2]
basket2 = [2]
Case 18
basket1 = [6,5,1]
basket2 = [6,5,1]
Case 19
basket1 = [2]
basket2 = [4]
Case 20
basket1 = [1,5,4,1]
basket2 = [1,1,4,5]
Case 21
basket1 = [4,5]
basket2 = [5,4]
Case 22
basket1 = [3]
basket2 = [3]
Case 23
basket1 = [1,1,1,3]
basket2 = [3,4,4,1]
Case 24
basket1 = [5]
basket2 = [4]
Case 25
basket1 = [3,3,6]
basket2 = [3,6,3]
Case 26
basket1 = [3]
basket2 = [3]
Case 27
basket1 = [6,5,5]
basket2 = [2,6,2]
Case 28
basket1 = [3]
basket2 = [1]
Case 29
basket1 = [8,10,23,10,26,19,26,4,27,1,24,11,23,20,14,10,23,11,23,13,19,11,19,15,19,20,3,3,14,27,20,21,11,28,7,26,22,24,7,22,25,14,26,3,4,14,27,29,24,23]
basket2 = [26,11,23,14,28,28,13,28,3,8,15,14,23,24,14,29,7,9,9,7,27,20,14,26,2,2,27,27,21,21,25,13,11,24,2,23,26,14,2,24,28,28,1,26,13,18,21,10,18,14]
Case 30
basket1 = [62,451,175,602,494,471,608,812,621,376,937,305,45,245,577,657,308,849,673,650,511,815,698,677,921,901,705,672,810,448,560,463,143,937,310,27,573,141,878,172,560,835,542,51,502,23,166,608,282,698,836,52,196,299,64,745,511,819,649,760,588,77,47,772,384,936,579,65,119,3,442,53,531,961,222,594,301,1,744,952,35,978,834,812,650,448,171,175,24,119,228,183,355,548,832,308,832,815,739,41,33,463,41,166,602,151,491,648,584,299,253,767,806,171,588,173,588,550,106,358,502,791,584,473,432,745,635,206,145,887,923,836,188,598,749,223,142,598,455,474,819,615,82,467,471,109,874,142,708,376,673,253,904,695,675,650,898,908,717,648,446,473,507,147,41,605,835,363,615,76,45,64,678,300,608,961,363,981,671,719,539,824,676,929,207,678,384,895,308,36,757,31,355,899,730,272,33,57,41,746,594,954,772,656,679,929,62,981,980,280,52,408,701,708,671,17,903,824,405,531,467,661,141,514,980,188,272,656,571,501,77,495,474,182,769,880,473,717,384,545,276,47,510,604,958,207,849,245,597,548,893,767,33,130,359,408,465,143,245,106,673,604,510,695,751,163,764,163,608,442,978,408,646,887,228,552,196,92,582,764,405,495,17,130,719,31,778,806,232,436,360,730,491,707,749,997,751,92,539,598]
basket2 = [5,597,673,742,719,146,925,222,584,61,714,1,618,109,310,68,196,719,394,57,883,661,394,969,707,446,511,904,172,843,943,545,11,661,737,33,30,245,182,414,675,303,608,808,525,679,943,605,377,676,866,717,618,196,791,895,311,607,85,414,697,359,621,598,962,925,25,68,745,842,737,3,111,282,85,271,866,584,900,257,542,51,805,744,89,834,255,494,997,657,903,205,82,257,588,744,747,878,151,898,648,520,661,74,219,952,786,498,187,760,607,11,958,280,311,739,453,219,57,587,491,907,801,35,507,742,842,649,717,173,571,286,908,679,206,714,747,300,843,679,80,286,5,65,62,744,936,705,520,110,550,808,705,514,36,507,762,10,707,608,757,303,821,223,954,375,89,701,901,384,883,45,801,587,61,146,487,276,110,299,453,147,459,577,108,381,568,76,25,786,35,933,62,199,746,358,459,10,205,805,931,627,487,648,769,559,893,455,193,30,737,921,27,57,907,931,301,23,187,463,299,821,26,635,473,573,7,646,243,377,305,465,677,672,19,962,45,650,627,53,559,50,193,24,969,432,900,408,923,707,579,7,199,810,778,243,697,35,737,705,745,874,145,552,381,18,762,507,255,16,232,772,16,451,498,271,183,899,933,491,582,360,375,463,568,880,26,501,308,19,108,74,511,111,525,772,50,436,80,18]
Case 31
basket1 = [204413166,399769545,376631821,261659678,271694957,490298275,710694835,236665043,684967225,103103368,178973194,271694957,497161629,132646811,964470223,145806757,132646811,145806757,865245929,710694835,109786489,399769545,147291252,535165287,471111796,361256153,109786489,147291252,376631821,430259519,430259519,684967225,109786489,351929627,490298275,204413166,109786489,204413166,261659678,936412071,972762243,377710945,350224990,345591940,399769545,494832087,471111796,471111796,103103368,361256153,399769545,204413166,710694835,535165287,972762243,178973194,399769545,376631821,361256153,471111796,103103368,936412071,471111796,535165287,471111796,705316843,358117554,236665043,147291252,341900437,351929627,361256153,147291252,350224990,376631821,471111796,109786489,145806757,430259519,757706502,350224990,351929627,132646811,147291252,490298275,684967225,710694835,865245929,132646811,204413166,976697298,351929627,145806757,865245929,341900437,159093467,497161629,399769545,109786489,345591940,299232528,287874411,287874411,399769545,361256153,351929627,178973194,109786489,109786489,535165287,976697298,376631821,936412071,132646811,358117554,287874411,376631821,299232528,145806757,361256153,972762243,494832087,345591940,261659678,490298275,490523901,351929627,147291252,341900437,109786489,109786489,147291252,361256153,535165287,261659678,350224990,535165287,936412071,471111796,430259519,103103368,287874411,261659678,471111796,367719763,490298275,350224990,705316843,287874411,710694835,976697298,261659678,178973194,376631821,865245929,271694957,936412071,494832087,490523901,299232528,350224990,147291252,345591940,430259519,710694835,361256153,271694957,361256153,497161629,497161629,471111796,178973194,271694957,494832087,399769545,757706502,705316843,204413166,261659678,710694835,350224990,399769545,490523901,290371050,299232528,145806757,535165287,351929627,377710945,287874411,109786489,290371050,350224990,399769545,705316843,964470223,865245929,299232528,271694957,430259519,299232528,490523901,399769545,261659678,710694835,936412071,204413166,377710945,109786489,972762243,936412071,430259519,271694957,367719763,535165287,103103368,376631821,361256153,287874411,471111796,299232528,471111796,377710945,287874411,103103368,497161629,535165287,710694835,705316843,345591940,287874411,351929627,964470223,103103368,204413166,705316843,710694835,377710945,271694957,345591940,350224990,377710945,710694835,376631821,377710945,490298275,341900437,972762243,361256153,287874411,705316843,299232528,132646811,261659678,147291252,376631821,351929627,147291252,236665043,535165287,261659678,345591940,358117554,399769545,299232528,430259519,964470223,132646811,287874411,358117554,430259519,236665043,147291252,497161629,287874411,271694957,490298275,710694835,865245929,351929627,361256153,103103368,535165287,271694957,109786489,287874411,964470223,710694835,972762243,757706502,361256153,350224990,290371050,287874411,494832087,358117554,299232528,345591940,497161629,341900437]
basket2 = [964470223,976697298,350224990,367719763,865245929,147291252,204413166,299232528,361256153,287874411,757706502,299232528,361256153,341900437,109786489,145806757,351929627,299232528,494832087,103103368,204413166,261659678,936412071,377710945,145806757,535165287,972762243,103103368,204413166,705316843,358117554,361256153,361256153,490523901,109786489,377710945,972762243,936412071,399769545,490298275,236665043,271694957,159093467,236665043,430259519,132646811,350224990,345591940,350224990,132646811,147291252,236665043,261659678,345591940,964470223,132646811,972762243,710694835,376631821,376631821,361256153,299232528,494832087,351929627,710694835,350224990,490523901,490523901,345591940,494832087,490298275,299232528,287874411,361256153,350224990,341900437,471111796,494832087,299232528,109786489,399769545,351929627,399769545,299232528,471111796,145806757,964470223,145806757,290371050,399769545,497161629,367719763,399769545,377710945,376631821,178973194,376631821,261659678,972762243,159093467,490298275,494832087,109786489,145806757,351929627,287874411,299232528,290371050,358117554,132646811,299232528,490523901,103103368,494832087,132646811,757706502,361256153,964470223,471111796,705316843,345591940,109786489,471111796,964470223,535165287,367719763,865245929,494832087,490298275,261659678,972762243,865245929,710694835,936412071,132646811,341900437,159093467,147291252,376631821,430259519,936412071,361256153,399769545,535165287,351929627,345591940,399769545,271694957,109786489,345591940,684967225,236665043,287874411,376631821,399769545,341900437,399769545,103103368,490298275,109786489,535165287,377710945,204413166,757706502,377710945,964470223,430259519,535165287,178973194,345591940,287874411,497161629,147291252,361256153,710694835,936412071,367719763,178973194,710694835,757706502,936412071,345591940,361256153,705316843,132646811,145806757,341900437,132646811,535165287,936412071,471111796,345591940,972762243,345591940,299232528,865245929,287874411,535165287,377710945,972762243,972762243,490298275,430259519,684967225,399769545,964470223,204413166,361256153,147291252,471111796,710694835,376631821,367719763,376631821,132646811,705316843,710694835,178973194,376631821,430259519,399769545,757706502,341900437,497161629,341900437,109786489,705316843,497161629,109786489,710694835,350224990,710694835,471111796,490298275,287874411,471111796,361256153,178973194,109786489,358117554,494832087,350224990,497161629,377710945,399769545,350224990,497161629,345591940,290371050,159093467,972762243,204413166,377710945,430259519,299232528,865245929,684967225,204413166,109786489,299232528,271694957,261659678,399769545,290371050,287874411,494832087,145806757,494832087,271694957,710694835,361256153,145806757,351929627,159093467,710694835,490523901,204413166,494832087,490523901,361256153,361256153,367719763,430259519,936412071,936412071,299232528,345591940,145806757,377710945,145806757,261659678,350224990,350224990,376631821,299232528,972762243,865245929,399769545,290371050,497161629]
Case 32
basket1 = [637953570,498732409,112855729,828900722,828900722,64746116,662827524,300900119,993672071,662827524,460653304,509543545,300900119,1,424325312,290782176,703952803,23836839,490069480,23836839,917609907,509543545,436212185,119221020,828900722,509543545,300900119,424325312,52020872,498732409,329941851,509543545,637953570,993672071,558594932,662827524,662827524,535012494,424325312,424325312,535012494,646370872,460653304,764795424,998128151,23836839,150716800,828900722,823654302,21710671,703952803,329941851,703952803,343178095,436212185,365180203,498732409,560502394,714178266,828900722,343178095,365180203,923628840,300900119,79515015,21710671,150716800,560502394,917609907,560502394,119221020,151714960,506472613,343178095,959109664,254334569,823654302,112855729,998128151,643132608,40209941,79515015,959109664,424325312,490069480,823654302,942698775,506472613,47343786,942698775,823654302,942698775,23836839,79515015,714178266,119221020,21710671,628532032,290782176,923628840,509543545,942698775,240241397,40209941,79515015,959109664,424325312,506472613,473150406,468344761,704575382,146428948,804460796,823654302,57316448,637953570,509543545,643132608,828900722,240241397,329941851,151714960,509543545,150716800,558594932,993672071,560502394,742246955,920996538,942698775,343178095,460653304,112855729,460653304,828900722,79515015,535012494,79515015,490069480,906350322,906350322,628532032,424325312,742246955,460653304,64746116,343178095,365180203,300900119,473150406,490069480,959109664,1,704575382,52020872,498732409,473150406,942698775,509543545,240241397,923628840,243829074,150716800,558594932,942698775,703952803,646370872,628532032,917609907,646370872,300900119,498732409,628532032,343178095,959109664,150716800,509543545,79515015,923628840,917609907,637953570,856009945,460653304,628532032,959109664,923628840,436212185,643132608,498732409,146428948,823654302,993672071,637953570,64746116,52020872,57316448,509543545,628532032,243829074,828900722]
basket2 = [119221020,300900119,490069480,473150406,704575382,146428948,119221020,47343786,560502394,942698775,509543545,172127967,57316448,146428948,240241397,637953570,172127967,146428948,290782176,714178266,300900119,424325312,329941851,704575382,254334569,468344761,365180203,40209941,300900119,823654302,856009945,959109664,47343786,923628840,558594932,709922062,923628840,509543545,742246955,923628840,628532032,119221020,21710671,329941851,804460796,254334569,146428948,920996538,942698775,79515015,804460796,646370872,344105253,64746116,424325312,804460796,923628840,468344761,344105253,714178266,473150406,959109664,151714960,662827524,804460796,993672071,151714960,460653304,662827524,828900722,714178266,468344761,703952803,560502394,704575382,509543545,959109664,424325312,535012494,704575382,473150406,560502394,290782176,959109664,714178266,424325312,150716800,643132608,558594932,240241397,637953570,643132608,240241397,1,560502394,637953570,112855729,828900722,509543545,959109664,823654302,714178266,709922062,490069480,920996538,704575382,290782176,535012494,151714960,150716800,329941851,628532032,424325312,506472613,823654302,804460796,146428948,509543545,150716800,804460796,993672071,643132608,150716800,436212185,742246955,490069480,856009945,558594932,151714960,365180203,52020872,240241397,959109664,490069480,704575382,923628840,742246955,290782176,856009945,343178095,535012494,498732409,240241397,506472613,764795424,643132608,704575382,920996538,742246955,815716650,473150406,815716650,643132608,343178095,490069480,823654302,468344761,23836839,917609907,300900119,560502394,300900119,172127967,509543545,47343786,704575382,300900119,942698775,57316448,535012494,506472613,490069480,509543545,509543545,920996538,920996538,150716800,40209941,560502394,460653304,703952803,498732409,535012494,468344761,365180203,79515015,917609907,23836839,804460796,79515015,343178095,1,942698775,343178095,254334569,146428948,172127967,714178266,473150406,942698775]
Case 33
basket1 = [8,6,44,31,25,7,13,24,40,12,27,31,16,41,4,46,1,30,19,6,28,9,25,20,38,36,34,17,17,9,21,12,30,4,21,9,16,26,37,16,35,35,1,46,8,19,11,10,31,28,17,6,19,40,3,21,31,4,3,40,9,49,35,20,9,33,36,24,27,3,15,25,6,7,33,24,25,11,21,8,8,8,15,26,43,49,47,47,20,32,8,4,20,36,8,21,40,8,6,35]
basket2 = [47,14,35,27,24,20,33,15,20,13,26,14,17,40,28,12,24,31,37,8,49,47,36,28,33,6,34,49,6,42,6,42,31,1,36,49,40,8,40,13,18,21,40,46,26,3,44,49,43,33,35,21,42,32,40,24,3,19,24,24,27,24,45,26,24,45,20,42,21,20,38,37,1,40,40,37,47,12,41,16,3,18,43,21,26,12,40,36,8,43,8,9,3,15,21,12,33,13,3,10]