tickets_loj
Buying Tickets
This summer, NOI celebrates her 30th birthday in the city of SZ. OIers will travel in from cities all across the country to attend this event in SZ.
The cities of the country form a rooted tree with the city of SZ as its root, and each city is connected to its parent by road. For convenience, we number the cities with integers from to . SZ will be city 1, and every other city is connected to its parent by a road of length .
To travel from city to city SZ, one can choose an ancestor of city , pay for the ticket, and take the transit to . Then, choose an ancestor of city , pay for the ticket, arrive at , and so on until you arrive at city SZ.
Each city also has a distance limit for its transit system. For each ancestor of city , city can be reached directly from city if and only if the total length of all roads between them does not exceed . Each city also has two non-negative integers , as fare parameters. If the total length of roads in the path from city to city is , then the ticket cost of going directly from city to city is .
The OIer in each city wants to arrive in SZ while minimising their spending on tickets. Your task is to tell the OIers in each city what is the minimum amount of money they have to spend.
Limits
, , , , .
The distance from any one city to city SZ will not exceed .
Input represents the data type. , where:
- When or , all , that is, the cities form a line rooted at SZ.
- When or , all , that is, there is no distance limit imposed on the cities.
- When , none of the above constraints apply.
Subtask #
|
Score
|
Constraints
|
1 |
10 |
|
2 |
20 |
|
3 |
10 |
|
4 |
30 |
|
5 |
10 |
|
6 |
20 |
No additional constraints |
Input format
The first line of input contains two integers , , indicating the number of cities and the data type respectively. (The Data Type is explained under Limits.)
The next lines of input respectively describe cities 2 through . The line describing city contain 5 non-negative integers , respectively representing the parent of city , the length of the road from to its parent, the two fare parameters of , and the transit distance limit from .
Output format
Output lines each containing a single integer. Line must contain the minimum cost of tickets to get from city to SZ.
Samples
Sample Input 1
|
Sample Output 1
|
7 3
1 2 20 0 3
1 5 10 100 5
2 4 10 10 10
2 9 1 100 10
3 5 20 100 10
4 4 20 0 10
|
40 150 70 149 300 150
|