#include <bits/stdc++.h>
         #define ll long long
         #define scanint1(a) scanf("%d",&a)
         #define scanint2(a,b) scanf("%d %d",&a,&b)
         #define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
         #define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
         #define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
         #define scanlong1(a) scanf("%ld",&a)
         #define scanlong2(a,b) scanf("%ld %ld",&a,&b)
         #define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
         #define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
         #define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
         #define read  freopen("input.txt","r",stdin);
         #define write  freopen("output.txt","w",stdout);

         //Convert from int to char (0 to 25 )--->(a to z) == (char)(int+97)
         //Convert from char int to int char-48
         //~ reflects 0 to 1 and 1 to 0 ; ex:: ~5 =-5
         //tester.erase(std::remove(tester.begin(), tester.end(), '0'), tester.end());
         //bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

                using namespace std;

                  int main()
                {
                    int n,k;
                    scanint2(n,k);
                    long v[100005];
                    int s[100005];
                    long cum[100005];
                    for(int i=1;i<=n;i++)

                    {
                        int x;
                        scanint1(x);
                        v[i]=x;
                        cum[i]=x;

                    }
                    for(int i=1;i<=n;i++)
                    {
                        int x;
                        scanint1(x);
                        s[i]=x;

                    }

                    v[0]=0;
                    vector<long> vc;
                    long result=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(s[i]) result+=v[i];
                        if(!s[i])
                        {
                            vc.push_back(i);

                        }
                    }

                    long mx=0;
                    if(vc.size())  mx=v[vc[0]];
                    for(int i=1;i<vc.size();i++)
                    {
                         if(vc[i]<=vc[i-1]+k-1 &&vc[i-1]<=n-k+1 )
                            mx+=v[vc[i]];

                   else if(vc[i]<=n-k+1)  mx=max(v[vc[i]],mx);

                    }

                  result+=mx;
                  cout<<result<<endl;
                  return 0;
}


#include <bits/stdc++.h>
         #define ll long long
         #define scanint1(a) scanf("%d",&a)
         #define scanint2(a,b) scanf("%d %d",&a,&b)
         #define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
         #define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
         #define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
         #define scanlong1(a) scanf("%ld",&a)
         #define scanlong2(a,b) scanf("%ld %ld",&a,&b)
         #define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
         #define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
         #define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
         #define read  freopen("input.txt","r",stdin);
         #define write  freopen("output.txt","w",stdout);

         //Convert from int to char (0 to 25 )--->(a to z) == (char)(int+97)
         //Convert from char int to int char-48
         //~ reflects 0 to 1 and 1 to 0 ; ex:: ~5 =-5
         //tester.erase(std::remove(tester.begin(), tester.end(), '0'), tester.end());
         //bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

                using namespace std;

                  int main()
                {
                    int n,k;
                    scanint2(n,k);
                    int v[100005];
                    int s[100005];
                    for(int i=1;i<=n;i++)

                    {
                        int x;
                        scanint1(x);
                        v[i]=x;

                    }
                    for(int i=1;i<=n;i++)
                    {
                        int x;
                        scanint1(x);
                        s[i]=x;

                    }

                    vector<int> vc;
                    long result=0;
                    for(int i=1;i<=n;i++)
                    {
                       if(s[i]) result+=v[i];
                       else if(i<=n-k+1 &&!s[i]) vc.push_back(i);
                    }

                     long mx=0;

                    for(int i=0;i<vc.size();i++)
                    {
                        long temp=0;
                         for(int j=0;j<k;j++)
                            if(!s[vc[i]+j])
                            temp+=v[vc[i]+j];

                         mx=max(temp,mx);
                    }
                    result+=mx;
                    cout<<result<<endl;

                    return 0;
                }


#include <bits/stdc++.h>
         #define ll long long
         #define scanint1(a) scanf("%d",&a)
         #define scanint2(a,b) scanf("%d %d",&a,&b)
         #define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
         #define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
         #define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
         #define scanlong1(a) scanf("%ld",&a)
         #define scanlong2(a,b) scanf("%ld %ld",&a,&b)
         #define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
         #define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
         #define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
         #define read  freopen("input.txt","r",stdin);
         #define write  freopen("output.txt","w",stdout);

         //Convert from int to char (0 to 25 )--->(a to z) == (char)(int+97)
         //Convert from char int to int char-48
         //~ reflects 0 to 1 and 1 to 0 ; ex:: ~5 =-5
         //tester.erase(std::remove(tester.begin(), tester.end(), '0'), tester.end());
         //bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

                using namespace std;

                  int main()
                {
                    int n,k;
                    scanint2(n,k);
                    long v[100005];
                    int s[100005];
                    for(int i=1;i<=n;i++)

                    {
                        int x;
                        scanint1(x);
                        v[i]=x;

                    }
                    for(int i=1;i<=n;i++)
                    {
                        int x;
                        scanint1(x);
                        s[i]=x;

                    }

                    vector<int> vc;
                    long result=0;
                    for(int i=1;i<=n;i++)
                    {
                       if(s[i]) result+=v[i];
                       else if(i<=n-k+1 &&!s[i])
                           vc.push_back(i);

                    }

                    int last=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(!s[i]) v[i]+=last;
                        if(!s[i]) last=v[i];

                    }

                    long mx=0;
                    for(int i=0;i<vc.size();i++)
                    {

                          for(int j=vc[i]+k-1;j>=i;j--)
                          {if(!s[j]){mx=max(mx,v[j]); break;}
                          }

                    }
                    result+=mx;
                    cout<<result<<endl;
                    return 0;
                }


#include <bits/stdc++.h>
         #define ll long long
         #define scanint1(a) scanf("%d",&a)
         #define scanint2(a,b) scanf("%d %d",&a,&b)
         #define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
         #define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
         #define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
         #define scanlong1(a) scanf("%ld",&a)
         #define scanlong2(a,b) scanf("%ld %ld",&a,&b)
         #define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
         #define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
         #define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
         #define read  freopen("input.txt","r",stdin);
         #define write  freopen("output.txt","w",stdout);

         //Convert from int to char (0 to 25 )--->(a to z) == (char)(int+97)
         //Convert from char int to int char-48
         //~ reflects 0 to 1 and 1 to 0 ; ex:: ~5 =-5
         //tester.erase(std::remove(tester.begin(), tester.end(), '0'), tester.end());
         //bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

                using namespace std;

                  int main()
                {
                    int n,k;
                    scanint2(n,k);
                    long v[100005];
                    int s[100005];
                    for(int i=1;i<=n;i++)

                    {
                        int x;
                        scanint1(x);
                        v[i]=x;

                    }
                    for(int i=1;i<=n;i++)
                    {
                        int x;
                        scanint1(x);
                        s[i]=x;

                    }

                    vector<int> vc;
                    long result=0;
                    for(int i=1;i<=n;i++)
                    {
                       if(s[i]) result+=v[i];
                       else if(i<=n-k+1 &&!s[i])
                           vc.push_back(i);

                    }

                    int last=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(!s[i]) v[i]+=last;
                        if(!s[i]) last=v[i];

                    }

                    long mx=0;
                    for(int i=0;i<vc.size();i++)
                    {

                        mx=max(mx,v[vc[i]]);
                        if(!s[vc[i]+k-1])
                            mx=max(mx,v[vc[i]+k-1]);

                    }
                    result+=mx;
                    cout<<result<<endl;
                    return 0;
                }


#include <bits/stdc++.h>
         #define ll long long
         #define scanint1(a) scanf("%d",&a)
         #define scanint2(a,b) scanf("%d %d",&a,&b)
         #define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
         #define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
         #define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
         #define scanlong1(a) scanf("%ld",&a)
         #define scanlong2(a,b) scanf("%ld %ld",&a,&b)
         #define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
         #define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
         #define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
         #define read  freopen("input.txt","r",stdin);
         #define write  freopen("output.txt","w",stdout);

         //Convert from int to char (0 to 25 )--->(a to z) == (char)(int+97)
         //Convert from char int to int char-48
         //~ reflects 0 to 1 and 1 to 0 ; ex:: ~5 =-5
         //tester.erase(std::remove(tester.begin(), tester.end(), '0'), tester.end());
         //bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

                using namespace std;

                  int main()
                {
                    int n,k;
                    scanint2(n,k);
                    long v[100005];
                    int s[100005];
                    for(int i=1;i<=n;i++)

                    {
                        int x;
                        scanint1(x);
                        v[i]=x;

                    }
                    for(int i=1;i<=n;i++)
                    {
                        int x;
                        scanint1(x);
                        s[i]=x;

                    }

                    v[0]=0;
                    vector<int> vc;
                    long result=0;
                    for(int i=1;i<=n;i++)
                    {

                        if(s[i]) result+=v[i];
                        if(!s[i]&&i<=n+k-1) vc.push_back(i);
                    }

                    long mx =0;
                  for(int i=0;i<vc.size();i++)
                    {
                         long temp=0;
                             for(int j=vc[i];j<=vc[i]+k-1;j++)
                             {
                                 if(!s[j]) temp+=v[j];

                             }
                             mx=max(mx,temp);
                    }

                  result+=mx;
                  cout<<result<<endl;
                    return 0;
}


#include <bits/stdc++.h>
         #define ll long long
         #define scanint1(a) scanf("%d",&a)
         #define scanint2(a,b) scanf("%d %d",&a,&b)
         #define scanint3(a,b,c) scanf("%d %d %d",&a,&b,&c)
         #define scanint4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
         #define scanint5(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
         #define scanlong1(a) scanf("%ld",&a)
         #define scanlong2(a,b) scanf("%ld %ld",&a,&b)
         #define scanlong3(a,b,c) scanf("%ld %ld %ld",&a,&b,&c)
         #define scanlong4(a,b,c,d) scanf("%ld %ld %ld %ld",&a,&b,&c,&d)
         #define scanlong5(a,b,c,d,e) scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e)
         #define read  freopen("input.txt","r",stdin);
         #define write  freopen("output.txt","w",stdout);

         //Convert from int to char (0 to 25 )--->(a to z) == (char)(int+97)
         //Convert from char int to int char-48
         //~ reflects 0 to 1 and 1 to 0 ; ex:: ~5 =-5
         //tester.erase(std::remove(tester.begin(), tester.end(), '0'), tester.end());
         //bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

                using namespace std;

                  int main()
                {
                    int n,k;
                    scanint2(n,k);
                    long v[100005];
                    int s[100005];
                    long cum[100005];
                    for(int i=1;i<=n;i++)

                    {
                        int x;
                        scanint1(x);
                        v[i]=x;
                        cum[i]=x;

                    }
                    for(int i=1;i<=n;i++)
                    {
                        int x;
                        scanint1(x);
                        s[i]=x;

                    }

                    v[0]=0;
                    vector<long> vc;
                    long result=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(s[i]) result+=v[i];
                        if(!s[i])
                        {
                            vc.push_back(i);

                        }
                    }

                    long mx=0;
                     if(vc.size())  mx=v[vc[0]];
                    for(int i=1;i<vc.size();i++)
                    {
                         if(vc[i]<=vc[i-1]+k+1 &&vc[i-1]<=n-k+1 )
                            mx+=v[vc[i]];

                   else if(vc[i]<=n-k+1)  mx=max(v[vc[i]],mx);

                    }

                  result+=mx;
                  cout<<result<<endl;
                    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#define mem(a) meset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int M=1e5+10;
int a[M],f[M];
int main() {
	int n,k;
	cin>>n>>k;
	for(int i=0; i<n; ++i)
		scanf("%d",&a[i]);
	for(int i=0; i<n; ++i)
		scanf("%d",&f[i]);
	ll sum=0;
	int pos=0;
	for(int i=0; i<n-k; ++i) {
		ll tem=0;
		if(f[i]==0) {
			for(int j=i; j<i+k; ++j) {
				tem+=a[j];
			}
			if(tem>sum) {
				sum=tem;
				pos=i;
			}
		}
	}
	ll res=0;
	for(int i=0; i<pos; ++i) {
		if(f[i]==1) {
			res+=a[i];
		}
	}
	res+=sum;
	for(int i=pos+k; i<n; ++i) {
		if(f[i]==1) {
			res+=a[i];
		}
	}
	cout<<res<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#define mem(a) meset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int M=1e5+10;
int a[M],f[M];
int main() {
	int n,k;
	cin>>n>>k;
	ll res=0;
	for(int i=0; i<n; ++i)
		scanf("%d",&a[i]);
	for(int i=0; i<n; ++i) {
		scanf("%d",&f[i]);
		if(f[i]==1) {
			res+=a[i];
		}
	}
	ll sum=0;
	int pos=0;
	if(n==k) {
		for(int i=0; i<n; ++i) {
			if(f[i]==0)
				res+=a[i];
		}
	} else {
		for(int i=0; i<n; ++i) {
			ll tem=0;
			if(f[i]==0) {
				for(int j=i; j<i+k&&j<n; ++j) {
					tem+=a[j];
				}
				if(tem>sum) {
					sum=tem;
					pos=i;
				}
			}
		}
	}
	if(pos!=0) {
		for(int i=pos; i<pos+k; ++i) {
			if(f[i]==0) {
				res+=a[i];
			}
		}
	}
	cout<<res<<endl;
	//cout<<pos<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#define mem(a) meset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
const int M=1e5+10;
int a[M],f[M];
int main() {
	int n,k;
	cin>>n>>k;
	ll res=0;
	for(int i=0; i<n; ++i)
		scanf("%d",&a[i]);
	for(int i=0; i<n; ++i) {
		scanf("%d",&f[i]);
		if(f[i]==1) {
			res+=a[i];
		}
	}
	ll sum=0;
	int pos=0;
	if(n==k) {
		for(int i=0; i<n; ++i) {
			if(f[i]==0)
				res+=a[i];
		}
	} else {
		for(int i=0; i<n-k; ++i) {
			ll tem=0;
			if(f[i]==0) {
				for(int j=i; j<i+k&&j<n; ++j) {
					tem+=a[j];
				}
				if(tem>sum) {
					sum=tem;
					pos=i;
				}
			}
		}
	}
	if(pos!=0) {
		for(int i=pos; i<pos+k; ++i) {
			if(f[i]==0) {
				res+=a[i];
			}
		}
	}
	cout<<res<<endl;
	//cout<<pos<<endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <cstring>
#include <chrono>
#include <bitset>
#include <list>
#include <stdlib.h>
#include <iomanip>
#include <bitset>
#include <chrono>

using namespace std;

#define inf   2147480647

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ifd   if (debug)

#define debug true
#define ll    long long
#define ull   unsinged long long

void log_time(string s)
{
    if (debug) {
        chrono::milliseconds ms = chrono::duration_cast< chrono::milliseconds >(chrono::system_clock::now().time_since_epoch());
        cout << s << ":" << ms.count() << endl;
    }
}

ll gcd(ll x, ll y)
{
    return y == 0LL ? x : gcd(y, x % y);
}

int gcd(int x, int y)
{
    return y == 0 ? x : gcd(y, x % y);
}

bool comparator(const pair<ll, ll> &x, const pair<ll, ll> &y)
{
    return x.second - x.first < y.second - y.first;
}

int f_abs(int x)
{
    return x < 0 ? -x : x;
}

bool c1(const pair<int, int> &x, const pair<int, int> &y)
{
    return x.second < y.second;
}

ll dfs(vector< set< pair<int, int> > > &adj, vector<char> &color, int c, vector<int> &groups, int group, ll size)
{
    color[c] = 'g';

    groups[c] = group;

    int curr_size = 1LL;

    for (set< pair<int, int> >::iterator it = adj[c].begin(); it != adj[c].end(); it++) {
        pair<int, int> p = *it;
        int n = p.first;
        if (color[n] == 'w') {
            curr_size += dfs(adj, color, n, groups, group, 0);
        }
    }

    color[c] = 'b';

    return curr_size;
}

int get_root(vector<int> &group, int x)
{
    while (group[x] != x) {
        group[x] = group[group[x]];
        x = group[x];
    }
    return x;
}

ll amount(ll size)
{
    if (size < 2) {
        return 0LL;
    } else {
        ll count = 1LL *  size * (size - 1) / 2;
        return count;
    }
}

ll group_merge(vector<int> &group, int x, int y, vector<ll> &depth, vector<ll> &size, ll total)
{
    total = total - (amount(size[y]) + amount(size[x])) + amount(size[x] + size[y]);

    if (depth[x] > depth[y]) {
        group[y] = x;
        size[x] += size[y];
    } else if (depth[x] < depth[y]) {
        group[x] = y;
        size[y] += size[x];
    } else {
        group[x] = y;
        depth[y]++;
        size[y] += size[x];
    }

    return total;
}

int main()
{
    std::ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector< pair<ll, pair<int, int > > > edges(n - 1);
    forn(i, n - 1) {
        cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;
    }
    sort(edges.begin(), edges.end());

    vector< pair<ll, int> > as(m);
    forn(i, m) {
        cin >> as[i].first;
        as[i].second = i;
    }
    sort(as.begin(), as.end());

    ll min_weight = as.front().first;

    vector<int> groups(n);
    vector<ll> depth(n, 1LL);
    vector<ll> size(n, 1LL);
    forn(i, n) {
        groups[i] = i;
    }

    ll total = 0LL;
    vector< pair<ll, pair<int, int > > >::iterator it = edges.begin();
    while (it != edges.end() && (*it).first < min_weight) {
        pair<ll, pair<int, int> > weight_and_coords = *it;
        pair<int, int> cc = weight_and_coords.second;
        int r1 = get_root(groups, cc.first), r2 = get_root(groups, cc.second);
        if (r1 != r2) {
            total = group_merge(groups, r1, r2, depth, size, total);
        }
        it++;
    }

    map<int, ll> solution;
    forn(i, as.size()) {
        pair<int, int> pas = as[i];
        ll weight = pas.first;

        while (it != edges.end() && (*it).first <= weight) {
            pair< ll, pair<int, int> > p = *it;

            pair<int, int> cc = p.second;
            int r1 = get_root(groups, cc.first), r2 = get_root(groups, cc.second);
            if (r1 != r2) {
                total = group_merge(groups, r1, r2, depth, size, total);
            }

            it++;
        }

        solution[pas.second] = total;
    }

    sort(as.begin(), as.end(), c1);

    forn(i, as.size()) {
        cout << solution[as[i].second] << ' ';
    }
    cout << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <cstring>
#include <chrono>
#include <bitset>
#include <list>
#include <stdlib.h>
#include <iomanip>
#include <bitset>
#include <chrono>

using namespace std;

#define inf   2147480647

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ifd   if (debug)

#define debug true
#define ll    long long
#define ull   unsinged long long

void log_time(string s)
{
    if (debug) {
        chrono::milliseconds ms = chrono::duration_cast< chrono::milliseconds >(chrono::system_clock::now().time_since_epoch());
        cout << s << ":" << ms.count() << endl;
    }
}

ll gcd(ll x, ll y)
{
    return y == 0LL ? x : gcd(y, x % y);
}

int gcd(int x, int y)
{
    return y == 0 ? x : gcd(y, x % y);
}

bool comparator(const pair<ll, ll> &x, const pair<ll, ll> &y)
{
    return x.second - x.first < y.second - y.first;
}

int f_abs(int x)
{
    return x < 0 ? -x : x;
}

bool c1(const pair<int, int> &x, const pair<int, int> &y)
{
    return x.second < y.second;
}

ll dfs(vector< set< pair<int, int> > > &adj, vector<char> &color, int c, vector<int> &groups, int group, ll size)
{
    color[c] = 'g';

    groups[c] = group;

    int curr_size = 1LL;

    for (set< pair<int, int> >::iterator it = adj[c].begin(); it != adj[c].end(); it++) {
        pair<int, int> p = *it;
        int n = p.first;
        if (color[n] == 'w') {
            curr_size += dfs(adj, color, n, groups, group, 0);
        }
    }

    color[c] = 'b';

    return curr_size;
}

int get_root(vector<int> &group, int x)
{
    while (group[x] != x) {
        group[x] = group[group[x]];
        x = group[x];
    }
    return x;
}

ll amount(int size)
{
    if (size < 2) {
        return 0LL;
    } else {
        ll count = 1LL *  size * (size - 1) / 2;
        return count;
    }
}

ll group_merge(vector<int> &group, int x, int y, vector<int> &depth, vector<int> &size, ll total)
{
    total = total - (amount(size[y]) + amount(size[x])) + amount(size[x] + size[y]);

    if (depth[x] > depth[y]) {
        group[y] = x;
        size[x] += size[y];
    } else if (depth[x] < depth[y]) {
        group[x] = y;
        size[y] += size[x];
    } else {
        group[x] = y;
        depth[y]++;
        size[y] += size[x];
    }

    return total;
}

int main()
{
    std::ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector< pair<int, pair<int, int > > > edges(n - 1);
    forn(i, n - 1) {
        cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;
    }
    sort(edges.begin(), edges.end());

    vector< pair<int, int> > as(m);
    forn(i, m) {
        cin >> as[i].first;
        as[i].second = i;
    }
    sort(as.begin(), as.end());

    int min_weight = as.front().first;

    vector<int> groups(n);
    vector<int> depth(n, 1);
    vector<int> size(n, 1);
    forn(i, n) {
        groups[i] = i;
    }

    ll total = 0LL;
    vector< pair<int, pair<int, int > > >::iterator it = edges.begin();
    while (it != edges.end() && (*it).first < min_weight) {
        pair<int, pair<int, int> > weight_and_coords = *it;
        pair<int, int> cc = weight_and_coords.second;
        int r1 = get_root(groups, cc.first), r2 = get_root(groups, cc.second);
        if (r1 != r2) {
            total = group_merge(groups, r1, r2, depth, size, total);
        }
        it++;
    }

    //map<int, ll> solution;
    vector<ll> solution(m);
    forn(i, as.size()) {
        pair<int, int> pas = as[i];
        int weight = pas.first;

        while (it != edges.end() && (*it).first <= weight) {
            pair< int, pair<int, int> > p = *it;

            pair<int, int> cc = p.second;
            int r1 = get_root(groups, cc.first), r2 = get_root(groups, cc.second);
            if (r1 != r2) {
                total = group_merge(groups, r1, r2, depth, size, total);
            }

            it++;
        }

        solution[pas.second] = total;
    }

    sort(as.begin(), as.end(), c1);

    forn(i, solution.size()) {
        cout << solution[as[i].second] << ' ';
    }
    cout << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <cstring>
#include <chrono>
#include <bitset>
#include <list>
#include <stdlib.h>
#include <iomanip>
#include <bitset>
#include <chrono>

using namespace std;

#define inf   2147480647

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ifd   if (debug)

#define debug true
#define ll    long long
#define ull   unsinged long long

void log_time(string s)
{
    if (debug) {
        chrono::milliseconds ms = chrono::duration_cast< chrono::milliseconds >(chrono::system_clock::now().time_since_epoch());
        cout << s << ":" << ms.count() << endl;
    }
}

ll gcd(ll x, ll y)
{
    return y == 0LL ? x : gcd(y, x % y);
}

int gcd(int x, int y)
{
    return y == 0 ? x : gcd(y, x % y);
}

bool comparator(const pair<ll, ll> &x, const pair<ll, ll> &y)
{
    return x.second - x.first < y.second - y.first;
}

int f_abs(int x)
{
    return x < 0 ? -x : x;
}

bool c1(const pair<int, int> &x, const pair<int, int> &y) {
    return x.second < y.second;
}

ll dfs(vector< set< pair<int, int> > > &adj, vector<char> &color, int c, vector<int> &groups, int group, ll size) {
    color[c] = 'g';

    groups[c] = group;

    int curr_size = 1LL;

    for (set< pair<int, int> >::iterator it = adj[c].begin(); it != adj[c].end(); it++) {
        pair<int, int> p = *it;
        int n = p.first;
        if (color[n] == 'w') {
            curr_size += dfs(adj, color, n, groups, group, 0);
        }
    }

    color[c] = 'b';

    return curr_size;
}

int get_root(vector<int> &group, int x) {
    while (group[x] != x) {
        group[x] = group[group[x]];
        x = group[x];
    }
    return x;
}

ll amount(ll size) {
    if (size < 2) {
        return 0LL;
    } else {
        ll count = 1LL *  size * (size - 1) / 2;
        return count;
    }
}

ll group_merge(vector<int> &group, int x, int y, map<int, ll> &depth, map<int, ll> &size, ll total) {
    total = total - (amount(size[y]) + amount(size[x])) + amount(size[x] + size[y]);

    if (depth[x] > depth[y]) {
        group[y] = x;
        //depth.erase(y);
        size[x] += size[y];
        //size.erase(y);
    } else if (depth[x] < depth[y]) {
        group[x] = y;
        //depth.erase(x);
        size[y] += size[x];
        //size.erase(x);
    } else {
        group[x] = y;
        //depth.erase(x);
        depth[y]++;
        size[y] += size[x];
        //size.erase(x);
    }

    return total;
}

int main()
{
    std::ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    map< int, vector< pair<int, int> > > stats;
    int t1, t2, w1;
    forn (i, n - 1) {
        cin >> t1 >> t2 >> w1;
        stats[w1].push_back(make_pair(t1 - 1, t2 - 1));
    }

    vector< pair<int, int> > as(m);
    forn (i, m) {
        cin >> as[i].first;
        as[i].second = i;
    }
    sort(as.begin(), as.end());

    int min_weight = as.front().first;

    vector< set< pair<int, int> > > adj(n, set< pair<int, int> >());
    for (map< int, vector< pair<int, int> > >::iterator it = stats.begin(); it != stats.end(); it++) {
        pair< int, vector< pair<int, int> > > p = *it;
        int w = p.first;
        if (w < min_weight) {
            forn (i, p.second.size()) {
                int x = p.second[i].first;
                int y = p.second[i].second;
                adj[x].insert(make_pair(y, w));
                adj[y].insert(make_pair(x, w));
            }
        }
    }

    vector<int> groups(n);
    vector<char> color(n, 'w');
    map<int, ll> depth;
    map<int, ll> size;
    ll total = 0LL;
    forn (i, n) {
        if (color[i] == 'w') {
            ll s = dfs(adj, color, i, groups, i, 0LL);
            size[i] = 1LL * s;
            depth[i] = 1LL;
            total += amount(s);
        }
    }

    map<int, ll> solution;

    map< int, vector< pair<int, int> > >::iterator sit = stats.begin();
    forn (i, as.size()) {
        pair<int, int> pas = as[i];
        int weight = pas.first;

        while (sit != stats.end() && (*sit).first <= weight) {
            pair< int, vector< pair<int, int> > > p = *sit;
            forn (j, p.second.size()) {
                pair<int, int> cc = p.second[j];
                int r1 = get_root(groups, cc.first), r2 = get_root(groups, cc.second);
                if (r1 != r2) {
                    total = group_merge(groups, r1, r2, depth, size, total);
                }
            }
            //stats.erase(stats.begin());
            sit++;
        }

        solution[pas.second] = total;
    }

    sort(as.begin(), as.end(), c1);

    forn (i, as.size()) {
        cout << solution[as[i].second] << ' ';
    }
    cout << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <cstring>
#include <chrono>
#include <bitset>
#include <list>
#include <stdlib.h>
#include <iomanip>
#include <bitset>
#include <chrono>

using namespace std;

#define inf   2147480647

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ifd   if (debug)

#define debug true
#define ll    long long
#define ull   unsinged long long

void log_time(string s)
{
    if (debug) {
        chrono::milliseconds ms = chrono::duration_cast< chrono::milliseconds >(chrono::system_clock::now().time_since_epoch());
        cout << s << ":" << ms.count() << endl;
    }
}

ll gcd(ll x, ll y)
{
    return y == 0LL ? x : gcd(y, x % y);
}

int gcd(int x, int y)
{
    return y == 0 ? x : gcd(y, x % y);
}

bool comparator(const pair<ll, ll> &x, const pair<ll, ll> &y)
{
    return x.second - x.first < y.second - y.first;
}

int f_abs(int x)
{
    return x < 0 ? -x : x;
}

bool c1(const pair<int, int> &x, const pair<int, int> &y) {
    return x.second < y.second;
}

int dfs(vector< set< pair<int, int> > > &adj, vector<char> &color, int c, vector<int> &groups, int group, int size) {
    color[c] = 'g';

    groups[c] = group;

    int curr_size = 1;

    for (set< pair<int, int> >::iterator it = adj[c].begin(); it != adj[c].end(); it++) {
        pair<int, int> p = *it;
        int n = p.first;
        if (color[n] == 'w') {
            curr_size += dfs(adj, color, n, groups, group, 0);
        }
    }

    color[c] = 'b';

    return curr_size;
}

int get_root(vector<int> &group, int x) {
    while (group[x] != x) {
        group[x] = group[group[x]];
        x = group[x];
    }
    return x;
}

ll amount(int size) {
    if (size < 2) {
        return 0LL;
    } else {
        ll count = 1LL *  size * (size - 1) / 2;
        return count;
    }
}

ll group_merge(vector<int> &group, int x, int y, map<int, int> &depth, map<int, int> &size, ll total) {
    total -= (amount(size[y]) + amount(size[x]));
    total += amount(size[x] + size[y]);

    if (depth[x] > depth[y]) {
        group[y] = x;
        //depth.erase(y);
        size[x] += size[y];
        size.erase(y);
    } else if (depth[x] < depth[y]) {
        group[x] = y;
        //depth.erase(x);
        size[y] += size[x];
        size.erase(x);
    } else {
        group[x] = y;
        //depth.erase(x);
        depth[y]++;
        size[y] += size[x];
        size.erase(x);
    }

    return total;
}

int main()
{
    std::ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    // input edges
    map< int, vector< pair<int, int> > > stats;
    vector< tuple<int, int, int> > input;
    int t1, t2, w1;
    forn (i, n - 1) {
        cin >> t1 >> t2 >> w1;
        t1--; t2--;

        input.push_back(make_tuple(t1, t2, w1));
        stats[w1].push_back(make_pair(t1, t2));
    }

    vector< pair<int, int> > as(m);
    forn (i, m) {
        cin >> as[i].first;
        as[i].second = i;
    }
    sort(as.begin(), as.end());

    int min_weight = as.front().first;

    vector< set< pair<int, int> > > adj(n, set< pair<int, int> >());
    forn (i, input.size()) {
        tuple<int, int, int> t = input[i];
        int w = get<0>(t);
        if (w < min_weight) {
            int x = get<1>(t);
            int y = get<2>(t);
            adj[x].insert(make_pair(y, w));
            adj[y].insert(make_pair(x, w));
        }
    }

    /*
    for (map< int, vector< pair<int, int> > >::iterator it = stats.begin(); it != stats.end(); it++) {
        pair< int, vector< pair<int, int> > > p = *it;

        if (p.first >= as.front().first) {
            forn (i, p.second.size()) {
                pair<int, int> cc = p.second[i];
                adj[cc.first].erase(make_pair(cc.second, p.first));
                adj[cc.second].erase(make_pair(cc.first, p.first));
            }
        }
    }
    */

    vector<int> groups(n);
    vector<char> color(n, 'w');
    map<int, int> depth;
    map<int, int> size;
    ll total = 0LL;
    forn (i, n) {
        if (color[i] == 'w') {
            int s = dfs(adj, color, i, groups, i, 0);
            size[i] = s;
            depth[i] = 1;
            total += 0LL + amount(s);
        }
    }

    map<int, ll> solution;

    map< int, vector< pair<int, int> > >::iterator sit = stats.begin();
    forn (i, as.size()) {
        pair<int, int> pas = as[i];
        int weight = pas.first;

        while (sit != stats.end() && (*sit).first <= weight) {
            pair< int, vector< pair<int, int> > > p = *sit;
            forn (j, p.second.size()) {
                pair<int, int> cc = p.second[j];
                int r1 = get_root(groups, cc.first), r2 = get_root(groups, cc.second);
                if (r1 != r2) {
                    total = group_merge(groups, r1, r2, depth, size, total);
                }
            }
            //stats.erase(stats.begin());
            sit++;
        }

        /*
        ll count = 0LL;
        for (map<int, int>::iterator it = size.begin(); it != size.end(); it++) {
            pair<int, int> p = *it;
            count += p.second < 2 ? 0 : 1LL * p.second * (p.second - 1) / 2;
        }
         */

        solution[pas.second] = total;
    }

    sort(as.begin(), as.end(), c1);

    forn (i, as.size()) {
        cout << solution[as[i].second] << ' ';
    }
    cout << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <cstring>
#include <chrono>
#include <bitset>
#include <list>
#include <stdlib.h>
#include <iomanip>
#include <bitset>
#include <chrono>

using namespace std;

#define inf   2147480647

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ifd   if (debug)

#define debug true
#define ll    long long
#define ull   unsinged long long

void log_time(string s)
{
    if (debug) {
        chrono::milliseconds ms = chrono::duration_cast< chrono::milliseconds >(chrono::system_clock::now().time_since_epoch());
        cout << s << ":" << ms.count() << endl;
    }
}

ll gcd(ll x, ll y)
{
    return y == 0LL ? x : gcd(y, x % y);
}

int gcd(int x, int y)
{
    return y == 0 ? x : gcd(y, x % y);
}

bool comparator(const pair<ll, ll> &x, const pair<ll, ll> &y)
{
    return x.second - x.first < y.second - y.first;
}

int f_abs(int x)
{
    return x < 0 ? -x : x;
}

int dfs(vector< set< pair<int, int> > > &adj, vector<char> &color, int c, int sum) {
    color[c] = 'g';

    int curr_sum = 1;

    for (set< pair<int, int> >::iterator it = adj[c].begin(); it != adj[c].end(); it++) {
        pair<int, int> p = *it;
        int n = p.first;
        //int w = p.second;
        if (color[n] == 'w') {
            curr_sum += dfs(adj, color, n, 0);
        }
    }

    color[c] = 'b';

    return curr_sum;
}

int main()
{
    std::ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    map< int, vector< pair<int, int> > > stats;
    vector< set< pair<int, int> > > adj(n, set< pair<int, int> >());
    int t1, t2, w1;
    forn (i, n - 1) {
        cin >> t1 >> t2 >> w1;
        t1--; t2--;
        adj[t1].insert(make_pair(t2, w1));
        adj[t2].insert(make_pair(t1, w1));

        stats[w1].push_back(make_pair(t1, t2));
    }

    vector< pair<int, int> > as(m);
    forn (i, m) {
        cin >> as[i].first;
        as[i].second = i;
    }

    sort(as.rbegin(), as.rend());

    vector<ll> solution(m);

    for (int gi = 0; gi < m; gi++) {
        int weight_supremum = as[gi].first;
        vector<char> color(n, 'w');

        int rem = 0;
        while (!stats.empty() && (*stats.rbegin()).first > weight_supremum) {
            pair<int, vector< pair<int, int > > > p = *stats.rbegin();
            for (int i = 0; i < p.second.size(); i++) {
                pair<int, int> vs = p.second[i];
                adj[vs.first].erase(make_pair(vs.second, p.first));
                adj[vs.second].erase(make_pair(vs.first, p.first));
            }
            stats.erase(prev(stats.end()));
            rem++;
        }

        ll total = 0LL;

        if (rem > 0 || gi == 0) {
            forn (i, n) {
                if (color[i] == 'w') {
                    int count = dfs(adj, color, i, 0);
                    total += count > 1 ? 1LL * count * (count - 1) / 2 : 0LL;
                }
            }
            solution[as[gi].second] = total;
        } else {
            solution[as[gi].second] = solution[as[gi - 1].second];
        }

        //cout << total << endl;
    }

    forn (i, m) {
        cout << solution[i] << ' ';
    }
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< ll , pair<ll,ll> > > edges;

pair<ll,ll> q[400005];

ll prll [400005];

ll ans;

ll par [400005];
ll siz [400005];

ll prog [400005];

ll f(ll x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(ll x,ll y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(ll i=1;i<400005;i++)prog[i] = prog[i-1]+i;
    ll n,m;
    cin >>n>>m;
    for(ll i=0;i<n-1;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(ll i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(ll i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(ll i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    ll ind = 0;
    for(ll i=0;i<m;i++){
        for(;edges[ind].first<=q[i].first && ind<=m;ind++){
            ll u = f(edges[ind].second.first);
            ll v = f(edges[ind].second.second);
            ans -= prog[ siz[ u ] - 1 ] + prog[ siz[ v ] - 1 ];
            ans += prog[ siz[ u ] + siz[ v ] - 1 ];
            unite(u,v);
        }
        prll[ q[i].second ] = ans;
    }
    for(ll i=0;i<m;i++)cout<<prll[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< ll , pair<ll,ll> > > edges;

pair<ll,ll> q[400005];

ll print [400005];

ll ans;

ll par [400005];
ll siz [400005];

ll prog [400005];

ll f(ll x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(ll x,ll y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(ll i=1;i<400005;i++)prog[i] = prog[i-1]+i;
    ll n,m;
    cin >>n>>m;
    for(ll i=0;i<n-1;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(ll i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(ll i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(ll i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    ll ind = 0;
    for(ll i=0;i<m;i++){
        for( ; edges[ind].first <= q[i].first && ind<n-1 ; ind++){
            ll u = f(edges[ind].second.first);
            ll v = f(edges[ind].second.second);
            ans -= prog[ siz[u] - 1 ] + prog[ siz[v] - 1 ];
            ans += prog[ siz[u] + siz[v] - 1 ];
            unite(u,v);
        }
        print [ q[i].second ] = ans;
    }
    for(ll i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< int , pair<int,int> > > edges;

pair<int,int> q[200005];

int print [200005];

ll ans;

int par [200005];
int siz [200005];

ll prog [200005];

int f(int x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(int x,int y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(int i=1;i<200005;i++)prog[i] = prog[i-1]+i;
    int n,m;
    cin >>n>>m;
    for(int i=0;i<n-1;i++){
        int u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(int i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(int i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(int i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    int ind = 0;
    for(int i=0;i<m;i++){
        for(;edges[ind].first<=q[i].first && ind<=m;ind++){
            int u = f(edges[ind].second.first);
            int v = f(edges[ind].second.second);
            ans -= prog[ siz[ u ] - 1 ] + prog[ siz[ v ] - 1 ];
            ans += prog[ siz[ u ] + siz[ v ] - 1 ];
            unite(u,v);
        }
        print[ q[i].second ] = ans;
    }
    for(int i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< ll , pair<ll,ll> > > edges;

pair<ll,ll> q[400005];

ll print [400005];

ll ans;

ll par [400005];
ll siz [400005];

ll prog [400005];

ll f(ll x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(ll x,ll y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(ll i=1;i<400005;i++)prog[i] = prog[i-1]+i;
    ll n,m;
    cin >>n>>m;
    for(ll i=0;i<n-1;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(ll i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(ll i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(ll i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    ll ind = 0;
    for(ll i=0;i<m;i++){
        for( ; edges[ind].first <= q[i].first && ind<m ; ind++){
            ll u = f(edges[ind].second.first);
            ll v = f(edges[ind].second.second);
            ans -= prog[ siz[u] - 1 ] + prog[ siz[v] - 1 ];
            ans += prog[ siz[u] + siz[v] - 1 ];
            unite(u,v);
        }
        print [ q[i].second ] = ans;
    }
    for(ll i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< ll , pair<ll,ll> > > edges;

pair<ll,ll> q[400005];

ll print [400005];

ll ans;

ll par [400005];
ll siz [400005];

ll prog [400005];

ll f(ll x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(ll x,ll y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(ll i=1;i<400005;i++)prog[i] = prog[i-1]+i;
    ll n,m;
    cin >>n>>m;
    for(ll i=0;i<n-1;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(ll i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(ll i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(ll i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    ll ind = 0;
    for(ll i=0;i<m;i++){
        for( ; edges[ind].first <= q[i].first && ind<n ; ind++){
            ll u = f(edges[ind].second.first);
            ll v = f(edges[ind].second.second);
            ans -= prog[ siz[u] - 1 ] + prog[ siz[v] - 1 ];
            ans += prog[ siz[u] + siz[v] - 1 ];
            unite(u,v);
        }
        print [ q[i].second ] = ans;
    }
    for(ll i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< ll , pair<ll,ll> > > edges;

pair<ll,ll> q[400005];

ll print [400005];

ll ans;

ll par [400005];
ll siz [400005];

ll prog [400005];

ll f(ll x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(ll x,ll y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(ll i=1;i<400005;i++)prog[i] = prog[i-1]+i;
    ll n,m;
    cin >>n>>m;
    for(ll i=0;i<n-1;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(ll i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(ll i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(ll i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    ll ind = 0;
    for(ll i=0;i<m;i++){
        for( ; edges[ind].first <= q[i].first && ind<=m ; ind++){
            ll u = f(edges[ind].second.first);
            ll v = f(edges[ind].second.second);
            ans -= prog[ siz[u] - 1 ] + prog[ siz[v] - 1 ];
            ans += prog[ siz[u] + siz[v] - 1 ];
            unite(u,v);
        }
        print [ q[i].second ] = ans;
    }
    for(ll i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< int , pair<int,int> > > edges;

pair<int,int> q[200005];

int print [200005];

ll ans;

int par [200005];
int siz [200005];

ll prog [200005];

int f(int x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(int x,int y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(int i=1;i<200005;i++)prog[i] = prog[i-1]+i;
    int n,m;
    cin >>n>>m;
    for(int i=0;i<n-1;i++){
        int u,v,w;
        cin>>u>>v>>w;
        edges.push_back({ w , {u,v}  });
    }
    for(int i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(int i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(int i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    int ind = 0;
    for(int i=0;i<m;i++){
        for(;edges[ind].first<=q[i].first && ind<=m;ind++){
            int u = f(edges[ind].second.first);
            int v = f(edges[ind].second.second);
            ans -= prog[ siz[ u ] - 1 ] + prog[ siz[ v ] - 1 ];
            ans += prog[ siz[ u ] + siz[ v ] - 1 ];
            unite(u,v);
        }
        print[ q[i].second ] = ans;
    }
    for(int i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< ll , pair<ll,ll> > > edges;

pair<ll,ll> q[400005];

ll prll [400005];

ll ans;

ll par [400005];
ll siz [400005];

ll prog [400005];

ll f(ll x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(ll x,ll y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(ll i=1;i<400005;i++)prog[i] = prog[i-1]+i;
    ll n,m;
    cin >>n>>m;
    for(ll i=0;i<n-1;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;
        v--;
        edges.push_back({ w , {u,v}  });
    }
    for(ll i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(ll i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    if(n==1){
        for(ll i=0;i<m;i++)cout<<"0 ";
        return 0;
    }
    sort(q,q+m);
    ll ind = 0;
    for(ll i=0;i<m;i++){
        for(;edges[ind].first<=q[i].first && ind<=m;ind++){
            ll u = f(edges[ind].second.first);
            ll v = f(edges[ind].second.second);
            unite(u,v);
            ans -= prog[ siz[ u ] - 1 ] + prog[ siz[ v ] - 1 ];
            ans += siz[f(u)];
        }
        prll[ q[i].second ] = ans;
    }
    for(ll i=0;i<m;i++)cout<<prll[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector < pair< int , pair<int,int> > > edges;

pair<int,int> q[200005];

int print [200005];

ll ans;

int par [200005];
int siz [200005];

ll prog [200005];

int f(int x){
    while(x!=par[x])x=par[x];
    return x;
}

void unite(int x,int y){
    if(siz[x]<siz[y])swap(x,y);
    siz[x] += siz[y];
    par[y] = par[x];
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(int i=1;i<200005;i++)prog[i] = prog[i-1]+i;
    int n,m;
    cin >>n>>m;
    for(int i=0;i<n-1;i++){
        int u,v,w;
        cin>>u>>v>>w;
        edges.push_back({ w , {u,v}  });
    }
    for(int i=0;i<n;i++){
        par[i]=i;
        siz[i]=1;
    }
    sort(edges.begin(),edges.end());
    for(int i=0;i<m;i++){
        cin >> q[i].first;
        q[i].second = i;
    }
    sort(q,q+m);
    int ind = 0;
    for(int i=0;i<m;i++){
        for(;edges[ind].first<=q[i].first && ind<=m;ind++){
            int u = f(edges[ind].second.first);
            int v = f(edges[ind].second.second);
            ans -= prog[ siz[ u ] - 1 ] + prog[ siz[ v ] - 1 ];
            ans += prog[ siz[ u ] + siz[ v ] - 1 ];
            unite(u,v);
        }
        print[ q[i].second ] = ans;
    }
    for(int i=0;i<m;i++)cout<<print[i]<<" ";

    return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
    	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x,y}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n+1);
    vlli size(n+1,1);
    for (int i=0;i<=n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        mp[weight]+=size1*size2;

    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        {
            cout<<dp[queries[i]]<<" ";
        }

    }

    cout<<endl;

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x-1,y-1}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n);
    vlli size(n,1);
    for (int i=0;i<n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200001,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<=m;i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    cout<<dp[queries[i]]<<" ";

    cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x,y}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n+1);
    vlli size(n+1,1);
    for (int i=0;i<=n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        {
            out<<dp[queries[i]]<<" ";
        }

    }

    cout<<endl;

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x-1,y-1}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n);
    vlli size(n,1);
    for (int i=0;i<n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    cout<<dp[queries[i]]<<" ";
    cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x-1,y-1}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n);
    vlli size(n,1);
    for (int i=0;i<n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200001,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    cout<<dp[queries[i]]<<" ";
    cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x-1,y-1}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n);
    vlli size(n,1);
    for (int i=0;i<n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        cout<<dp[queries[i]]<<" ";
        else
        {
            cout<<"\n";
            cout<<"brrr "<<"\n";
        }

    }

    cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x,y}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n+1);
    vlli size(n+1,1);
    for (int i=0;i<=n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        cout<<dp[queries[i]]<<" ";
        else
        {
            cout<<"\n";
            cout<<"brrr "<<"\n";
        }

    }

    cout<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x,y}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n+1);
    vlli size(n+1,1);
    for (int i=0;i<=n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        mp[weight]+=size1*size2;

    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        {
            cout<<dp[queries[i]]<<" ";
        }

    }

    cout<<endl;

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x,y}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n+1);
    vlli size(n+1,1);
    for (int i=0;i<=n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        {
            cout<<dp[queries[i]]<<" ";
        }

    }

    cout<<endl;

	return 0;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
typedef long long int lli;
#define scanvector(a,n) vector<lli>a; for(lli i=0;i<n;i++) { lli x ; cin>>x; a.push_back(x);}
#define scanmatrix(a,n,m) vector<vector<lli> > a(n,vector<lli> (m,0));for (lli i=0;i<n;i++){for (int j=0;j<m;j++){lli temp;cin>>temp;a[i][j]=temp;}}
#define pi 3.14159265358979
typedef long double ld;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<vector<int> > mati;
typedef vector<vector<lli> > matlli;
typedef vector<vector<bool > >matb;
#define For(i,a,m,l) for(int i=a;i<m;i+=l)

int find_set(int v, vlli &parent)
{
    if (v==parent[v])
    return v;
    return parent[v]=find_set(parent[v],parent);
}

void union_sets(lli a, lli b, vlli &parent,vlli &size)
{
    a=find_set(a,parent);
    b=find_set(b,parent);
    if (a!=b)
    {
        if (size[a]<size[b])
        {
            swap(a,b);
        }
        //now the size of a is bigger than b
        // we attach the smaller one to the bigger one
        parent[b]=a;
        size[a]+=size[b];

    }
}

int main()
{
	lli n,m;
    cin>>n>>m;
    vector<pair<lli,pair<lli,lli> > > inp;
    for (int i=0;i<n-1;i++)
    {
        lli x,y,z;
        cin>>x>>y>>z;
        inp.push_back({z,{x-1,y-1}});
    }
    sort(inp.begin(),inp.end());
    scanvector(queries, m);
    map<lli,lli> mp;
    for (int i=0;i<m;i++)
    {
        mp[queries[i]]=0;
    }
    vlli parents(n);
    vlli size(n,1);
    for (int i=0;i<n;i++)
    {
        parents[i]=i;
    }
    lli temp=0;
    for (int i=0;i<inp.size();i++)
    {
        lli weight=inp[i].first;
        lli u=inp[i].second.first;
        lli v=inp[i].second.second;
        lli size1=size[find_set(u,parents)];
        lli size2=size[find_set(v,parents)];
        union_sets(u,v,parents,size);

        auto it=mp.lower_bound(weight);
        it->second+=size1*size2;
    }
    vlli dp(200002,0);
    for (auto it=mp.begin();it!=mp.end();it++)
    dp[it->first]=it->second;

    for (int i=1;i<dp.size();i++)
    dp[i]+=dp[i-1];
    for (int i=0;i<m;i++)
    {
        if (queries[i]>=0 and queries[i]<=dp.size())
        cout<<dp[queries[i]]<<" ";
        else
        {
            cout<<"\n";
            cout<<"brrr "<<queries[i]<<"\n";
        }

    }

    cout<<"\n";

	return 0;
}


/*
	author: parv2809
*/

#include<bits/stdc++.h>
#define int long long int
#define mod 1000000007
#define inf 1e18
#define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
#define cin(t) int t;cin>>t
#define w(t) while(t--)
#define nl cout<<endl;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ft(i) (i&(-1*i))
#define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
#define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

int max(int a, int b) {
	if (a > b)
		return a;
	return b;
}

int min(int a, int b) {
	if (a < b)
		return a;
	return b;
}

int powmd(int a, int b) {
	if (b == 1)	return a;
	if (b == 0)	return 1;

	int half = powmd(a, b / 2);
	int ans = half * half;
	ans %= mod;
	if (b & 1) {
		ans *= a;
	}
	ans %= mod;
	return ans;
}

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

}

struct edge {
	int a, b, wt;

	bool operator < (const edge& e) const {
		return wt < e.wt;
	}

};

edge edges[200005];
int parent[200005];
int size[200005];

struct dsu {

	void init(int n) {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int get_parent(int x) {
		if (parent[x] == x)
			return x;

		return parent[x] = get_parent(parent[x]);
	}

	int _union(int a, int b) {
		int a_parent = get_parent(a);
		int b_parent = get_parent(b);

		int ans = size[b_parent] * size[a_parent];
		size[b_parent] += size[a_parent];
		parent[a_parent] = b_parent;
		return ans;
	}

};

int32_t main() {

	fastIO();
	int t = 1;
	w(t) {

		int n, q;
		cin >> n >> q;
		dsu graph;
		graph.init(n);
		int a, b, wt;
		for (int i = 0; i < n - 1; i++) {
			cin >> a >> b >> wt;
			a--, b--;
			edges[i].a = a;
			edges[i].b = b;
			edges[i].wt = wt;
		}

		sort(edges, edges + n - 1);

		map<int, int> m;
		for (int i = 0; i < n - 1; i++) {
			m[edges[i].wt] += graph._union(edges[i].a, edges[i].b);
		}

		vector<pii > prefix(m.size());
		int sum = 0;
		for (auto it : m) {
			sum += it.second;
			prefix.pb(make_pair(it.first, sum));
		}

		int query;
		pii curr_pair;
		for (int i = 0; i < q; i++) {
			cin >> query;
			curr_pair.first = query;
			curr_pair.second = inf;
			auto it = upper_bound(prefix.begin(), prefix.end(), curr_pair);
			if (it == prefix.begin()) {
				cout << 0 << " ";
			}
			else {
				it--;
				cout << it->second << " ";
			}
		}
		nl;
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;


/*
	author: parv2809
*/

#include<bits/stdc++.h>
// #define int long long int
// #define mod 1000000007
#define inf 1e9
// #define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
// #define cin(t) int t;cin>>t
#define w(t) while(t--)
// #define nl cout<<endl;
// #define pii pair<int,int>
// #define mp make_pair
// #define pb push_back
// #define ft(i) (i&(-1*i))
// #define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
// #define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

}

struct edge {
	int wt, a, b;

	bool operator < (const edge& e) const {
		return wt < e.wt;
	}
};

struct dsu {

	vector<int> parent, size;
	void init(int n) {
		parent.resize(n);
		size.resize(n);
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int get_par(int a) {
		if (parent[a] == a)
			return a;

		return parent[a] = get_par(parent[a]);
	}

	int _union(int a, int b) {

		int a_par = get_par(a);
		int b_par = get_par(b);

		int ans = (size[a_par] * size[b_par]);

		parent[a_par] = parent[b_par];
		size[b_par] += size[a_par];
		return ans;
	}

};

int main() {

	// fastIO();
	int t = 1;
	w(t) {
		int n, q, m;
		dsu graph;
		graph.init(n);
		cin >> n >> q;
		m = n - 1;
		vector<edge> edges(m);

		for (int i = 0; i < m; i++) {
			cin >> edges[i].a >> edges[i].b >> edges[i].wt;
			edges[i].a--;
			edges[i].b--;
		}

		sort(edges.begin(), edges.end());

		map<int, int> cm;
		for (auto x : edges) {
			int curr = graph._union(x.a, x.b);
			cm[x.wt] += curr;
		}

		vector<pair<int, int> > prefix_arr;
		int prefix_val = 0;

		for (auto x : cm) {
			prefix_val += x.second;
			prefix_arr.push_back(make_pair(x.first, prefix_val));
		}

		vector<int> queries(q);
		for (int i = 0; i < q; i++) {
			cin >> queries[i];
			pair<int, int> curr_pair;
			curr_pair.first = queries[i];
			curr_pair.second = inf;
			auto it = upper_bound(prefix_arr.begin(), prefix_arr.end(), curr_pair);

			if (it == prefix_arr.begin())
				cout << 0 << " ";
			else {
				it--;
				cout << it->second << " ";
			}
		}
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;


/*
	author: parv2809
*/

#include<bits/stdc++.h>
// #define int long long int
#define mod 1000000007
#define inf 1e9
#define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
#define cin(t) int t;cin>>t
#define w(t) while(t--)
#define nl cout<<endl;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ft(i) (i&(-1*i))
#define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
#define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

int max(int a, int b) {
	if (a > b)
		return a;
	return b;
}

int min(int a, int b) {
	if (a < b)
		return a;
	return b;
}

int powmd(int a, int b) {
	if (b == 1)	return a;
	if (b == 0)	return 1;

	int half = powmd(a, b / 2);
	int ans = half * half;
	ans %= mod;
	if (b & 1) {
		ans *= a;
	}
	ans %= mod;
	return ans;
}

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

}

struct edge {
	int wt, a, b;

	bool operator < (const edge& e) const {
		return wt < e.wt;
	}
};

struct dsu {

	vector<int> parent, size;
	void init(int n) {
		parent.resize(n);
		size.resize(n);
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int get_par(int a) {
		if (parent[a] == a)
			return a;

		return parent[a] = get_par(parent[a]);
	}

	int _union(int a, int b) {

		int a_par = get_par(a);
		int b_par = get_par(b);

		int ans = (size[a_par] * size[b_par]);

		parent[a_par] = parent[b_par];
		size[b_par] += size[a_par];
		return ans;
	}

};

int32_t main() {

	fastIO();
	int t = 1;
	w(t) {
		int n, q, m;
		dsu graph;
		graph.init(n);
		cin >> n >> q;
		m = n - 1;
		vector<edge> edges(m);

		for (int i = 0; i < m; i++) {
			cin >> edges[i].a >> edges[i].b >> edges[i].wt;
			edges[i].a--;
			edges[i].b--;
		}

		sort(edges.begin(), edges.end());

		map<int, int> cm;
		for (auto x : edges) {
			int curr = graph._union(x.a, x.b);
			cm[x.wt] += curr;
		}

		vector<pair<int, int> > prefix_arr;
		int prefix_val = 0;

		for (auto x : cm) {
			prefix_val += x.second;
			prefix_arr.push_back(make_pair(x.first, prefix_val));
		}

		vector<int> queries(q);
		for (int i = 0; i < q; i++) {
			cin >> queries[i];
			pair<int, int> curr_pair;
			curr_pair.first = queries[i];
			curr_pair.second = inf;
			auto it = upper_bound(prefix_arr.begin(), prefix_arr.end(), curr_pair);

			if (it == prefix_arr.begin())
				cout << 0 << " ";
			else {
				it--;
				cout << it->second << " ";
			}
		}
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;


/*
	author: parv2809
*/

#include<bits/stdc++.h>
// #define int long long int
// #define mod 1000000007
#define inf 1e9
// #define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
// #define cin(t) int t;cin>>t
#define w(t) while(t--)
// #define nl cout<<endl;
// #define pii pair<int,int>
// #define mp make_pair
// #define pb push_back
// #define ft(i) (i&(-1*i))
// #define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
// #define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

}

struct edge {
	int wt, a, b;

	bool operator < (const edge& e) const {
		return wt < e.wt;
	}
};

struct dsu {

	vector<int> parent, size;
	void init(int n) {
		for (int i = 0; i < n; i++) {
			parent.push_back(i);
			size.push_back(1);
		}
	}

	int get_par(int a) {
		if (parent[a] == a)
			return a;

		return parent[a] = get_par(parent[a]);
	}

	int _union(int a, int b) {

		int a_par = get_par(a);
		int b_par = get_par(b);

		int ans = (size[a_par] * size[b_par]);

		parent[a_par] = parent[b_par];
		size[b_par] += size[a_par];
		return ans;
	}

};

int main() {

	// fastIO();
	int t = 1;
	w(t) {
		int n, q, m;
		dsu graph;
		graph.init(n);
		cin >> n >> q;
		m = n - 1;
		vector<edge> edges(m);

		for (int i = 0; i < m; i++) {
			cin >> edges[i].a >> edges[i].b >> edges[i].wt;
			edges[i].a--;
			edges[i].b--;
		}

		sort(edges.begin(), edges.end());

		map<int, int> cm;
		for (auto x : edges) {
			int curr = graph._union(x.a, x.b);
			cm[x.wt] += curr;
		}

		vector<pair<int, int> > prefix_arr;
		int prefix_val = 0;

		for (auto x : cm) {
			prefix_val += x.second;
			prefix_arr.push_back(make_pair(x.first, prefix_val));
		}

		pair<int, int> curr_pair;
		int curr_query;
		for (int i = 0; i < q; i++) {
			cin >> curr_query;
			curr_pair.first = curr_query;
			curr_pair.second = inf;
			auto it = upper_bound(prefix_arr.begin(), prefix_arr.end(), curr_pair);

			if (it == prefix_arr.begin())
				cout << 0 << " ";
			else {
				it--;
				cout << it->second << " ";
			}
		}
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;


/*
	author: parv2809
*/

#include<bits/stdc++.h>
#define int long long int
#define mod 1000000007
#define inf 1e18
#define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
#define cin(t) int t;cin>>t
#define w(t) while(t--)
#define nl cout<<endl;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ft(i) (i&(-1*i))
#define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
#define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

int max(int a, int b) {
	if (a > b)
		return a;
	return b;
}

int min(int a, int b) {
	if (a < b)
		return a;
	return b;
}

int powmd(int a, int b) {
	if (b == 1)	return a;
	if (b == 0)	return 1;

	int half = powmd(a, b / 2);
	int ans = half * half;
	ans %= mod;
	if (b & 1) {
		ans *= a;
	}
	ans %= mod;
	return ans;
}

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

}

struct edge {
	int a, b, wt;

	bool operator < (const edge& e) const {
		return wt < e.wt;
	}

};

edge edges[200005];
int parent[200005];
int sizes[200005];

struct dsu {

	void init(int n) {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			sizes[i] = 1;
		}
	}

	int get_parent(int x) {
		if (parent[x] == x)
			return x;

		return parent[x] = get_parent(parent[x]);
	}

	int _union(int a, int b) {
		int a_parent = get_parent(a);
		int b_parent = get_parent(b);

		int ans = sizes[b_parent] * sizes[a_parent];
		sizes[b_parent] += sizes[a_parent];
		parent[a_parent] = b_parent;
		return ans;
	}

};

int32_t main() {

	fastIO();
	int t = 1;
	w(t) {

		int n, q;
		cin >> n >> q;
		dsu graph;
		graph.init(n);
		int a, b, wt;
		for (int i = 0; i < n - 1; i++) {
			cin >> a >> b >> wt;
			a--, b--;
			edges[i].a = a;
			edges[i].b = b;
			edges[i].wt = wt;
		}

		sort(edges, edges + n - 1);

		map<int, int> m;
		for (int i = 0; i < n - 1; i++) {
			m[edges[i].wt] += graph._union(edges[i].a, edges[i].b);
		}

		vector<pii > prefix(m.size());
		int sum = 0;
		for (auto it : m) {
			sum += it.second;
			prefix.pb(make_pair(it.first, sum));
		}

		int query;
		pii curr_pair;
		for (int i = 0; i < q; i++) {
			cin >> query;
			curr_pair.first = query;
			curr_pair.second = inf;
			auto it = upper_bound(prefix.begin(), prefix.end(), curr_pair);
			if (it == prefix.begin()) {
				cout << 0 << " ";
			}
			else {
				it--;
				cout << it->second << " ";
			}
		}
		nl;
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;


/*
	author: parv2809
*/

#include<bits/stdc++.h>
// #define int long long int
#define mod 1000000007
#define inf 1e9
#define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
#define cin(t) int t;cin>>t
#define w(t) while(t--)
#define nl cout<<endl;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ft(i) (i&(-1*i))
#define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
#define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

}

struct edge {
	int wt, a, b;

	bool operator < (const edge& e) const {
		return wt < e.wt;
	}
};

struct dsu {

	vector<int> parent, size;
	void init(int n) {
		parent.resize(n);
		size.resize(n);
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int get_par(int a) {
		if (parent[a] == a)
			return a;

		return parent[a] = get_par(parent[a]);
	}

	int _union(int a, int b) {

		int a_par = get_par(a);
		int b_par = get_par(b);

		int ans = (size[a_par] * size[b_par]);

		parent[a_par] = parent[b_par];
		size[b_par] += size[a_par];
		return ans;
	}

};

int main() {

	fastIO();
	int t = 1;
	w(t) {
		int n, q, m;
		dsu graph;
		graph.init(n);
		cin >> n >> q;
		m = n - 1;
		vector<edge> edges(m);

		for (int i = 0; i < m; i++) {
			cin >> edges[i].a >> edges[i].b >> edges[i].wt;
			edges[i].a--;
			edges[i].b--;
		}

		sort(edges.begin(), edges.end());

		map<int, int> cm;
		for (auto x : edges) {
			int curr = graph._union(x.a, x.b);
			cm[x.wt] += curr;
		}

		vector<pair<int, int> > prefix_arr;
		int prefix_val = 0;

		for (auto x : cm) {
			prefix_val += x.second;
			prefix_arr.push_back(make_pair(x.first, prefix_val));
		}

		vector<int> queries(q);
		for (int i = 0; i < q; i++) {
			cin >> queries[i];
			pair<int, int> curr_pair;
			curr_pair.first = queries[i];
			curr_pair.second = inf;
			auto it = upper_bound(prefix_arr.begin(), prefix_arr.end(), curr_pair);

			if (it == prefix_arr.begin())
				cout << 0 << " ";
			else {
				it--;
				cout << it->second << " ";
			}
		}
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;