#P3976. [TJOI2015] 旅游
[TJOI2015] 旅游
Description
To improve her IQ, ZJY plans to travel to a new world. The cities in this world form a tree, and there is exactly one path between any two cities.
Each city has a type of gem with a certain price. To maximize profit, ZJY will choose to buy in city A and then resell in city B (only one purchase is allowed).
Because ZJY often acts cute when buying gems, the gem price in every city she passes through will increase. Let us calculate the maximum profit ZJY can earn after the trip. (For example, if the gem price in city A is , then ZJY’s selling price is also .)
Description
Input Format
- The first line contains a positive integer , the number of cities.
- The next line contains positive integers, the initial prices of the gems in each city. Each initial price does not exceed .
- Starting from the third line, there are lines. Each line contains two integers and , indicating there is an edge between city and city . City indices start from .
- The next line contains a positive integer , the number of queries.
- Each of the next lines contains three positive integers , , , indicating that ZJY travels from to , and the gem price in every city along the route (including both endpoints) increases by .
Output Format
For each query, output the maximum possible profit ZJY can obtain. If the best result is a loss, output .
3
1 2 3
1 2
2 3
2
1 2 100
1 3 100
1
1
5
1 2 3 4 5
1 2
1 3
2 4
4 5
6
1 5 50
2 4 500
3 4 5000
3 5 50000
1 3 500000
2 3 5000000
4
2
551
551
0
499499
Hint
Constraints
- For of the testdata, , .
- For of the testdata, , and at any time the gem price in any city does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号