Lucky Stars Management
Time limit2sMemory limit1024 MB
Given a company tree and expected fines E_i, decide whether the values are consistent and, if so, find Bill's minimum possible annual salary.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Dynamic programming, Math
- Solved
- No attempts yet
Problem
The company Lucky Stars has employees, numbered from 1 to in order of their position in the company. Bill, the President, has employee ID 1. Every employee except Bill has exactly one direct supervisor. The direct supervisor of employee is . The annual income of each employee is an integer between and .
One day Bill consulted a fortune-teller who is always right about the future. The fortune-teller said: "Someday, mistakes will be made, and Lucky Stars management will become difficult."
Each mistake is caused by exactly one employee. For , the probability that a mistake is caused by employee is . Mistakes are independent, so one employee can cause several mistakes.
Bill decided to enforce personal responsibility in case the fortune-teller is right. When types of mistakes happen in one day, Bill wants to choose one employee to pay a fine. The employee is chosen by these rules.
- When an employee makes a mistake, that employee and every superior above them (the direct supervisor, the supervisor's supervisor, and so on, up to Bill) are responsible.
- Among the employees responsible for all types of mistakes, the employee with the largest number pays the fine.
- If is the annual salary of the employee who made the -th mistake for , the fine is .
Bill calculated the expected fine for employee , assuming the fortune-teller is right, and reported it modulo . For , write the expected fine as , where and are coprime integers and is not divisible by . The answer is div modulo .
Now you are given the company structure ( for each employee) and the expected fines that Bill calculated. Decide whether all the given information could be correct. If so, find the minimum possible annual salary of Bill.
Input
The first line contains two integers () and (, is odd). The second line contains integers (), the direct supervisor of each employee . If , the second line is empty. The third line contains integers (), the calculated expected fines of the employees.
Output
If the calculated values are definitely wrong, print . Otherwise, print one integer: the minimum possible annual salary of Bill.