Santa infinite number candy m flavour you give rooted tree n vertex the root tree vertex 1 each vertex contain exactly candy the ith vertex candy flavour f_iSometimes Santa fear candy flavour k melt he choose vertex x randomly send subtree x baker replacement in replacement candy flavour k replace new candy flavour the candy flavour k leave unchanged after replacement tree restoredthe actual cost replace candy flavour k c_k give k the Baker keep price fix order calculation simple every time subtree come replacement Baker charge C matter subtree flavour issuppose give flavour k probability Santa choose vertex replacement vertex you need find expect value error calculate cost replacement flavour k the error calculate cost define follow Error Ek actual cost Price charge Bakers 2note actual cost cost replacement candy flavour k multiplied number candy subtreealso Santa wish replace candy vertex x candy flavour pocketyou need handle type operation change flavour candy vertex x w Calculate expect value error calculate cost replacement give flavour k