I have the following problem: I am given a tree with N apples, for each apple I am given it’s weight and height. I can pick apples up to a given height H, each time I pick an apple the height of every apple is increased with U. I have to find out the maximum weight of apples I can pick.
1 ≤ N ≤ 100000
0 < {H, U, apples’ weight and height, maximum weight} < 231
Example:
N=4 H=100 U=10
height weight
82 30
91 10
93 5
94 15
The answer is 45: first pick the apple with the weight of 15 then the one with the weight of 30.
Could someone help me approach this problem?
Work backwards. Start by deciding the last apple you will pick, then the second to last, etc.
Simple test:
Someone requested C++, so here it is. It’s nearly identical code and logic to the above Python, except for one change: C++ stdlib’s heap functions work with the max value instead of the min, so no need for the negation. (I kept this self-contained, but utilities such as a heap adapter and container inserter will make the code easier to use.)
C++ tests: