#include #include using namespace std; using i32 = int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using f64 = long double; using p2 = pair; using el = tuple; using mint = atcoder::modint998244353; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } i128 pow(i128 x, i128 n) { i64 res = 1; i64 t = x; while (n > 0) { if (n & 1) { res = res * t; } t = t * t; n >>= 1; } return res; } i64 pow(i64 x, i64 n, i64 m) { i64 res = 1; i64 t = x % m; while (n > 0) { if (n & 1) { res = res * t % m; } t = t * t % m; n >>= 1; } return res; } void _main() { i64 n, m; cin >> n >> m; vector p(n); vector> g(n); vector cnt(n, 0); for (i64 i = 0; i < n; i++) { cin >> p[i]; } for (i64 i = 0; i < n - 1; i++) { i64 a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); cnt[a]++, cnt[b]++; } vector dist(n, 1e18); queue que; for (i64 i = 0; i < m; i++) { i64 c; cin >> c; c--; dist[c] = 0; que.push(c); } while (!que.empty()) { i64 i = que.front(); que.pop(); for (i64 ni : g[i]) { if (dist[ni] > dist[i] + 1) { dist[ni] = dist[i] + 1; que.push(ni); } } } vector used(n, false); vector ev; set st; for (i64 i = 0; i < n; i++) { ev.push_back({dist[i], i}); if (cnt[i] == 1) { st.insert({p[i], i}); } } sort(ev.begin(), ev.end()); i64 now = 0; i64 idx = 0; i64 ans = 0; while (!st.empty()) { while (idx < ev.size() && ev[idx].first == now) { auto [x, i] = ev[idx]; used[i] = true; idx++; } while (!st.empty() && used[prev(st.end())->second]) { st.erase(*prev(st.end())); } if (st.empty()) break; auto [x, i] = *prev(st.end()); st.erase({x, i}); if (cnt[i] != 1) break; ans += x; for (i64 ni : g[i]) { cnt[ni]--; if (cnt[ni] == 1) { st.insert({p[ni], ni}); } } now++; } cout << ans << "\n"; }