# 2611. Mice and Cheese ###### tags: `Leetcode` `Medium` Link: https://leetcode.com/problems/mice-and-cheese/description/ ## 思路 Assume take all from the second array. Check the difference sequence: A[0] - B[0], A[1] - B[1], ... Take k largest from the sequence and sum up. Return the res = sum(B) + sum(k largest A[i]-B[i]) Python:O(klogk) Java:O(nlogn) ## Code ```java= class Solution { public int miceAndCheese(int[] reward1, int[] reward2, int k) { int ans = 0; int n = reward1.length; int[] diff = new int[n]; for(int i=0; i<n; i++){ diff[i] = reward1[i]-reward2[i]; ans += reward2[i]; } Arrays.sort(diff); for(int i=0; i<k; i++){ ans += diff[n-i-1]; } return ans; } } ``` ```python= class Solution: def miceAndCheese(self, reward1: List[int], reward2: List[int], k: int) -> int: return sum(reward2)+sum(heapq.nlargest(k, (a-b for a,b in zip(reward1, reward2)))) ```