C++ Python Java 5 liners
\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1)\n\n# C++\n\nclass Solution {\npublic:\n    int miceAndCheese(vector<int>& reward1, vec
\n# Complexity\n- Time complexity:\n`O(nlogn)`\n\n- Space complexity:\n`O(1)`\n\n# C++\n```\nclass Solution {\npublic:\n    int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {\n        //if mice2 ate all cheese\n        int ans = accumulate(reward2.begin(), reward2.end(), 0);\n\n        //change in reward of each cheese if mice1 ate it instead of mice2\n        //store the difference in reward1\n        for(int i=0; i<reward1.size(); i++) \n            reward1[i] -= reward2[i];\n\n        //sort it in descending order\n        //maximized difference will be first\n        sort(reward1.begin(),reward1.end(),greater<int>());\n\n        //take top k elements (i.e mice1 eats k cheese instead of mice2)\n        ans += accumulate(reward1.begin(), reward1.begin()+k, 0);\n\n        return ans;\n    }\n};\n```\n\n# Python\n```\nclass Solution(object):\n    def miceAndCheese(self, reward1, reward2, k):\n        //if mice2 ate all cheese\n        ans = sum(reward2)\n\n        //change in reward of each cheese if mice1 ate it instead of mice2\n        //store the difference in reward1\n        reward1 = [(x - y) for x, y in zip(reward1, reward2)]\n        \n        //sort it in descending order\n        //maximized difference will be first\n        reward1.sort(reverse=True)\n\n        //take top k elements (i.e mice1 eats k cheese instead of mice2)\n        ans +=  sum(reward1[:k])\n\n        return ans\n```\n# Java\n```\nclass Solution {\n    public static int miceAndCheese(int[] reward1, int[] reward2, int k) {\n        //if mice2 ate all cheese\n        int ans = Arrays.stream(reward2).sum();\n\n        //change in reward of each cheese if mice1 ate it instead of mice2\n        //store the difference in reward1\n        for(int i = 0; i < reward1.length; i++) {\n            reward1[i] -= reward2[i];\n        }\n\n        //sort it in descending order\n        //maximized difference will be at last\n        Arrays.sort(reward1);\n\n        //take last k elements (i.e mice1 eats k cheese instead of mice2)\n        int i = reward1.length-1;\n        while(k-- > 0)\n            ans += reward1[i--];\n\n        return ans;\n    }\n}\n```\n\n
['Sorting', 'Python', 'C++', 'Java']