Imagine you're the chief investment officer at a company with a hierarchical structure where the CEO (employee 1) is at the top, and every other employee reports to someone up the chain. You have a unique opportunity to invest in employee stocks with an interesting twist!
You're given:
nemployees, each with ID from 1 to npresent[i]: current price to buy employee i's stock todayfuture[i]: expected selling price tomorrowhierarchy: boss-employee relationshipsbudget: total investment funds
The Discount Rule: If a boss buys their own stock, all their direct reports get a 50% discount on their stock price! ๐
Your goal is to maximize profit by strategically choosing which stocks to buy, considering both the discount opportunities and profit potential. Each stock can only be purchased once, and you must stay within budget.
Example: If Boss A (price $100, profit $20) has Employee B (price $80, profit $50), buying Boss A's stock makes Employee B's stock cost only $40, potentially increasing overall returns!
Input & Output
Visualization
Time & Space Complexity
Each node-budget combination is computed once due to memoization
Memoization table stores results for all node-budget pairs
Constraints
- 1 โค n โค 1000
- 1 โค present[i], future[i] โค 104
- 1 โค budget โค 106
- hierarchy.length = n - 1
- Each employee has at most one direct boss
- The hierarchy forms a valid tree structure
- Employee 1 is always the root (CEO)