Inversions of a simple path sequence

Given a connected unimodal (unicyclic) graph, find a minimum inversion count over simple paths visiting at least K vertices, or -1 if none exist.

Hard8GraphDynamic programmingDFSSortingNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a connected undirected graph with NN vertices. The vertices are numbered from 11 to NN, and the graph has as many edges as vertices. Vertex ii carries one value ViV_i.

A simple path is a path that visits no vertex more than once. Writing the values of the vertices in the order the path visits them gives a sequence. That sequence is the sequence of the simple path. A path made of a single vertex is also a simple path.

The number of inversions of a sequence SS is the number of pairs (i,j)(i, j) with i<ji < j and S[i]>S[j]S[i] > S[j]. For example, S=(10,30,20,20)S = (10, 30, 20, 20) has two inversions, (2,3)(2, 3) and (2,4)(2, 4).

Given the graph and an integer KK, find the smallest inversion count over all simple paths that visit at least KK vertices.

Input

The first line has the number of vertices NN and the integer KK (3N10003 \le N \le 1000, 1KN1 \le K \le N).

Each of the next NN lines describes one edge with the numbers of the two vertices it joins. No edge joins a vertex to itself, and no edge is given twice.

The last line has the NN values V1V_1 through VNV_N in order (1Vi10001 \le V_i \le 1000).

Output

Print on the first line the smallest inversion count over all simple paths that visit at least KK vertices. If no such simple path exists, print -1.