Under construction

Still being built. Things may change

Energy

Time limit1sMemory limit1024 MB

Summary
Choose exactly k vertex-disjoint paths in a tree to maximize the total energy of the rooms they cover.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, DFS
Solved
No attempts yet

Problem

Villain Van Pelt needs to charge his game console urgently. For this, he uses an underground maze with energy sources.

The maze has nn rooms numbered from 1 to nn. Some pairs of rooms are connected by corridors, which can be traveled in both directions.

A path between rooms aa and bb is a sequence of rooms u1,u2,…,uku_1, u_2, \dots, u_k where u1=au_1 = a, uk=bu_k = b, and for every 1≤i≤k−11 \le i \le k - 1, rooms uiu_i and ui+1u_{i+1} are connected by a corridor. Any two rooms have exactly one path between them, so the maze is a tree with exactly n−1n-1 corridors.

Van Pelt does not want to enter the dark, frightening maze himself. Instead, he sends kk jaguars to fetch energy. For each jaguar, Van Pelt chooses a pair of rooms (possibly the same room), and the jaguar walks through every room on the path between them, including the chosen rooms. The jaguars are aggressive, so no two chosen paths may share a room, or the jaguars may fight. Van Pelt must send every jaguar.

Each room has an energy source holding aia_i units of energy in room ii. A source may hold negative energy. Each jaguar takes the energy from every source on its path. The energy Van Pelt receives is the total energy taken by the jaguars.

Find the maximum amount of energy Van Pelt can receive.

Input

The first line contains two integers nn and kk (1≤n≤10001 \le n \le 1000, 1≤k≤n1 \le k \le n), the number of rooms and the number of jaguars.

The second line contains nn integers aia_i (−109≤ai≤109-10^9 \le a_i \le 10^9), separated by spaces.

Each of the next n−1n-1 lines contains two integers uiu_i and viv_i, the rooms connected by a corridor.

Output

Print a single integer, the maximum amount of energy Van Pelt can receive.

Hint

In the sample from the statement, Van Pelt can send one jaguar on the path between rooms 3 and 7 and another on the path between rooms 5 and 6. The jaguars then cover rooms 3, 7, 5, 4, and 6, collecting 14 units of energy.

Examples1

  1. Example 1

    Input
    7 2
    3 -10 2 3 2 5 2
    1 2
    2 3
    2 4
    4 5
    4 6
    3 7
    
    Expected output
    14