A regional drone-delivery hub runs n simultaneous rendezvous slots between its delivery drones and its charging pads. The drone that launches in slot i is always paired with the pad that activates in slot i, and that pairing burns cargoWeight * powerMultiplier units of energy, where cargoWeight is the carried weight of the drone in that slot and powerMultiplier is the draw multiplier of the pad in that slot. The dispatcher may choose the launch order of the drones and, independently, the activation order of the pads -- any permutation of either list is allowed before the slots are fixed. Once both orders are chosen, the total energy burned is the sum of the n slot-by-slot products. Determine the minimum total energy the dispatcher can achieve over every possible choice of the two orderings.
The first line contains a single integer n, the number of drones (equal to the number of pads). The second line contains n space-separated integers w_1 ... w_n, the cargo weight of each drone. The third line contains n space-separated integers m_1 ... m_n, the power multiplier of each charging pad.
Print a single integer: the minimum possible total energy over all reorderings of the two sequences.
Example 1
Input
3 5 3 4 2 1 6
Expected
31
Explanation
Sort the weights ascending to [3, 4, 5] and the multipliers descending to [6, 2, 1], then pair position by position: 3*6 + 4*2 + 5*1 = 18 + 8 + 5 = 31, which is the minimum achievable total energy.
Example 2
Input
4 -2 3 -5 1 4 -3 2 0
Expected
-33
Explanation
Sort the weights ascending to [-5, -2, 1, 3] and the multipliers descending to [4, 2, 0, -3]. The paired products are -5*4=-20, -2*2=-4, 1*0=0, 3*(-3)=-9, summing to -33, the minimum total.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →