import java.util.*;
class Solution {
public int[] solution(String[] enroll, String[] referral, String[] seller, int[] amount) {
int[] answer = {};
final int brushPrice = 100;
Map<String, String> centerTree = new HashMap<>();
Map<String, Integer> profitMap = new HashMap<>();
//initailize
profitMap.put("-", 0);
for(int i=0; i<enroll.length; i++) {
if("-".equals(referral[i])) {
centerTree.put(enroll[i], "-");
} else {
centerTree.put(enroll[i], referral[i]);
}
profitMap.put(enroll[i], 0);
}
for(int i=0; i<seller.length; i++) {
int profit = brushPrice * amount[i];
String curSellerName = seller[i];
while(centerTree.containsKey(curSellerName)) {
int sharedProfit = getSharedProfit(profit);
profitMap.put(curSellerName, (profitMap.get(curSellerName) + profit - sharedProfit));
if(sharedProfit == 0) break;
profit = sharedProfit;
curSellerName = centerTree.get(curSellerName);
}
profitMap.put("-", profitMap.get("-") + profit);
}
answer = new int[enroll.length];
for(int i=0; i<enroll.length; i++) {
System.out.println(profitMap.get(enroll[i]));
answer[i] = profitMap.get(enroll[i]);
}
return answer;
}
public Integer getSharedProfit(Integer profit) {
int profitDist = profit / 10;
if(profitDist < 1) {
return 0;
}
return profitDist;
}
}