Thog is an avid TF2 (Thog's Fortnite 2) player, and he loves unlocking achievements in TF2; it was his love for TF2 that allowed him to discover alternate dimensions, as well as interdimensional portals to travel between them (the Nobel Prize was, unfortunately, not a TF2 achievement). To his delight, every dimension contains a different TF2!
When using a portal, Thog takes minutes to travel from dimension to (note: the reversed connection from to would take minutes). Additionally, the TF2 in dimension has available achievements for Thog to receive, which he can only achieve once: the achievement requires minutes to complete and adds points to Thog's global TF2 account.
Before Thog can start using his new technology, rival TF2 players sabotaged interdimensional travel, forcing Thog to spend at most minutes in the dimension. To make things worse, Thog's mom is coming home in minutes, and she will force Thog to drop what he is doing and write his infamous Yahoo answer.
Help Thog find out the maximum amount of points while still starting and returning to his home (at the dimension) within minutes!
(Note: DMOJ and Thog are not affiliated with Fortnite.)
Input Specification
The first line will contain and , the maximum number of minutes Thog can spend across all dimensions and the number of dimensions (which are labelled from to ).
The next lines will contain , , and , the maximum number of minutes Thog can spend in the dimension, the number of achievements in the dimension, and the number of minutes it takes to travel to the dimension.
Before each next dimension line is printed, the next lines directly after the dimension will contain and , the number of minutes Thog needs to spend to acquire the achievement option and the number of points received from completing the achievement option.
The next lines will contain and , a portal that allows Thog to travel between the dimension and the dimension (0-indexed).
(Important note: the connections will always form a tree from the root dimension 0.)
Constraints
For all subtasks:
Subtask 1 [20%]
Subtask 2 [80%]
No additional constraints.
Output Specification
Output the maximum number of points Thog can achieve in minutes (starting from his home at dimension 0).
Sample Input 1
50 1
70 4 0
20 7
35 5
15 10
4 6
Sample Output 1
23
Explanation for Sample Output 1
The maximum number of points Thog can achieve in 50 minutes is 23: he collects 7 points in 20 minutes, 10 points in 15 minutes, and 6 points in 4 minutes (the other achievement (5 points in 35 minutes) would push him over the maximum time he can spend in his initial dimension and the time he can spend collecting points).
Sample Input 2
100 2
80 4 0
20 10
70 40
80 50
30 20
70 2 1
40 10
20 40
0 1
Sample Output 2
80
Explanation for Sample Output 2
The maximum number of points Thog can achieve in 100 minutes is 80: he collects 40 points in 70 minutes, then he goes to dimension 1 in 1 minute and collects 40 points in 20 minutes, then he returns to his initial dimension in 0 minutes (which is the guaranteed travel time to his home in all test cases).
Sample Input 3
150 3
60 3 0
50 10
30 20
10 30
100 2 5
100 20
30 50
20 1 10
20 20
0 1
0 2
Sample Output 3
120
Explanation for Sample Output 3
The maximum number of points Thog can achieve in 150 minutes is 120: he collects 20 points in 30 minutes and 30 points in 10 minutes from his initial dimension, then he goes to dimension 1 in 5 minutes and collects 50 points in 30 minutes, then he goes to the dimension 2, taking 10 minutes (0 minutes to his home and 10 minutes to the third dimension), and collects 20 points in 20 minutes, then he returns home (from the third dimension) in 0 minutes.
Sample Input 4
100 5
60 3 0
50 10
30 20
40 30
100 2 5
100 20
30 50
20 1 10
20 20
50 2 10
10 50
50 10
100 1 5
50 10
0 1
0 2
1 3
3 4
Sample Output 4
130
Explanation for Sample Output 4
The maximum number of points Thog can achieve in 100 minutes is 130: he collects 30 points in 40 minutes from his initial dimension, then he goes to dimension 1 in 5 minutes and collects 50 points in 30 minutes, then he goes to the third dimension in 10 minutes and collects 50 points in 10 minutes, then he returns home in 5 minutes (back to dimension 1, then dimension 0).
Comments