You are running a multi-level marketing where you and the people in your network are pyramid selling toothbrushes. If someone in the network sells a toothbrush, the profit gets distributed to the people in this network. After some sales, you want to figure out which people in your network earned how much profit. Suppose that your multi-level marketing network is as follows:


center is you, and other blue rectangles indicate eight other workers in your network. This pyramid structure is a chain of referrals. The compensation rule is simple: Every salesperson distributes 10% of the sales profit to her recruiter and takes the rest of the sales profit. This is to say that every salesperson not only gets compensated for selling toothbrushes but also takes 10% of the profit that each person that she has recruited makes. If someone else has recruited that salesperson, then 10% of the profit that that salesperson makes goes to that recruiter. When calculating 10% of the profit, round down to the nearest dollar. If it is smaller than 1 dollar, you take all of it instead of distributing it.

Suppose that the sales history is as follows, where the profit for selling one toothbrush is 100 dollars:

Salesperson Sales amount Profit
young 12 1,200 dollars
john 4 400 dollars
tod 2 200 dollars
emily 5 500 dollars
mary 10 1,000 dollars

Salesperson young has made a profit of 1,200 dollars. young gives the 120 dollars—the 10% commission—to her recruiter, edward, and keeps 1,080 dollars for herself. edward gives mary, his recruiter, 12 dollars—the 10% commission of the 120 dollars he had received from young—and keeps 108 dollars for himself. mary gives 1 dollar—the 10% of 12 dollars that she had received from edward—to you, center, and keeps 11 dollars for herself. All of this can be illustrated as follows:


john also made 400 dollars of profit. john gives 40 dollars—the 10% commission—to center and keeps the rest—360 dollars. This can be illustrated as follows:


Another salesperson, tod, has also made 200 dollars of profit. tod keeps 180 dollars for himself and pays his recruiter jaimie the 10% commission. jaime receives 20 dollars from tod, from which she keeps 18 dollars for herself and pays mary two dollars for the 10% commission. Since the 10% of two dollars rounded down to the nearest dollar is 0, mary can keep the two dollars to herself. This can be illustrated as follows:


emily has made 500 dollars of profit. According to the compensation rule, she gets to keep 450 dollars, mary 45 dollars, and center 5 dollars from it. This can be illustrated as follows:


Lastly, mary has made 1,000 dollars of profit. She gets to keep 900 dollars from it and pays center 100 dollars. This can be illustrated as follows:


And here is the final result:


This is the result that you wanted to find out.

Suppose parameters enroll, referral, seller, and amount are given, where enroll is an array containing names of the salespersons, referral an array containing names of recruiters that have recruited other salespersons, seller an array containing names of salespersons in the sales history, and amount an array containing the sales history. Please write a function solution that returns an array containing the total amount of compensation that each salesperson has received. Sort them (in integers) by the order in which the names are listed in enroll.

  • The length of enroll is between 1 and 10,000.
    • Your name won't be in found enroll. Therefore, the length of enroll is equal to the total number of people in your marketing network except you.
  • The length of referral is equal to that of enroll.
    • The ith name in referral is the name of the person who recruited the i th salesperson in enroll.
    • For those who have been recruited by you, their recruiter name will be displayed as “-“ in referral. In the example above, such is the case for john and mary.
    • The names contained in enroll are sorted by the order they joined the network.
    • For example, if someone's name is the ith element of enroll, then her recruiter—the ith element of referral—will be the jth element of enroll.
  • The length of seller is between 1 and 100,000.
    • The ith name in seller indicates that this person has made the ith entry in the sale history.
    • There can be overlapping names in seller.
  • The length of amount is equal to that of seller.
    • The ith number in amount means the sales amount of the ith entry in the sale history.
    • The elements of amount—the range of sales amount—are natural numbers between 1 and 100.
  • Each salesperson will make 100 dollars of profit for selling one toothbrush.
  • Everyone's name is made up of less than 10 lowercase letters.
enroll referral seller amount result
["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] ["young", "john", "tod", "emily", "mary"] [12, 4, 2, 5, 10] [360, 958, 108, 0, 450, 18, 180, 1080]
["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] ["sam", "emily", "jaimie", "edward"] [2, 3, 5, 4] [0, 110, 378, 180, 270, 450, 0, 0]

Example #1

Demonstrated in the prompt above.

Example #2

Things have been slightly altered from Example 1. Since the compensation rules are the same, you can do a simple calculation to come up with the result shown in the table.

Result Stop
Result of [Run] or [Submit] will be displayed here