All problems

Problem 596

IPO

Hard · 30 pts · Greedy, Heap, Sorting

Problem

You start with capital W and may complete at most K distinct projects, one after another. Project i needs at least capital[i] to start and, when finished, adds profit[i] to your capital (the capital requirement is not spent). Choose the projects to maximise your final capital. Greedy works: sort the projects by required capital, and before each step push every project you can now afford into a max-heap of profits; then take the most profitable one.

Input

The first line contains N, K and W. The second line contains the N profits. The third line contains the N capital requirements.

Output

Print the maximum final capital.

Constraints

1 <= N <= 100000; 1 <= K <= 100000; 0 <= W <= 10^9; 0 <= profit[i] <= 10^4; 0 <= capital[i] <= 10^9

Example input

3 2 0
1 2 3
0 1 1

Example output

4

Explanation

With W = 0 and K = 2, only project 0 (profit 1, capital 0) is affordable, so do it and reach 1. Now projects 1 and 2 are affordable; project 2 gives 3, so the final capital is 1 + 3 = 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: LFU Cache Next: Furthest Building You Can Reach