1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<algorithm>
5 #include<vector>
6
7 #define maxn 200001
8
9 using namespace std;
10
11 vector<int>graph[maxn];
12
13 int rev[maxn],in[maxn],father[maxn],dfn[maxn],last[maxn],tot=0;
14
15 long long a[maxn];
16
17 bool vis[maxn];
18
19 struct tr{
20 int l,r,ps;
21 long long num,tag;
22 }tree[maxn*6];
23
24 long long read()
25 {
26 long long x=0;char ch=getchar();
27 while(ch<‘0‘||ch>‘9‘)ch=getchar();
28 while(ch>=‘0‘&&ch<=‘9‘)x=x*10+ch-‘0‘,ch=getchar();
29 return x;
30 }
31
32 void DFS(int poi)
33 {
34 dfn[poi]=++tot;
35 rev[tot]=poi;
36 for(int i=graph[poi].size()-1;i>=0;i--)
37 {
38 int u=graph[poi][i];
39 DFS(u);
40 }
41 last[poi]=tot;
42 }
43
44 void psh(int poi)
45 {
46 if(tree[poi].tag&&tree[poi].l!=tree[poi].r)
47 {
48 tree[poi<<1].tag+=tree[poi].tag;
49 tree[(poi<<1)|1].tag+=tree[poi].tag;
50 tree[poi<<1].num+=tree[poi].tag;
51 tree[(poi<<1)|1].num+=tree[poi].tag;
52 tree[poi].tag=0;
53 }
54 }
55
56 void update(int num,int l,int r,long long d)
57 {
58 psh(num);
59 if(tree[num].l==l&&tree[num].r==r)
60 {
61 tree[num].num+=d;
62 tree[num].tag+=d;
63 return;
64 }
65 int mid=(tree[num].l+tree[num].r)>>1;
66 if(mid>=r)update(num<<1,l,r,d);
67 else if(l>mid)update((num<<1)|1,l,r,d);
68 else update(num<<1,l,mid,d),update((num<<1)|1,mid+1,r,d);
69 if(tree[num].l==tree[num].r)return;
70 if(tree[num<<1].num>tree[(num<<1)|1].num){tree[num].num=tree[num<<1].num,tree[num].ps=tree[num<<1].ps;}
71 if(tree[(num<<1)|1].num>=tree[num<<1].num){tree[num].num=tree[(num<<1)|1].num,tree[num].ps=tree[(num<<1)|1].ps;}
72 }
73
74 void build(int num,int l,int r)
75 {
76 if(l==r)
77 {
78 tree[num].l=tree[num].r=l;
79 tree[num].ps=l;
80 return;
81 }
82 int mid=(l+r)>>1;
83 build(num<<1,l,mid);
84 build((num<<1)|1,mid+1,r);
85 tree[num].l=l,tree[num].r=r;
86 tree[num].ps=tree[(num<<1)|1].ps;
87 }
88
89 int main()
90 {
91 long long ans=0;
92 int n,k;
93 n=read(),k=read();
94 for(int i=1;i<=n;i++)a[i]=read();
95 for(int i=1;i<n;i++)
96 {
97 int u,v;
98 u=read();v=read();
99 graph[u].push_back(v);
100 father[v]=u;
101 }
102
103 father[1]=0;
104
105 DFS(1);
106 build(1,1,n);
107
108 for(int i=1;i<=n;i++)
109 update(1,dfn[i],last[i],a[i]);
110
111 while(k--)
112 {
113 psh(1);
114 ans+=tree[1].num;
115 int u=rev[tree[1].ps];
116 while(u&&!vis[u])
117 {
118 vis[u]=1;
119 update(1,dfn[u],last[u],-a[u]);
120 u=father[u];
121 }
122 }
123 printf("%lld",ans);
124 return 0;
125 }