Sort Deltas
We use an index array, and we sort it by r1[i] - r2[i], descending.\n\nThen, we take k values from the first array, and the rest - from the second one.\n\nC++\n
We use an index array, and we sort it by `r1[i] - r2[i]`, descending.\n\nThen, we take `k` values from the first array, and the rest - from the second one.\n\n**C++**\n```cpp\nint miceAndCheese(vector<int>& r1, vector<int>& r2, int k) {\n    vector<int> idx(r1.size());\n    iota(begin(idx), end(idx), 0);\n    nth_element(begin(idx), begin(idx) + k, end(idx), [&](int i, int j){\n        return r1[i] - r2[i] > r1[j] - r2[j];\n    });\n    return accumulate(begin(idx), begin(idx) + k, 0, [&](int s, int i) { return s + r1[i]; }) +\n        accumulate(begin(idx) + k, end(idx), 0, [&](int s, int i) { return s + r2[i]; });\n}\n```\n**C++**\nA nice alternative proposed by [0xFFFFFFFF](https://leetcode.com/0xFFFFFFFF/).\n```cpp\nint miceAndCheese(vector<int>& r1, vector<int>& r2, int k) {\n    transform(begin(r1), end(r1), begin(r2), begin(r1), minus<int>());\n    nth_element(begin(r1), begin(r1) + k, end(r1), greater<int>());\n    return accumulate(begin(r1), begin(r1) + k, \n        accumulate(begin(r2), end(r2), 0));\n}\n```
[]