[Java/C++/Python] K Largest A[i] - B[i]
Explanation\nAssume take all from the second array.\nCheck the difference sequence:\nA[0] - B[0], A[1] - B[1], ...\n\nTake k largest from the sequence and sum u
# **Explanation**\nAssume take all from the second array.\nCheck the difference sequence:\n`A[0] - B[0], A[1] - B[1], ...`\n\nTake `k` largest from the sequence and sum up.\n\nReturn the `res = sum(B) + sum(k largest A[i]-B[i])`\n<br>\n\n# **Complexity**\nJava uses sort\nTime `O(nlog)` Space `O(n)`\n\nPython uses heap\nTime `O(klogk)` Space `O(k)`\n\nC++ uses quick select would be\nTime `O(n)` Space `O(n)`\n<br>\n\n\n**Java**\n```java\n    public int miceAndCheese(int[] A, int[] B, int k) {\n        int res = 0, n = A.length, diff[] = new int[n];\n        for (int i = 0; i < n; i++) {\n            diff[i] = A[i] - B[i];\n            res += B[i];\n        }\n        Arrays.sort(diff);\n        for (int i = 0; i < k; i++)\n            res += diff[n - 1 - i];\n        return res;\n    }\n```\n**C++**\n```cpp\n    int miceAndCheese(vector<int> A, vector<int>& B, int k) {\n        int n = A.size(), res = 0;\n        for (int i = 0; i < n; ++i) {\n            A[i] -= B[i];\n            res += B[i];\n        }\n        nth_element(A.begin(), A.begin() + k, A.end(), greater<int>());\n        return accumulate(A.begin(), A.begin() + k, res);\n    }\n```\n**Python**\n```py\n    def miceAndCheese(self, A: List[int], B: List[int], k: int) -> int:\n        return sum(B) + sum(nlargest(k, (a - b for a, b in zip(A, B))))\n```\n
['C', 'Python', 'Java']