There is a hierarchical structure in an organization. A party is to be organized. No two immediate subordinates can come to the party. A profit is associated with every person. You have to maximize the total profit of all the persons who come to the party.

**Hierarchical Structure**

In a hierarchical organization, all employees(except the one at the head) are subordinates of some other employee.

Employees only directly report to their immediate superior. This allows for a flexible and efficient structure.

For purposes of this problem, this structure may be imagined as a tree, with each employee as its node.

Examples:

Input: 15 / \ 10 12 / \ / \ 26 4 7 9 Output: The Maximum Profit would be 15+12+26+9 = 62 The Parent 15 chooses sub-ordinate 12, 10 chooses 26 and 12 chooses 9. Input: 12 / | \ 9 25 16 / / \ 13 13 9 Output: 12+25+13+13 = 63

**Approach:**

Given the profit from each employee, we have to find the maximum sum such that no two employees(Nodes) with the same superior(Parent) are invited. This can be achieved if each employee selects the subordinate with the maximum contribution to go.

In the program, the hierarchy of the company is implemented in the form of a dictionary, with the key being a unique employee ID, and data being an array of the form [Profit associated with this employ, [List of immediate Sub-ordinates]].

For each Employee, the subordinate with the highest profit associated is added to the total profit. Further, the Employee at the head is always invited.

`def` `getMaxProfit(hier): ` ` ` `# The head has no competition and therefore invited ` ` ` `totSum ` `=` `hier[` `1` `][` `0` `] ` ` ` `for` `i ` `in` `hier: ` ` ` `currentSuperior ` `=` `hier[i] ` ` ` `selectedSub ` `=` `0` ` ` `# select the subordinate with maximum ` ` ` `# value of each superior ` ` ` `for` `j ` `in` `currentSuperior[` `1` `]: ` ` ` `if` `(hier[j][` `0` `] > selectedSub): ` ` ` `selectedSub ` `=` `hier[j][` `0` `] ` ` ` `totSum ` `+` `=` `selectedSub ` ` ` `return` `totSum ` ` ` `# driver function ` `def` `main(): ` ` ` `# Define the Organization as a Dictionary ` ` ` `# Index : [Profit, List of Employees] ` ` ` `# Same as example 1 given above ` `# 1:15 ` `# / \ ` `# 2:10 3:12 ` `# / \ / \ ` `# 4:26 5:4 6:7 7:9 ` ` ` ` ` `organization ` `=` `{` `1` `:[` `15` `, [` `2` `, ` `3` `]], ` ` ` `2` `:[` `10` `, [` `4` `, ` `5` `]], ` `3` `:[` `12` `, [` `6` `, ` `7` `]], ` ` ` `4` `:[` `26` `, []], ` `5` `:[` `4` `, []], ` `6` `:[` `7` `, []], ` `7` `:[` `9` `, []]} ` ` ` `maxProfit ` `=` `getMaxProfit(organization) ` ` ` `print` `(maxProfit) ` ` ` `main() ` |

*chevron_right*

*filter_none*

**Output:**

62

## Recommended Posts:

- Maximum profit such that total stolen value is less than K to get bonus
- Stock Buy Sell to Maximize Profit
- Maximize profit when divisibility by two numbers have associated profits
- Maximize profit in buying and selling stocks with Rest condition
- Maximize Profit by trading stocks based on given rate per day
- Probability that two persons will meet
- Minimum cost to buy N kilograms of sweet for M persons
- Maximum number of teams that can be formed with given persons
- Maximum profit by selling N items at two markets
- Total number of unit cells covered by all given Rectangles
- Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Minimum total cost incurred to reach the last station
- Count the total number of squares that can be visited by Bishop in one move
- Total position where king can reach on a chessboard in exactly M moves
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Schedule elevator to reduce the total time taken
- Total number of triangles formed when there are H horizontal and V vertical lines
- Total number of possible Binary Search Trees using Catalan Number
- How to calculate total time of an array in PHP ?

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.