#P89. [CSP-S 2023] Tree planting

[CSP-S 2023] Tree planting

Person in Charge

Problem Description

You are a forest caretaker. One day, you receive a task: plant trees on the plots of a forest and nurture them until they grow to specified heights.

The forest map consists of nn plots, with plot 11 connected to the forest entrance. There are n1n-1 roads connecting these plots, ensuring that every plot is reachable from any other. Initially, no trees are planted on any plot.

Your goal is to plant one tree on each plot and ensure that the tree on the ii-th plot grows to a height of at least aia_i meters.

Each day, you may choose an unplanted plot that is directly adjacent (i.e., connected by a single road) to a plot with an already planted tree and plant a tree of height 00 meters. If all plots have been planted, you perform no operation that day. Specifically, on the first day, you can only plant a tree on plot 11.

For each plot, starting from the day it is planted, the tree on that plot grows in height daily. Due to varying climate and soil conditions, on the xx-th day, the tree on the ii-th plot will grow by max(bi+x×ci,1)\max(b_i + x \times c_i, 1) meters. Note that xx is counted from the first day of the entire task, not the day the tree was planted.

You want to know: what is the minimum number of days required to complete your task?

Input Format

The first line of input contains a positive integer nn, the number of plots in the forest.

The next nn lines each contain three integers aia_i, bib_i, cic_i, describing the ii-th plot as explained in the problem description.

The following n1n-1 lines each contain two positive integers uiu_i, viv_i, representing a road connecting plots uiu_i and viv_i.

Output Format

Output a single positive integer, the minimum number of days required to complete the task.

Samples

4
12 1 1
2 4 -1
10 3 0
7 10 -2
1 2
1 3
3 4
5

Sample 1 Explanation

Day 11: Plant a tree on plot 11. The tree on plot 11 grows to 22 meters.

Day 22: Plant a tree on plot 33. The trees on plots 11 and 33 grow to 55 and 33 meters, respectively.

Day 33: Plant a tree on plot 44. The trees on plots 11, 33, and 44 grow to 99, 66, and 44 meters, respectively.

Day 44: Plant a tree on plot 22. The trees on plots 11, 22, 33, and 44 grow to 1414, 11, 99, and 66 meters, respectively.

Day 55: The trees on plots 11, 22, 33, and 44 grow to 2020, 22, 1212, and 77 meters, respectively.

Sample 2

See the files tree/tree2.in and tree/tree2.ans in the contestant directory.

Sample 3

See the files tree/tree3.in and tree/tree3.ans in the contestant directory.

Sample 4

See the files tree/tree4.in and tree/tree4.ans in the contestant directory.

Data Range

For all test data: 1n1051 \le n \le 10^5, 1ai10181 \le a_i \le 10^{18}, 1bi1091 \le b_i \le 10^9, 0ci1090 \le |c_i| \le 10^9, 1ui,vin1 \le u_i, v_i \le n. It is guaranteed that a solution exists within 10910^9 days.

Test Case nn \le Special Properties
11 2020 A
242 \sim 4 None
565 \sim 6 500500 A
787 \sim 8 10510^5
9109 \sim 10 B
111311 \sim 13 C
141614 \sim 16 D
172017 \sim 20 None

Special Property A: For all 1in1 \le i \le n, ci=0c_i = 0;

Special Property B: For all 1i<n1 \le i < n, ui=iu_i = i and vi=i+1v_i = i + 1;

Special Property C: No plot is directly connected to more than 22 roads;

Special Property D: For all 1i<n1 \le i < n, ui=1u_i = 1.