class Solution { public: int findMaximizedCapital(int k, int w, vector& profits, vector& capital) { vector capi(capital.size()); iota(capi.begin(), capi.end(), 0); sort(capi.begin(), capi.end(), [&](int i, int j) { return capital[i] < capital[j]; }); priority_queue pq; for (int i = 0; k--;) { while (i < capi.size() && w >= capital[capi[i]]) pq.push(profits[capi[i++]]); if (pq.size()) { w += pq.top(); pq.pop(); } } return w; } };