Under construction

Still being built. Things may change

Bandwidth Management

Time limit2sMemory limit1024 MB

Summary
Given a tree with vertex limits and a queue of path-add reservations, find the longest prefix of reservations that keeps every vertex under its limit.
Level

Hard8 of 10

Topics
Tree, Prefix sum, Binary search
Solved
No attempts yet

Problem

You are the network administrator of the shake! contest. Every network use is handled by registering a reservation in a queue and receiving your approval.

Surprisingly, the network is a graph with a tree structure. Each vertex of the network is numbered from 11 to NN, and every edge is bidirectional.

Each reservation is written as x,y,wx,y,w. The initial bandwidth usage of every vertex is 00. When a reservation is approved, the bandwidth usage of every vertex on the shortest path between vertex xx and vertex yy increases by ww. The shortest path includes both endpoints xx and yy.

Each vertex has a bandwidth limit. If a vertex uses too much bandwidth, the whole server goes down, and as an administrator you cannot accept that. In other words, no vertex's bandwidth usage may exceed its limit. The queue order matters, so you cannot approve a later reservation without approving the earlier ones. For example, you may accept reservations 1, 2, and 3, or accept none of them. You cannot accept only reservations 1 and 3, or only reservations 2 and 3.

Find the maximum number of reservations that can be approved.

Input

The first line contains the number of vertices NN and the number of reservations MM, separated by a space. (2≤N,M≤100 000)(2\le N,M\le 100\,000)

The next N−1N-1 lines each contain two vertices uu and vv connected by an edge, separated by a space. (1≤u,v≤N;u≠v)(1\le u,v \le N; u\ne v)

The next line contains NN integers A1,A2,…,ANA_1,A_2,\dots,A_N, separated by spaces. AiA_i is the bandwidth limit of vertex ii. (0≤Ai≤109)(0\le A_i\le10^9)

The next MM lines each contain integers x,y,wx,y,w describing a reservation, in the order they were added to the queue. If the reservation is approved, the bandwidth usage of every vertex on the shortest path between vertex xx and vertex yy increases by ww. (1≤w≤109;1≤x,y≤N)(1\le w\le 10^9; 1\le x,y\le N)

The network given in the input is a tree.

Output

Print the maximum number of reservations that can be approved, as a single integer.

Examples1

  1. Example 1

    Input
    3 2
    1 2
    2 3
    5 5 5
    2 1 5
    3 2 5
    
    Expected output
    1