Practice › Heap & Priority Queue
Maximize Capital (IPO)
Hard · Heap & Priority Queue · Asked at Amazon, Google, Microsoft
You start with capital w and may complete at most k distinct projects. Project i can be started only if your current capital is at least c[i]; finishing it adds its profit p[i] to your capital (the capital you need is not spent). Print the maximum capital you can end with.
Input
First line: n k w. Second line: the n profits. Third line: the n required capitals.
Output
The maximum final capital.
Constraints
- 1 ≤ n, k ≤ 10^5
- 0 ≤ w ≤ 10^9
- 0 ≤ p[i] ≤ 10^4
- 0 ≤ c[i] ≤ 10^9
Sort projects by required capital. Each round, push every project you can now afford into a max-heap of profits and take the top. Capital only grows, so a project once affordable stays affordable. Stop early if the heap is empty.
Example 1
Input
3 2 0 1 2 3 0 1 1
Output
4
Example 2
Input
3 3 0 1 2 3 0 1 2
Output
6
Hints
- Among the projects you can afford right now, the best choice is simply the most profitable one.
- Capital never decreases, so the set of affordable projects only grows.
- Sort by required capital and feed newly affordable projects into a max-heap of profits.
Approach
Sort projects by capital. Repeat k times: move every project with c ≤ w from the sorted list into a max-heap of profits, then take the largest profit (if the heap is empty, nothing more can be started — stop). Taking the largest available profit is never worse, because a bigger capital unlocks a superset of projects. The answer can exceed 2^31, so use 64-bit integers.
Complexity: O(n log n) time · O(n) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
long long w;
cin >> n >> k >> w;
vector<pair<long long, long long>> proj(n); // (capital, profit)
for (auto &p : proj) cin >> p.second;
for (auto &p : proj) cin >> p.first;
sort(proj.begin(), proj.end());
priority_queue<long long> pq;
int j = 0;
for (int t = 0; t < k; t++) {
while (j < n && proj[j].first <= w) pq.push(proj[j++].second);
if (pq.empty()) break;
w += pq.top();
pq.pop();
}
cout << w << "\n";
return 0;
}Python solution
import heapq
import sys
data = list(map(int, sys.stdin.read().split()))
n, k, w = data[0], data[1], data[2]
profits = data[3 : 3 + n]
capital = data[3 + n : 3 + 2 * n]
proj = sorted(zip(capital, profits))
heap = []
j = 0
for _ in range(k):
while j < n and proj[j][0] <= w:
heapq.heappush(heap, -proj[j][1])
j += 1
if not heap:
break
w -= heapq.heappop(heap)
print(w)Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
in.nextToken();
int k = (int) in.nval;
in.nextToken();
long w = (long) in.nval;
long[][] proj = new long[n][2]; // capital, profit
for (int i = 0; i < n; i++) {
in.nextToken();
proj[i][1] = (long) in.nval;
}
for (int i = 0; i < n; i++) {
in.nextToken();
proj[i][0] = (long) in.nval;
}
Arrays.sort(proj, (a, b) -> Long.compare(a[0], b[0]));
PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
int j = 0;
for (int t = 0; t < k; t++) {
while (j < n && proj[j][0] <= w) pq.add(proj[j++][1]);
if (pq.isEmpty()) break;
w += pq.poll();
}
System.out.println(w);
}
}