Dry Run Image Explanation ✅🔥 || Sorting Greedy || C++
Intuition\n Describe your first thoughts on how to solve this problem. \n1. Make a vector of pairs and store difference,index in each pair.\n2. Sort the vector 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Make a vector of pairs and store difference,index in each pair.\n2. Sort the vector in descending order such that maximum values are taken in the sum.\n3. Iterate till <k in this vector arr in which the x.second of pair stores the index of reward1. Add reward1[index] in sum.\n4. Iterate from k till <n in this vector arr in which the x.second of pair stores the index of reward2. Add reward2[index] in sum.\n\n\n![leetcode C 339.jpg](https://assets.leetcode.com/users/images/818e3ce0-50c6-488b-9df1-1ad5167d014f_1680408537.6465707.jpeg)\n\n# Code\n```\nclass Solution {\npublic:\n    \n    int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {\n        int sum = 0, n = reward1.size();\n        vector<pair<int,int>> arr;\n        \n        for(int i=0;i<n;i++)\n        {\n            int diff = reward1[i]-reward2[i];\n            arr.push_back({diff,i}); //storing pair of difference, index\n        }\n        \n        sort(arr.begin(),arr.end());\n        reverse(arr.begin(),arr.end()); // sorted in descending order\n        \n        int i;\n        for(i=0;i<k;i++) //add k values from reward1\n        {\n            auto x = arr[i];\n            int index = x.second;\n            sum += reward1[index];\n        }\n        \n        for(;i<n;i++) // add remaining values from reward2\n        {\n            auto x = arr[i];\n            int index = x.second;\n            sum += reward2[index];\n        }\n        \n        return sum;\n    }\n};\n```
['Greedy', 'Sorting', 'C++']