Programming Team

Choose exactly k candidates from a tree where each pick needs its recommender picked, maximizing total productivity divided by total salary; print the ratio to three decimals.

Hard8Dynamic programmingTreeBinary searchGreedyNo attempts yetTime limit3sMemory limit512 MB

Problem

UpCoder is forming a team to build its new website, and you choose the members. There are nn candidates, numbered 1 through nn. The CEO is employee number 0. Every candidate was recommended by an employee with a smaller number. Besides the employee number, a candidate is described by a salary ss, an expected productivity pp, and the number rr of the employee who recommended them.

You must put exactly kk of the nn candidates on the team. The total value of the team is the sum of the members' productivities divided by the sum of their salaries. A candidate may join the team only if the employee who recommended them is also on the team, or is the CEO. At least one member is therefore a candidate the CEO recommended. The CEO runs the business side of the company and does not count toward the kk members.

Find the largest total value a team can reach under these rules.

Input

The first line has two space separated integers kk and nn (1kn25001 \le k \le n \le 2500), where kk is the size of the team you must form and nn is the number of candidates.

Each of the next nn lines describes one employee, employee 1 first, then employee 2, and so on. A line holds three space separated integers ss, pp and rr: the salary ss (1s100001 \le s \le 10000), the productivity pp (1p100001 \le p \le 10000), and the number rr of the employee who recommended this candidate (0r<i0 \le r < i, where ii is this candidate's employee number).

Output

Print the largest total value on one line, to exactly three decimal places. Round the exact value at the fourth decimal place, and round up when the part below the third decimal place is exactly one half.