Under construction

Still being built. Things may change

Artifacts

Time limit1.5sMemory limit1024 MB

Summary
Find the minimum walk on a weighted tree that visits sites holding all k artifact kinds, where the start and end points cost nothing.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Bit manipulation, DFS
Solved
No attempts yet

Statement

Rick and Morty found a map of a planet in another dimension. The map shows nn dig sites connected by paths. Path ii connects sites uiu_i and viv_i and has length cic_i. There are exactly n−1n-1 paths, and every site can reach every other site, so the structure is a tree. An alien informant reported that there are kk kinds of artifacts in total, and site ii holds an artifact of kind aia_i. Rick is in a competition with a known tomb raider and must collect all kk kinds. Advanced technology does not work in this dimension. Rick can create portals at the start and end points of the route in advance, but he must walk the rest of the route. He chooses the start point, the end point, and the route (not necessarily simple) so that the distance he walks is minimal, and the route collects all kinds of artifacts.

Input

The first line contains two integers nn and kk (2⩽n⩽1052 \leqslant n \leqslant 10^5; 1⩽k⩽61 \leqslant k \leqslant 6), separated by a space. They are the number of sites and the number of required artifact kinds. The next line contains nn integers aia_i (0⩽ai⩽k0 \leqslant a_i \leqslant k), the artifact kind at each site. If ai=0a_i = 0, site ii holds no artifact. The next n−1n-1 lines each contain three integers uiu_i, viv_i, cic_i (1⩽ui,vi⩽n1 \leqslant u_i, v_i \leqslant n; 1⩽ci⩽1091 \leqslant c_i \leqslant 10^9). This means a path of length cic_i connects sites uiu_i and viv_i. The input graph is guaranteed to be a tree.

Output

If it is impossible to collect all kk kinds of artifacts, print −1-1. Otherwise, print the minimum distance Rick must walk to collect all kinds of artifacts.

Hints

In the first example, one optimal route is 1→3→2→3→41 \to 3 \to 2 \to 3 \to 4. In the second example, no site holds artifact kind 5, so it is impossible to collect all five kinds. In the third example, one optimal route is 2→3→42 \to 3 \to 4.

Examples3

  1. Example 1

    Input
    5 4
    1 3 2 4 4
    1 3 1
    2 3 1
    3 4 1
    4 5 1
    
    Expected output
    4
    
  2. Example 2

    Input
    5 5
    1 3 2 4 4
    1 3 1000
    2 3 5123
    3 4 3341
    4 5 7197
    
    Expected output
    -1
    
  3. Example 3

    Input
    4 3
    0 1 2 3
    1 2 10
    2 3 1
    3 4 50
    
    Expected output
    51