Under construction

Still being built. Things may change

Restaurant recommendation

Time limit3sMemory limit1024 MB

Summary
On a tree of cities, choose restaurants whose delivery balls do not overlap, maximizing the total preference of the chosen set.
Level

Medium7 of 10

Topics
Tree, Dynamic programming, Divide and conquer
Solved
No attempts yet

문제

The KOI country has NN cities, numbered 1 through NN.

The road network of KOI country is a tree when cities are vertices and roads are bidirectional edges. A tree is a connected graph with no cycles.

There are MM restaurants in total, numbered 1 through MM. Some cities may have no restaurant, and some cities may have two or more.

Restaurant ii (1≤i≤M1 \le i \le M) is in city cic_i, has delivery range did_i, and has customer preference gig_i. Restaurant ii can deliver only to cities reachable from city cic_i within did_i roads or fewer. Let Ri={j∣d(ci,j)≤di}R_i = \{j \mid d(c_i, j) \le d_i\} be the set of cities restaurant ii can deliver to, where d(a,b)d(a, b) is the length of the shortest path between cities aa and bb, that is, the minimum number of roads to cross between them.

You operate a delivery app. To avoid overlapping service, you want to choose a set SS of restaurants from the MM restaurants such that no city belongs to the delivery sets of two or more restaurants in SS. That is, for any two different restaurants ii and jj in SS, Ri∩Rj=∅R_i \cap R_j = \emptyset.

Among all valid sets SS, find the one with the largest total preference and print that total.

입력

The first line contains two integers NN and MM, separated by a space.

Each of the next N−1N-1 lines contains two integers aa and bb, separated by a space, meaning there is a road between cities aa and bb.

Each of the next MM lines contains three integers cic_i, did_i, and gig_i, separated by spaces, giving the city, delivery range, and preference of restaurant ii (1≤i≤M1 \le i \le M).

출력

Print on the first line the largest total preference of a valid set SS.

제한

  • All input values are integers.
  • 1≤N≤1051 \le N \le 10^5
  • 1≤M≤1051 \le M \le 10^5
  • For every ii (1≤i≤M1 \le i \le M): 0≤di≤N−10 \le d_i \le N - 1, 1≤gi≤1091 \le g_i \le 10^9

Examples1

  1. Example 1

    Input
    8 5
    1 2
    2 3
    3 4
    4 5
    5 6
    4 7
    4 8
    3 2 40
    6 0 5
    8 0 5
    2 1 16
    5 1 32
    
    Expected output
    53