My first instinct was just to sort by the gain from doubling, which is the right direction, but I fumbled on figuring out the actual gain formula.
First, recognize that doubling b[i] only increases the sum if b[i] < a[i], and the gain is a[i] - b[i]. Compute these gains for all indices, select up to k indices with the largest positive gains, and sum the original min(a[i], b[i]) plus these gains. This greedy strategy is optimal because each index's gain is independent and we can choose any subset of size up to k.
Pro tip: Mention that if k is larger than the number of positive gains, you simply take all positive gains; doubling indices with non-positive gain would decrease or not change the sum, so avoid them. Also, note that the solution runs in O(n log n) time due to sorting, which is efficient for large inputs.
For each index i, determine how doubling b[i] changes the contribution to the sum. The new contribution becomes min(a[i], 2*b[i]), so the gain is min(a[i], 2*b[i]) - min(a[i], b[i]).
Show that the gain is positive only when b[i] < a[i], and in that case the gain equals a[i] - b[i]. If b[i] >= a[i], doubling b[i] does not change the contribution (gain = 0).
Calculate the gain for each index, filter out non-positive gains, and sort the positive gains in descending order.
Take the top min(k, number of positive gains) gains and add them to the base sum of min(a[i], b[i]) over all indices.
Explain that since each index's gain is independent and we can choose any subset of size up to k, the greedy choice of the largest gains maximizes the total sum.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.