<p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>

<p>You can do the following operation <strong>any</strong> number of times:</p>

<ul>
	<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
</ul>

<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>

<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
<strong>Output:</strong> 8
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
- Increase the 0<sup>th</sup> element one time. The cost is 2.
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
The total cost is 2 + 3 + 3 = 8.
It can be shown that we cannot make the array equal with a smaller cost.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length == cost.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], cost[i] &lt;= 10<sup>6</sup></code></li>
	<li>Test cases are generated in a way that the output doesn&#39;t exceed&nbsp;2<sup>53</sup>-1</li>
</ul>

class Solution {
public:
    long long minCost(vector<int>& nums, vector<int>& cost) {
        // 使用 sort 並基於 nums 來排序 cost
        std::vector<int> indices(cost.size());
        // 初始化 indices 為 0, 1, 2, 3...
        for (int i = 0; i < indices.size(); ++i) {
            indices[i] = i;
        }

        // 根據 nums 的值來對 indices 進行排序
        std::sort(indices.begin(), indices.end(), [&nums](int i, int j) {
            return nums[i] < nums[j];
        });

        // 根據排序後的 indices 對 cost 進行重排
        std::vector<int> sorted_cost(cost.size());
        for (int i = 0; i < indices.size(); ++i) {
            sorted_cost[i] = cost[indices[i]];
        }

        // cout << "cost:";
        // for(auto c: sorted_cost) {
        //     cout << c << " ";
        // }
        // cout << endl;

        sort(nums.begin(), nums.end());
        // cout << "nums:";
        // for(auto n: nums) {
        //     cout << n << " ";
        // }
        // cout << endl;

        int l = 1, r = nums[nums.size()-1];
        int mid = (l+r) / 2;
        long long ans = calCost(nums, sorted_cost, r);
        // cout << "target: " << r << endl;
        // cout << "Cost: " << ans << endl;

        while(l <= r) {
            mid = (l+r) / 2;
            long long nowCost = calCost(nums, sorted_cost, mid);
            // cout << "target: " << mid << endl;
            // cout << "Cost: " << nowCost << endl;
            if(l == r) return nowCost;

            long long leftCost = calCost(nums, sorted_cost, mid-1);
            long long rightCost = calCost(nums, sorted_cost, mid+1);
            if(leftCost >= nowCost && rightCost >= nowCost) return nowCost;

            if(rightCost < nowCost) {
                l = mid + 1;
            } else { //nowCost <= ans
                r = mid - 1;
                ans = nowCost;
            }
        }

        // iterate answer
        // long long ans = calCost(nums, cost, nums[0]);
        // for(auto n: nums) {
        //     long long nowCost = calCost(nums, cost, n);
        //     cout << "nowCost:" << nowCost << ", target:" << n << endl;
        //     ans = min(ans, nowCost);
        // }

        return ans;
    }

    long long calCost(vector<int> &nums, vector<int> &cost, int target) {
        
        long long totalCost = 0;
        for(int i=0;i<nums.size();i++) {
            long long costNow = target - nums[i];
            if(costNow < 0) costNow *= -1;
            // cout << "cost+=" << costNow * cost[i] << endl;
            totalCost += costNow * cost[i];
        }
        return totalCost;
    }
};
44100