#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifndef LOCAL_PROJECT
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
#else
#define debug(x)   {cerr <<#x<<" = " <<x<<"\n"; }
#define debuga(a, n) {cerr << #a << " = "; forn(i,n) cerr<<a[i]<<' '; cerr<<endl;}
#define debug2(x, y)       {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z)    {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define forit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int nextint(){ int x; scanf("%d",&x);   return x; }
inline ll  nextll() { ll  x; scanf("%lld",&x); return x; }

#define gc getchar
template <typename T> void scanint(T &x) {
T c = gc(); while(((c < 48) || (c > 57)) && (c!='-')) c = gc();
bool neg = false; if(c == '-') {neg = true; c = gc();} x = 0; for(;c < 48 || c > 57;c=gc());
for(;c > 47 && c < 58;c=gc()) x = (x*10) + (c - 48); if(neg) x = -x;
}
// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

// lambda exp auto  square = [](int inp) { return inp * inp; } ;

template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
os<<"("<<t.first<<", "<<t.second<<")";
return os;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) {
    out << v[i]; if (i != last) out << ", "; } out << "]"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const set<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const multiset<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T, typename VAL> ostream& operator<< (ostream& out, const map<T,VAL>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}

template <typename Arg, typename... Args>
void ptrace(std::ostream& out, Arg&& arg, Args&&... args)
{
    out<<"<< ";
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ", " << std::forward<Args>(args)),0)...};
    out<<" >>\n";
}
#ifndef LOCAL_PROJECT
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
}
#else
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
    cerr<<"<< ";
    cerr << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cerr << ", " << std::forward<Args>(args)),0)...};
    cerr<<" >>\n";
}
#endif

template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

void _build(){

}

int n, m;
vpii g[maxn];
vi adj[maxn];
int sub[maxn], lo[maxn], hi[maxn], seen[maxn];
map<pii, int> weights;

int dfs0(int u, int from){
    sub[u] = 1;
    for(int nei: adj[u]){
        if(nei != from){
            sub[u] += dfs0(nei, u);
        }
    }
    return sub[u];
}

int dfs1(int u, int from){
    lo[u] = oo;
    //if(from != -1) lo[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmin(lo[u], dfs1(nei, u));
            mmin(lo[u], we);
        }
    }
    return lo[u];
}

int dfs2(int u, int from){
    hi[u] = -1;
    //if(from != -1) hi[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmax(hi[u], dfs2(nei, u));
            mmax(hi[u], we);
        }
    }
    return hi[u];
}

ll dfs3(int u, int from, int q){
    ll ans = 0;
    if(hi[u] <= q){
        return sub[u] * 1LL * (sub[u] - 1) / 2;
    }
    if(lo[u] <= q){
        for(int nei: adj[u]){
            if(nei != from){
                ans += dfs3(nei, u, q);
            }
        }
    }
    return ans;
}

int dfs4(int u, int q){
    if(seen[u]) return 0;
    seen[u]++;
    int ans = 1;
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(we <= q){
            ans += dfs4(nei, q);
        }
    }
    return ans;
}

class UnionFind { // OOP style
public:
vi p, rank, big; // remember: vi is vector<int>
map<int,int> id;
UnionFind(int N) { rank.assign(N, 0); big.assign(N, 1);
p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) { // if from different set
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]){
  p[y] = x; // rank keeps the tree short
  big[x] += big[y];
  id[x] = big[x];
  id.erase(y);
}
else { p[x] = y;
    big[y] += big[x];
    id[y] = big[y];
    id.erase(x);
if (rank[x] == rank[y]) rank[y]++; }
} } };

void _solve(){
    read(n, m);
    vvi edges;
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        edges.pb({w,v,u});
    }
    sort(all(edges));

    vpii qs;
    vll ans(m);
    forn(ii,m){
        int q; read(q);
        qs.pb({q, ii});
    }
    sort(all(qs));
    int j = 0;
    UnionFind forest(n);

    for(pii p: qs){
        while(j < n-1 and edges[j][0] <= p.first){
            forest.unionSet(edges[j][1], edges[j][2]);
            j++;
        }
        ll sol = 0;
        for(const auto &b: forest.id){
            sol += (b.se - 1) * 1LL * b.se;
        }
        ans[p.se] = sol / 2;
    }
    outv(ans);
    //forn(i,m) cout << ans[i] << endl;
}

void _solve0(){
    //mem(sub, 0);
    read(n, m);
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        //weights[upair(u,v)] = w;
        adj[u].pb(v);
        adj[v].pb(u);
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    //dfs0(0, -1);
    //dfs1(0, -1);
    //dfs2(0, -1);
    //forn(i,n) trace(i+1, sub[i], lo[i], hi[i]);
    forn(ii,m){
        int q; read(q);
        mem(seen, 0);

        ll ans = 0;// dfs3(0, -1, q);
        forn(i,n) if(!seen[i]){
            ll x = dfs4(i, q);
            ans += x * (x-1) / 2;
        }
        cout << ans << endl;
    }
}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(GOOGLE) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifndef LOCAL_PROJECT
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
#else
#define debug(x)   {cerr <<#x<<" = " <<x<<"\n"; }
#define debuga(a, n) {cerr << #a << " = "; forn(i,n) cerr<<a[i]<<' '; cerr<<endl;}
#define debug2(x, y)       {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z)    {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define forit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int nextint(){ int x; scanf("%d",&x);   return x; }
inline ll  nextll() { ll  x; scanf("%lld",&x); return x; }

#define gc getchar
template <typename T> void scanint(T &x) {
T c = gc(); while(((c < 48) || (c > 57)) && (c!='-')) c = gc();
bool neg = false; if(c == '-') {neg = true; c = gc();} x = 0; for(;c < 48 || c > 57;c=gc());
for(;c > 47 && c < 58;c=gc()) x = (x*10) + (c - 48); if(neg) x = -x;
}
// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

// lambda exp auto  square = [](int inp) { return inp * inp; } ;

template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
os<<"("<<t.first<<", "<<t.second<<")";
return os;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) {
    out << v[i]; if (i != last) out << ", "; } out << "]"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const set<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const multiset<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T, typename VAL> ostream& operator<< (ostream& out, const map<T,VAL>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}

template <typename Arg, typename... Args>
void ptrace(std::ostream& out, Arg&& arg, Args&&... args)
{
    out<<"<< ";
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ", " << std::forward<Args>(args)),0)...};
    out<<" >>\n";
}
#ifndef LOCAL_PROJECT
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
}
#else
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
    cerr<<"<< ";
    cerr << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cerr << ", " << std::forward<Args>(args)),0)...};
    cerr<<" >>\n";
}
#endif

template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

void _build(){

}

int n, m;

class UnionFind { // OOP style
public:
vi p, rank, big; // remember: vi is vector<int>
ll ans = 0;
UnionFind(int N) { rank.assign(N, 0); big.assign(N, 1);
p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
    if (!isSameSet(i, j)) { // if from different set
        int x = findSet(i), y = findSet(j);
        ans -= (big[x]) * 1ll * (big[x] - 1) / 2;
        ans -= (big[y]) * 1ll * (big[y] - 1) / 2;
        if (rank[x] > rank[y]){
          p[y] = x; // rank keeps the tree short
          big[x] += big[y];
          ans += (big[x]) * 1ll * (big[x] - 1) / 2;
        }
        else { p[x] = y;
            big[y] += big[x];
            ans += (big[y]) * 1ll * (big[y] - 1) / 2;
        if (rank[x] == rank[y]) rank[y]++; }

    }
} };

void _solve(){
    read(n, m);
    vvi edges;
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        edges.pb({w,v,u});
    }
    sort(all(edges));

    vpii qs;
    vll ans(m);
    forn(ii,m){
        int q; read(q);
        qs.pb({q, ii});
    }
    sort(all(qs));
    int j = 0;
    UnionFind forest(n);

    for(pii p: qs){
        while(j < n-1 and edges[j][0] <= p.first){
            forest.unionSet(edges[j][1], edges[j][2]);
            j++;
        }

        ans[p.se] = forest.ans;
    }
    outv(ans);
}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(GOOGLE) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifndef LOCAL_PROJECT
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
#else
#define debug(x)   {cerr <<#x<<" = " <<x<<"\n"; }
#define debuga(a, n) {cerr << #a << " = "; forn(i,n) cerr<<a[i]<<' '; cerr<<endl;}
#define debug2(x, y)       {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z)    {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define forit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int nextint(){ int x; scanf("%d",&x);   return x; }
inline ll  nextll() { ll  x; scanf("%lld",&x); return x; }

#define gc getchar
template <typename T> void scanint(T &x) {
T c = gc(); while(((c < 48) || (c > 57)) && (c!='-')) c = gc();
bool neg = false; if(c == '-') {neg = true; c = gc();} x = 0; for(;c < 48 || c > 57;c=gc());
for(;c > 47 && c < 58;c=gc()) x = (x*10) + (c - 48); if(neg) x = -x;
}
// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

// lambda exp auto  square = [](int inp) { return inp * inp; } ;

template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
os<<"("<<t.first<<", "<<t.second<<")";
return os;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) {
    out << v[i]; if (i != last) out << ", "; } out << "]"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const set<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const multiset<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T, typename VAL> ostream& operator<< (ostream& out, const map<T,VAL>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}

template <typename Arg, typename... Args>
void ptrace(std::ostream& out, Arg&& arg, Args&&... args)
{
    out<<"<< ";
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ", " << std::forward<Args>(args)),0)...};
    out<<" >>\n";
}
#ifndef LOCAL_PROJECT
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
}
#else
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
    cerr<<"<< ";
    cerr << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cerr << ", " << std::forward<Args>(args)),0)...};
    cerr<<" >>\n";
}
#endif

template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

void _build(){

}

int n, m;
vpii g[maxn];
vi adj[maxn];
int sub[maxn], lo[maxn], hi[maxn], seen[maxn];
map<pii, int> weights;

int dfs0(int u, int from){
    sub[u] = 1;
    for(int nei: adj[u]){
        if(nei != from){
            sub[u] += dfs0(nei, u);
        }
    }
    return sub[u];
}

int dfs1(int u, int from){
    lo[u] = oo;
    //if(from != -1) lo[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmin(lo[u], dfs1(nei, u));
            mmin(lo[u], we);
        }
    }
    return lo[u];
}

int dfs2(int u, int from){
    hi[u] = -1;
    //if(from != -1) hi[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmax(hi[u], dfs2(nei, u));
            mmax(hi[u], we);
        }
    }
    return hi[u];
}

ll dfs3(int u, int from, int q){
    ll ans = 0;
    if(hi[u] <= q){
        return sub[u] * 1LL * (sub[u] - 1) / 2;
    }
    if(lo[u] <= q){
        for(int nei: adj[u]){
            if(nei != from){
                ans += dfs3(nei, u, q);
            }
        }
    }
    return ans;
}

int dfs4(int u, int q){
    if(seen[u]) return 0;
    seen[u]++;
    int ans = 1;
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(we <= q){
            ans += dfs4(nei, q);
        }
    }
    return ans;
}

class UnionFind { // OOP style
public:
vi p, rank, big; // remember: vi is vector<int>
ll ans = 0;
UnionFind(int N) { rank.assign(N, 0); big.assign(N, 1);
p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
    if (!isSameSet(i, j)) { // if from different set

        int x = findSet(i), y = findSet(j);
        ans -= (big[x]) * 1ll * (big[x] - 1) / 2;
        ans -= (big[y]) * 1ll * (big[y] - 1) / 2;
        if (rank[x] > rank[y]){
          p[y] = x; // rank keeps the tree short
          big[x] += big[y];
          ans += (big[x]) * 1ll * (big[x] - 1) / 2;
          //ans += x * 1LL * (x-1) / 2;
          //ans -= y * 1LL * (y-1) / 2;
          //id[x] = big[x];
          //id.erase(y);
        }
        else { p[x] = y;
            big[y] += big[x];
            ans += (big[y]) * 1ll * (big[y] - 1) / 2;
            //ans += y * 1LL * (y-1) / 2;
            //ans -= x * 1LL * (x-1) / 2;
            //id[y] = big[y];
            //id.erase(x);
        if (rank[x] == rank[y]) rank[y]++; }
        //ans += (big[x] + big[y]) * 1ll * (big[x] + big[y] - 1) / 2;

    }
} };

void _solve(){
    read(n, m);
    vvi edges;
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        edges.pb({w,v,u});
    }
    sort(all(edges));

    vpii qs;
    vll ans(m);
    forn(ii,m){
        int q; read(q);
        qs.pb({q, ii});
    }
    sort(all(qs));
    int j = 0;
    UnionFind forest(n);

    for(pii p: qs){
        while(j < n-1 and edges[j][0] <= p.first){
            forest.unionSet(edges[j][1], edges[j][2]);
            j++;
        }

        ans[p.se] = forest.ans;
    }
    outv(ans);
    //forn(i,m) cout << ans[i] << endl;
}

void _solve0(){
    //mem(sub, 0);
    read(n, m);
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        //weights[upair(u,v)] = w;
        adj[u].pb(v);
        adj[v].pb(u);
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    //dfs0(0, -1);
    //dfs1(0, -1);
    //dfs2(0, -1);
    //forn(i,n) trace(i+1, sub[i], lo[i], hi[i]);
    forn(ii,m){
        int q; read(q);
        mem(seen, 0);

        ll ans = 0;// dfs3(0, -1, q);
        forn(i,n) if(!seen[i]){
            ll x = dfs4(i, q);
            ans += x * (x-1) / 2;
        }
        cout << ans << endl;
    }
}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(GOOGLE) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifndef LOCAL_PROJECT
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
#else
#define debug(x)   {cerr <<#x<<" = " <<x<<"\n"; }
#define debuga(a, n) {cerr << #a << " = "; forn(i,n) cerr<<a[i]<<' '; cerr<<endl;}
#define debug2(x, y)       {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z)    {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define forit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int nextint(){ int x; scanf("%d",&x);   return x; }
inline ll  nextll() { ll  x; scanf("%lld",&x); return x; }

#define gc getchar
template <typename T> void scanint(T &x) {
T c = gc(); while(((c < 48) || (c > 57)) && (c!='-')) c = gc();
bool neg = false; if(c == '-') {neg = true; c = gc();} x = 0; for(;c < 48 || c > 57;c=gc());
for(;c > 47 && c < 58;c=gc()) x = (x*10) + (c - 48); if(neg) x = -x;
}
// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

// lambda exp auto  square = [](int inp) { return inp * inp; } ;

template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
os<<"("<<t.first<<", "<<t.second<<")";
return os;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) {
    out << v[i]; if (i != last) out << ", "; } out << "]"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const set<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const multiset<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T, typename VAL> ostream& operator<< (ostream& out, const map<T,VAL>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}

template <typename Arg, typename... Args>
void ptrace(std::ostream& out, Arg&& arg, Args&&... args)
{
    out<<"<< ";
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ", " << std::forward<Args>(args)),0)...};
    out<<" >>\n";
}
#ifndef LOCAL_PROJECT
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
}
#else
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
    cerr<<"<< ";
    cerr << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cerr << ", " << std::forward<Args>(args)),0)...};
    cerr<<" >>\n";
}
#endif

template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

void _build(){

}

int n, m;
vpii g[maxn];
vi adj[maxn];
int sub[maxn], lo[maxn], hi[maxn], seen[maxn];
map<pii, int> weights;

int dfs0(int u, int from){
    sub[u] = 1;
    for(int nei: adj[u]){
        if(nei != from){
            sub[u] += dfs0(nei, u);
        }
    }
    return sub[u];
}

int dfs1(int u, int from){
    lo[u] = oo;
    //if(from != -1) lo[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmin(lo[u], dfs1(nei, u));
            mmin(lo[u], we);
        }
    }
    return lo[u];
}

int dfs2(int u, int from){
    hi[u] = -1;
    //if(from != -1) hi[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmax(hi[u], dfs2(nei, u));
            mmax(hi[u], we);
        }
    }
    return hi[u];
}

ll dfs3(int u, int from, int q){
    ll ans = 0;
    if(hi[u] <= q){
        return sub[u] * 1LL * (sub[u] - 1) / 2;
    }
    if(lo[u] <= q){
        for(int nei: adj[u]){
            if(nei != from){
                ans += dfs3(nei, u, q);
            }
        }
    }
    return ans;
}

int dfs4(int u, int q){
    if(seen[u]) return 0;
    seen[u]++;
    int ans = 1;
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(we <= q){
            ans += dfs4(nei, q);
        }
    }
    return ans;
}

class UnionFind { // OOP style
vi p, rank, big; // remember: vi is vector<int>
map<int,int> id;
UnionFind(int N) { rank.assign(N, 0); big.assign(N, 1);
p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) { // if from different set
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]){
  p[y] = x; // rank keeps the tree short
  big[x] += big[y];
  id[x] = big[x];
  id.erase(y);
}
else { p[x] = y;
    big[y] += big[x];
    id[y] = big[y];
    id.erase(x);
if (rank[x] == rank[y]) rank[y]++; }
} } };

void _solve(){
    read(n, m);
    vvi edges;
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        edges.pb({w,v,u});
    }
    sort(all(edges));

    vpii qs;
    vi ans(m);
    forn(ii,m){
        int q; read(q);
        qs.pb({q, ii});
    }
    sort(all(qs));
    int j = 0;
    UnionFind forest(n);

    for(pii p: qs){
        while(j < n-1 and edges[j][0] <= p.first){
            forest.unionSet(edges[j][1], edges[j][2]);
            j++;
        }
        ll sol = 0;
        for(const auto &b: forest.id){
            sol += (b.se - 1) * 1LL * b.se;
        }
        ans[p.se] = sol;
    }
    outv(ans);
    //forn(i,m) cout << ans[i] << endl;
}

void _solve0(){
    //mem(sub, 0);
    read(n, m);
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        //weights[upair(u,v)] = w;
        adj[u].pb(v);
        adj[v].pb(u);
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    //dfs0(0, -1);
    //dfs1(0, -1);
    //dfs2(0, -1);
    //forn(i,n) trace(i+1, sub[i], lo[i], hi[i]);
    forn(ii,m){
        int q; read(q);
        mem(seen, 0);

        ll ans = 0;// dfs3(0, -1, q);
        forn(i,n) if(!seen[i]){
            ll x = dfs4(i, q);
            ans += x * (x-1) / 2;
        }
        cout << ans << endl;
    }
}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(GOOGLE) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifndef LOCAL_PROJECT
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
#else
#define debug(x)   {cerr <<#x<<" = " <<x<<"\n"; }
#define debuga(a, n) {cerr << #a << " = "; forn(i,n) cerr<<a[i]<<' '; cerr<<endl;}
#define debug2(x, y)       {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z)    {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n) for(int i=n-1;i>=0;i--)
#define forbn(i,n,m) for(int i=m-1;i>=(int)(n);i--)
#define forit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int nextint(){ int x; scanf("%d",&x);   return x; }
inline ll  nextll() { ll  x; scanf("%lld",&x); return x; }

#define gc getchar
template <typename T> void scanint(T &x) {
T c = gc(); while(((c < 48) || (c > 57)) && (c!='-')) c = gc();
bool neg = false; if(c == '-') {neg = true; c = gc();} x = 0; for(;c < 48 || c > 57;c=gc());
for(;c > 47 && c < 58;c=gc()) x = (x*10) + (c - 48); if(neg) x = -x;
}
// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

// lambda exp auto  square = [](int inp) { return inp * inp; } ;

template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
os<<"("<<t.first<<", "<<t.second<<")";
return os;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) {
    out << v[i]; if (i != last) out << ", "; } out << "]"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const set<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const multiset<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T, typename VAL> ostream& operator<< (ostream& out, const map<T,VAL>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void finalize(T x){
    cout << x << endl;
    exit(0);
}
void yesno(int x, int last = 1){
    cout << (x ? "YES" : "NO") << endl;
    if(last) exit(0);
}

template <typename Arg, typename... Args>
void ptrace(std::ostream& out, Arg&& arg, Args&&... args)
{
    out<<"<< ";
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ", " << std::forward<Args>(args)),0)...};
    out<<" >>\n";
}
#ifndef LOCAL_PROJECT
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
}
#else
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
    cerr<<"<< ";
    cerr << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cerr << ", " << std::forward<Args>(args)),0)...};
    cerr<<" >>\n";
}
#endif

template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

void _build(){

}

int n, m;
vpii g[maxn];
vi adj[maxn];
int sub[maxn], lo[maxn], hi[maxn], seen[maxn];
map<pii, int> weights;

int dfs0(int u, int from){
    sub[u] = 1;
    for(int nei: adj[u]){
        if(nei != from){
            sub[u] += dfs0(nei, u);
        }
    }
    return sub[u];
}

int dfs1(int u, int from){
    lo[u] = oo;
    //if(from != -1) lo[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmin(lo[u], dfs1(nei, u));
            mmin(lo[u], we);
        }
    }
    return lo[u];
}

int dfs2(int u, int from){
    hi[u] = -1;
    //if(from != -1) hi[u] = weights[upair(u,from)];
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(nei != from){
            mmax(hi[u], dfs2(nei, u));
            mmax(hi[u], we);
        }
    }
    return hi[u];
}

ll dfs3(int u, int from, int q){
    ll ans = 0;
    if(hi[u] <= q){
        return sub[u] * 1LL * (sub[u] - 1) / 2;
    }
    if(lo[u] <= q){
        for(int nei: adj[u]){
            if(nei != from){
                ans += dfs3(nei, u, q);
            }
        }
    }
    return ans;
}

int dfs4(int u, int q){
    if(seen[u]) return 0;
    seen[u]++;
    int ans = 1;
    for(pii p: g[u]){
        int nei = p.fi, we = p.se;
        if(we <= q){
            ans += dfs4(nei, q);
        }
    }
    return ans;
}

void _solve(){
    //mem(sub, 0);
    read(n, m);
    forn(i,n-1){
        int u,v,w;
        read(u,v,w); u--; v--;
        weights[upair(u,v)] = w;
        adj[u].pb(v);
        adj[v].pb(u);
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    //dfs0(0, -1);
    //dfs1(0, -1);
    //dfs2(0, -1);
    //forn(i,n) trace(i+1, sub[i], lo[i], hi[i]);
    forn(ii,m){
        int q; read(q);
        mem(seen, 0);

        ll ans = 0;// dfs3(0, -1, q);
        forn(i,n) if(!seen[i]){
            ll x = dfs4(i, q);
            ans += x * (x-1) / 2;
        }
        cout << ans << endl;
    }
}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(GOOGLE) cin>>qq;
    forn(i,qq){
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include <bits/stdc++.h>
using namespace std;

#define LOCAL_PROJECTx

#ifndef LOCAL_PROJECT
#define debug(x)
#define debuga(a, n)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, w)
#else
#define debug(x)   {cerr <<#x<<" = " <<x<<"\n"; }
#define debuga(a, n) {cerr << #a << " = "; forn(i,n) cerr<<a[i]<<' '; cerr<<endl;}
#define debug2(x, y)       {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z)    {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";}
#endif

#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sz(c) ((int) c.size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define forn(i,m) for(int i=0;i<(int)(m);i++)
#define fornn(i,n,m) for(int i=n;i<(int)(m);i++)
#define forb(i,n,m) for(int i=n;i>=(int)(m);i--)
#define forit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
//#define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl;
#define upair(a,b)  make_pair(min(a,b),max(a,b))
#define fi first
#define se second
#define mmax(a,b) a = (a) > (b) ? (a) : (b)
#define mmin(a,b) a = (a) < (b) ? (a) : (b)
#define abs(x) (((x) < 0) ? -(x) : (x))
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define has(c,x) (c.find(x)!=c.end())
#define pw(x) (1LL << (x))
#define ibit(x,i) (x & pw(i))
#define sbit(x,i) (x |= pw(i))

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

inline int nextint(){ int x; scanf("%d",&x);   return x; }
inline ll  nextll() { ll  x; scanf("%lld",&x); return x; }

#define gc getchar
template <typename T> void scanint(T &x) {
T c = gc(); while(((c < 48) || (c > 57)) && (c!='-')) c = gc();
bool neg = false; if(c == '-') {neg = true; c = gc();} x = 0; for(;c < 48 || c > 57;c=gc());
for(;c > 47 && c < 58;c=gc()) x = (x*10) + (c - 48); if(neg) x = -x;
}
// variadics
template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; }
template < typename T ,  typename... Ts > T min_( T first , Ts... last ){ return  min_(first, min_(last...)); }
template<typename T >T max_ ( T a , T b ) { return a > b ? a : b ; }
template < typename T ,  typename... Ts > T max_( T first , Ts... last ){ return  max_(first, max_(last...)); }

// lambda exp auto  square = [](int inp) { return inp * inp; } ;

template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) {
os<<"("<<t.first<<", "<<t.second<<")";
return os;
}
template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) {
    out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) {
    out << v[i]; if (i != last) out << ", "; } out << "]"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const set<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T> ostream& operator<< (ostream& out, const multiset<T>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}
template<typename T, typename VAL> ostream& operator<< (ostream& out, const map<T,VAL>& v) {
    out << "{"; auto last = v.end(); for(auto i = v.begin(); i != last;) {
    out << *i; if (++i != last) out << ", "; } out << "}"; return out;
}

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}

template <typename Arg, typename... Args>
void ptrace(std::ostream& out, Arg&& arg, Args&&... args)
{
    out<<"<< ";
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ", " << std::forward<Args>(args)),0)...};
    out<<" >>\n";
}
#ifndef LOCAL_PROJECT
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
}
#else
template <typename Arg, typename... Args>
void trace(Arg&& arg, Args&&... args)
{
    cerr<<"<< ";
    cerr << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cerr << ", " << std::forward<Args>(args)),0)...};
    cerr<<" >>\n";
}
#endif

template <typename T> void readv(vector<T> &v, int n){
    v = vector<T>(n);
    forn(i,n) cin >> (v[i]);
}
template <typename T> void reads(set<T> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v.insert(x);
    }
}
template <typename T> void readm(map<T, int> &v, int n){
    T x;
    forn(i,n){
        cin >> x;
        v[x]++;
    }
}

template <typename Arg, typename... Args>
void read(Arg&& arg, Args&&... args)
{
    cin >> std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};
}

ll pwr(ll base, ll p, ll mod){
    ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;
}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }

const long double PI = (long double)(3.1415926535897932384626433832795);
const ll  mx_ll   = numeric_limits<ll> :: max();
const int mx_int  = numeric_limits<int> :: max();
const int mod = 1e9+7;
const int oo = 0x3f3f3f3f;
const ll  OO = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};

/*************************************************************************/

const int maxn=200123;

void _build(){

}

void _solve(){

}

void _input(){

}

/*************************************************************************/

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //cout.precision(15);
    bool GOOGLE = false;
    _build();
    int qq=1;
    if(GOOGLE) cin>>qq;
    forn(i,qq){
        _input();
        if(GOOGLE) cout<<"Case #"<<i+1<<": ";
        _solve();
        //cout<<endl;
        //fprintf (stderr, "%d / %d = %.2f | %.2f\n", i+1, qq, (double)clock () / CLOCKS_PER_SEC, ((double)clock () / (i+1) * qq) / CLOCKS_PER_SEC);
    }

}

/*************************************************************************/
/*
#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>
#include <cassert>
1-4
10-17
20-22

*/


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
vector< pair<int, pi> > e(200007);
vector<pi> q(200007);
int parent[200007];
ll s[200007];
ll ans[200007];
ll sum;

int find(int i){
	if(parent[i]==-1)return i;
	return find(parent[i]);
}
void merge(int a,int b){
	int x= find(a);
	int y=find(b);
	if(x==y)return ;

	parent[y]=x;
	sum -= ((s[x]-1)*s[x])/2;
	sum -=((s[y]-1)*s[y])/2;
	s[x]+=s[y];
	sum+=((s[x]-1)*s[x])/2;
	return ;
}

int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=0;i<n-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		e[i]=(mp(w,mp(a,b)));
	}

	for(int i=0;i<m;i++){
		int a;
		cin>>a;
		q[i]=mp(a,i);
	}
	sort(e.begin(),e.begin()+n-1);
	sort(q.begin(),q.begin()+m);

	sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,s[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			merge(e[j].ss.ff , e[j].ss.ss);
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
vector< pair<int, pi> > e(200007);
vector<pi> q(200007);
int parent[200007];
int s[200007];
int ans[200007];
ll sum;

int find(int i){
	if(parent[i]==-1)return i;
	return find(parent[i]);
}
void merge(int a,int b){
	int x= find(a);
	int y=find(b);
	if(x==y)return ;

	parent[y]=x;
	sum -= ((s[x]-1)*s[x])/2;
	sum -=((s[y]-1)*s[y])/2;
	s[x]+=s[y];
	sum+=((s[x]-1)*s[x])/2;
	return ;
}

int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=0;i<n-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		e[i]=(mp(w,mp(a,b)));
	}

	for(int i=0;i<m;i++){
		int a;
		cin>>a;
		q[i]=mp(a,i);
	}
	sort(e.begin(),e.begin()+n-1);
	sort(q.begin(),q.begin()+m);

	sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,s[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			int x=find(e[j].ss.ff),y=find(e[j].ss.ss);
			merge(e[j].ss.ff , e[j].ss.ss);
			//cout<<sum<<endl;
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
vector< pair<int, pi> > e;
vector<pi> q;
int parent[200007];
ll s[200007];
ll ans[200007];

int find(int i){
	if(parent[i]==-1)return i;
	return find(parent[i]);
}
int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	int a,b,w;
	for(int i=0;i<n-1;i++){
		cin>>a>>b>>w;
		e.pb(mp(w,mp(a,b)));
	}
	for(int i=0;i<m;i++){
		cin>>a;
		q.pb(mp(a,i));
	}
	sort(e.begin(),e.end());
	sort(q.begin(),q.end());

	ll sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,s[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			int x=find(e[j].ss.ff);
	int y=find(e[j].ss.ss);

	parent[y]=x;
	sum -= ((s[x]-1)*s[x])/2;
	sum -=((s[y]-1)*s[y])/2;
	s[x]+=s[y];
	sum+=((s[x]-1)*s[x])/2;
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
int parent[200007];
ll s[200007];

int find(int i){
	if(parent[i]==-1)return i;
	return find(parent[i]);
}
int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	int a,b,w;
	vector< pair<int, pi> > e;
	for(int i=0;i<n-1;i++){
		cin>>a>>b>>w;
		e.pb(mp(w,mp(a,b)));
	}
	vector<pi> q;
	ll ans[m];
	for(int i=0;i<m;i++){
		cin>>a;
		q.pb(mp(a,i));
    }

	sort(e.begin(),e.end());
	sort(q.begin(),q.end());

	ll sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,s[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			int x=find(e[j].ss.ff);
	int y=find(e[j].ss.ss);

	parent[y]=x;
	sum -= ((s[x]-1)*s[x])/2;
	sum -=((s[y]-1)*s[y])/2;
	s[x]+=s[y];
	sum+=((s[x]-1)*s[x])/2;
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
vector< pair<int, pi> > e(200007);
vector<pi> q(200007);
int parent[200007];
ll s[200007];
ll ans[200007];

int find(int i){
	if(parent[i]==-1)return i;
	return find(parent[i]);
}
int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=0;i<n-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		e[i]=(mp(w,mp(a,b)));
	}

	for(int i=0;i<m;i++){
		int a;
		cin>>a;
		q[i]=mp(a,i);
	}
	sort(e.begin(),e.begin()+n-1);
	sort(q.begin(),q.begin()+m);

	ll sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,s[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			int x=find(e[j].ss.ff);
	int y=find(e[j].ss.ss);

	parent[y]=x;
	sum -= ((s[x]-1)*s[x])/2;
	sum -=((s[y]-1)*s[y])/2;
	s[x]+=s[y];
	sum+=((s[x]-1)*s[x])/2;
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
int parent[200007];
ll s[200007];

int find(int i){
	if(parent[i]==-1)return i;
	parent[i]=find(parent[i]);
	return parent[i];
}
int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	int a,b,w;
	vector< pair<int, pi> > e;
	for(int i=0;i<n-1;i++){
		cin>>a>>b>>w;
		e.pb(mp(w,mp(a,b)));
	}
	vector<pi> q;
	ll ans[m];
	for(int i=0;i<m;i++){
		cin>>a;
		q.pb(mp(a,i));
    }

	sort(e.begin(),e.end());
	sort(q.begin(),q.end());

	ll sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,s[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			int x=find(e[j].ss.ff);
	int y=find(e[j].ss.ss);

	parent[y]=x;
	sum -= ((s[x]-1)*s[x])/2;
	sum -=((s[y]-1)*s[y])/2;
	s[x]+=s[y];
	sum+=((s[x]-1)*s[x])/2;
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define ld long double
#define endl "\n"
#define ld long double
#define ff first
#define ss second
#define pi pair<int,int>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define forall(i,n) for(int i=0;i<n;i++)
int n,m;
vector< pair<int, pi> > e(200007);
vector<pi> q(200007);
int parent[200007];
int size[200007];
int ans[200007];
ll sum;

int find(int i){
	if(parent[i]==-1)return i;
	return find(parent[i]);
}
void merge(int a,int b){
	int x= find(a);
	int y=find(b);
	if(x==y)return ;

	parent[y]=x;
	sum -= ((size[x]-1)*size[x])/2;
	sum -=((size[y]-1)*size[y])/2;
	size[x]+=size[y];
	sum+=((size[x]-1)*size[x])/2;
	return ;
}

int main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=0;i<n-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		e[i]=(mp(w,mp(a,b)));
	}

	for(int i=0;i<m;i++){
		int a;
		cin>>a;
		q[i]=mp(a,i);
	}
	sort(e.begin(),e.begin()+n-1);
	sort(q.begin(),q.begin()+m);

	sum=0;
	for(int i=0;i<=n;i++)parent[i]=-1,size[i]=1;

	int j=0;
	for(int i=0;i<m;i++){
		while(j<n-1 && e[j].ff <= q[i].ff  ){
			int x=find(e[j].ss.ff),y=find(e[j].ss.ss);
			merge(e[j].ss.ff , e[j].ss.ss);
			//cout<<sum<<endl;
			j++;
		}
		ans[q[i].ss]=sum;
	}
	for(int i=0;i<m;i++)cout<<ans[i]<<" ";

 return 0;
}


#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 1e7 + 233;
const int mod = 1e9 + 7;

int n, m;
struct query
{
    int x, id; ll ans;
};
struct edge
{
    int to, nxt, w;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
    tot = 0;
    memset(head, -1, sizeof head);
}
void addedge(int u, int v, int w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
int fa[maxn << 1]; ll num[maxn << 1];
int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f2] = f1;
}
struct ed
{
    int u, v, w;
};
int main()
{
    // double pp = clock();
    // freopen("233.in", "r", stdin);
    // freopen("233.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;
    vector<ed> e(n - 1);
    vector<query> qry(m);
    int u, v, w;
    for(int i = 0; i <= n - 2; ++i)cin >> e[i].u >> e[i].v >> e[i].w;
    for(int i = 0; i <= m - 1; ++i)cin >> qry[i].x, qry[i].id = i;
    if(n == 1)
    {
        for(int i = 1; i <= m; ++i)cout << 0 << " ";
        return 0;
    }
    for(int i = 0; i <= n - 2; ++i)fa[i] = i;
    init();
    sort(e.begin(), e.end(), [](ed &i, ed &j){return i.w < j.w;});
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.x < j.x;});
    for(auto i: e)addedge(i.u, i.v, i.w), addedge(i.v, i.u, i.w);
    int cnt = 0;
    for(auto &q: qry)
    {
        int ans = 0;
        vector<int> k;
        while(cnt <= n - 2 && e[cnt].w <= q.x)
        {
            num[cnt] = 1;
            k.push_back(cnt);
            for(auto u: {e[cnt].u, e[cnt].v})
            for(int i = head[u]; ~i; i = edge[i].nxt)
            {
                int id = i >> 1;
                if(id == cnt)continue;
                int f = _find(id);
                if(num[f])
                {
                    num[cnt] += num[f];
                    num[f] = 0;
                    _union(cnt, id);
                    break;
                }
            }
            ++cnt;
        }
        if(cnt == n - 1 && k.size() == 0)k.push_back(cnt - 1);
        for(auto i: k)q.ans += (num[i] * (num[i] + 1) >> 1);
    }
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.id < j.id;});
    for(auto i: qry)cout << i.ans << " ";cout << endl;

    // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
    return 0;
}


#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 1e7 + 233;
const int mod = 1e9 + 7;

int n, m;
struct query
{
    int x, id; ll ans;
};
int fa[maxn << 1]; ll num[maxn << 1];
inline int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
inline void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f2] = f1;
}
struct ed
{
    int u, v; ll w;
};
int main()
{
    // double pp = clock();
    // freopen("233.in", "r", stdin);
    // freopen("233.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;
    vector<ed> e(n - 1);
    vector<query> qry(m);
    int u, v, w;
    for(int i = 0; i <= n - 2; ++i)cin >> e[i].u >> e[i].v >> e[i].w;
    for(int i = 0; i <= m - 1; ++i)cin >> qry[i].x, qry[i].id = i;
    if(n == 1)
    {
        for(int i = 1; i <= m; ++i)cout << 0 << " ";
        return 0;
    }
    for(int i = 0; i <= n - 1; ++i)fa[i] = i;
    sort(e.begin(), e.end(), [](ed &i, ed &j){return i.w < j.w;});
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.x < j.x;});
    int cnt = 0;
    ll a[3];
    ll ans = 0;
    // for(auto &q: qry)
    for(int q = 0; q < qry.size(); ++q)
    {
        while(cnt <= n - 2 && e[cnt].w <= qry[q].x)
        {
            a[0] = a[1] = a[2] = 0;
            a[1] = num[_find(e[cnt].u)]; num[_find(e[cnt].u)] = 0;
            a[2] = num[_find(e[cnt].v)]; num[_find(e[cnt].v)] = 0;
            _union(e[cnt].u, e[cnt].v);
            num[_find(e[cnt].u)] = a[1] + a[2] + 1;
            ans -= (a[1] * (a[1] + 1) >> 1);
            ans -= (a[2] * (a[2] + 1) >> 1);
            ans += ((a[1] + a[2] + 1) * (a[1] + a[2] + 2) >> 1);
            ++cnt;
        }
        qry[q].ans = ans;
    }
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.id < j.id;});
    // for(auto i: qry)cout << i.ans << " ";cout << endl;
    for(int i = 0; i < qry.size(); ++i)cout << qry[i].ans << " ";cout << endl;

    // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
    return 0;
}


#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 1e7 + 233;
const int mod = 1e9 + 7;

int n, m;
struct query
{
    int x, id; ll ans;
};
struct edge
{
    int to, nxt; ll w;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
    tot = 0;
    memset(head, -1, sizeof head);
}
void addedge(int u, int v, ll w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
int fa[maxn << 1]; ll num[maxn << 1];
int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f2] = f1;
}
struct ed
{
    int u, v; ll w;
};
int main()
{
    // double pp = clock();
    // freopen("233.in", "r", stdin);
    // freopen("233.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;
    vector<ed> e(n - 1);
    vector<query> qry(m);
    int u, v, w;
    for(int i = 0; i <= n - 2; ++i)cin >> e[i].u >> e[i].v >> e[i].w;
    for(int i = 0; i <= m - 1; ++i)cin >> qry[i].x, qry[i].id = i;
    if(n == 1)
    {
        for(int i = 1; i <= m; ++i)cout << 0 << " ";
        return 0;
    }
    for(int i = 0; i <= n - 2; ++i)fa[i] = i;
    init();
    sort(e.begin(), e.end(), [](ed &i, ed &j){return i.w < j.w;});
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.x < j.x;});
    for(auto i: e)addedge(i.u, i.v, i.w), addedge(i.v, i.u, i.w);
    int cnt = 0;
    vector<int> kk;
    set<int> k;
    for(auto &q: qry)
    {
        int ans = 0;
        // if(cnt != n - 1)k.clear();
        while(cnt <= n - 2 && e[cnt].w <= q.x)
        {
            num[cnt] = 1;
            k.insert(cnt);
            for(auto u: {e[cnt].u, e[cnt].v})
            for(int i = head[u]; ~i; i = edge[i].nxt)
            {
                int id = i >> 1;
                if(id == cnt)continue;
                int f = _find(id);
                if(num[f])
                {
                    num[cnt] += num[f];
                    num[f] = 0;
                    k.erase(f);
                    _union(cnt, id);
                    break;
                }
            }
            ++cnt;
        }
        // if(k.size() == 0)for(int i = 0; i <= n - 1; ++i)if(num[i])k.push_back(i);
        // if(cnt == n - 1 && k.size() == 0)k.push_back(cnt - 1);
        for(auto i: k)q.ans += (num[i] * (num[i] + 1) >> 1);
    }
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.id < j.id;});
    for(auto i: qry)cout << i.ans << " ";cout << endl;

    // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
    return 0;
}


#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 1e7 + 233;
const int mod = 1e9 + 7;

int n, m;
struct query
{
    int x, id; ll ans;
};
struct edge
{
    int to, nxt; ll w;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
    tot = 0;
    memset(head, -1, sizeof head);
}
void addedge(int u, int v, ll w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
int fa[maxn << 1]; ll num[maxn << 1];
int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f2] = f1;
}
struct ed
{
    int u, v; ll w;
};
int main()
{
    // double pp = clock();
    // freopen("233.in", "r", stdin);
    // freopen("233.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;
    vector<ed> e(n - 1);
    vector<query> qry(m);
    int u, v, w;
    for(int i = 0; i <= n - 2; ++i)cin >> e[i].u >> e[i].v >> e[i].w;
    for(int i = 0; i <= m - 1; ++i)cin >> qry[i].x, qry[i].id = i;
    if(n == 1)
    {
        for(int i = 1; i <= m; ++i)cout << 0 << " ";
        return 0;
    }
    for(int i = 0; i <= n - 2; ++i)fa[i] = i;
    init();
    sort(e.begin(), e.end(), [](ed &i, ed &j){return i.w < j.w;});
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.x < j.x;});
    for(auto i: e)addedge(i.u, i.v, i.w), addedge(i.v, i.u, i.w);
    int cnt = 0;
    ll a[3];
    ll ans = 0;
    for(auto &q: qry)
    {
        while(cnt <= n - 2 && e[cnt].w <= q.x)
        {
            a[0] = a[1] = a[2] = 0;
            num[cnt] = 1;
            for(auto u: {e[cnt].u, e[cnt].v})
            for(int i = head[u]; ~i; i = edge[i].nxt)
            {
                int id = i >> 1;
                if(id == cnt)continue;
                int f = _find(id);
                if(num[f])
                {
                    num[cnt] += num[f];
                    a[++a[0]] = num[f];
                    num[f] = 0;
                    _union(cnt, id);
                    break;
                }
            }
            ans -= (a[1] * (a[1] + 1) >> 1);
            ans -= (a[2] * (a[2] + 1) >> 1);
            ans += ((a[1] + a[2] + 1) * (a[1] + a[2] + 2) >> 1);
            ++cnt;
        }
        // if(k.size() == 0)for(int i = 0; i <= n - 1; ++i)if(num[i])k.push_back(i);
        // if(cnt == n - 1 && k.size() == 0)k.push_back(cnt - 1);
        // for(auto i: k)q.ans += (num[i] * (num[i] + 1) >> 1);
        q.ans = ans;
    }
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.id < j.id;});
    for(auto i: qry)cout << i.ans << " ";cout << endl;

    // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
    return 0;
}


#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 1e7 + 233;
const int mod = 1e9 + 7;

int n, m;
struct query
{
    int x, id; ll ans;
};
struct edge
{
    int to, nxt, w;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
    tot = 0;
    memset(head, -1, sizeof head);
}
void addedge(int u, int v, int w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
int fa[maxn << 1]; ll num[maxn << 1];
int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f2] = f1;
}
struct ed
{
    int u, v, w;
};
int main()
{
    // double pp = clock();
    // freopen("233.in", "r", stdin);
    // freopen("233.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;
    vector<ed> e(n - 1);
    vector<query> qry(m);
    int u, v, w;
    for(int i = 0; i <= n - 2; ++i)cin >> e[i].u >> e[i].v >> e[i].w;
    for(int i = 0; i <= m - 1; ++i)cin >> qry[i].x, qry[i].id = i;
    if(n == 1)
    {
        for(int i = 1; i <= m; ++i)cout << 0 << " ";
        return 0;
    }
    for(int i = 0; i <= n - 2; ++i)fa[i] = i;
    init();
    sort(e.begin(), e.end(), [](ed &i, ed &j){return i.w < j.w;});
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.x < j.x;});
    for(auto i: e)addedge(i.u, i.v, i.w), addedge(i.v, i.u, i.w);
    int cnt = 0;
    for(auto &q: qry)
    {
        int ans = 0;
        vector<int> k;
        while(e[cnt].w <= q.x && cnt <= n - 2)
        {
            num[cnt] = 1;
            k.push_back(cnt);
            for(auto u: {e[cnt].u, e[cnt].v})
            for(int i = head[u]; ~i; i = edge[i].nxt)
            {
                int id = i >> 1;
                if(id == cnt)continue;
                int f = _find(id);
                if(num[f])
                {
                    num[cnt] += num[f];
                    num[f] = 0;
                    _union(cnt, id);
                    break;
                }
            }
            ++cnt;
        }
        if(cnt == n - 1 && k.size() == 0)k.push_back(cnt - 1);
        for(auto i: k)q.ans += (num[i] * (num[i] + 1) >> 1);
    }
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.id < j.id;});
    for(auto i: qry)cout << i.ans << " ";cout << endl;

    // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
    return 0;
}


#include <bits/stdc++.h>
#define aaa cout<<233<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;//1061109567 > 1e9
const ll linf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 2e5 + 5;
const int maxm = 1e7 + 233;
const int mod = 1e9 + 7;

int n, m;
struct query
{
    int x, id; ll ans;
};
struct edge
{
    int to, nxt; ll w;
}edge[maxn << 1];
int tot, head[maxn << 1];
void init()
{
    tot = 0;
    memset(head, -1, sizeof head);
}
void addedge(int u, int v, ll w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
int fa[maxn << 1]; ll num[maxn << 1];
inline int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
inline void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f2] = f1;
}
struct ed
{
    int u, v; ll w;
};
int main()
{
    // double pp = clock();
    // freopen("233.in", "r", stdin);
    // freopen("233.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;
    vector<ed> e(n - 1);
    vector<query> qry(m);
    int u, v, w;
    for(int i = 0; i <= n - 2; ++i)cin >> e[i].u >> e[i].v >> e[i].w;
    for(int i = 0; i <= m - 1; ++i)cin >> qry[i].x, qry[i].id = i;
    if(n == 1)
    {
        for(int i = 1; i <= m; ++i)cout << 0 << " ";
        return 0;
    }
    for(int i = 0; i <= n - 2; ++i)fa[i] = i;
    init();
    sort(e.begin(), e.end(), [](ed &i, ed &j){return i.w < j.w;});
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.x < j.x;});
    // for(auto i: e)addedge(i.u, i.v, i.w), addedge(i.v, i.u, i.w);
    for(int i = 0; i < e.size(); ++i)addedge(e[i].u, e[i].v, e[i].w), addedge(e[i].v, e[i].u, e[i].w);
    int cnt = 0;
    ll a[3];
    ll ans = 0;
    // for(auto &q: qry)
    for(int q = 0; q < qry.size(); ++q)
    {
        while(cnt <= n - 2 && e[cnt].w <= qry[q].x)
        {
            a[0] = a[1] = a[2] = 0;
            num[cnt] = 1;
            for(auto u: {e[cnt].u, e[cnt].v})
            for(int i = head[u]; ~i; i = edge[i].nxt)
            {
                int id = i >> 1;
                if(id == cnt)continue;
                int f = _find(id);
                if(num[f])
                {
                    num[cnt] += num[f];
                    a[++a[0]] = num[f];
                    num[f] = 0;
                    _union(cnt, id);
                    break;
                }
            }
            ans -= (a[1] * (a[1] + 1) >> 1);
            ans -= (a[2] * (a[2] + 1) >> 1);
            ans += ((a[1] + a[2] + 1) * (a[1] + a[2] + 2) >> 1);
            ++cnt;
        }
        // q.ans = ans;
        qry[q].ans = ans;
    }
    sort(qry.begin(), qry.end(), [](query &i, query &j){return i.id < j.id;});
    // for(auto i: qry)cout << i.ans << " ";cout << endl;
    for(int i = 0; i < qry.size(); ++i)cout << qry[i].ans << " ";cout << endl;

    // cout << endl << (clock() - pp) / CLOCKS_PER_SEC << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    int x,y,z;
    st(int p, int q,  int r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    int x,y;
} Q[100005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
int par[200005];
int fre[200005];
int print[200005];

int findd(int x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(int a, int b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    int n,q;
    cin>>n>>q;
    for(int i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        int x,y,z;
        scanf("%d %d %d", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(int i = 0; i< q; i++)
    {
        int x;
        scanf("%d", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    int now = 0;
    long long ans = 0;
    for(int i = 0; i< q; i++)
    {
        int cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            int a = findd(vec[now].x);
            int b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        print[Q[i].x] = ans;
    }
    for(int i = 0; i< q; i++)
        cout<<print[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    long long x,y,z;
    st(long long p, long long q,  long long r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    long long x,y;
} Q[200005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
long long par[200005];
long long fre[200005];
long long print[200005];

long long findd(long long x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(long long a, long long b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    long long n,q;
    cin>>n>>q;
    for(long long i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        long long x,y,z;
        scanf("%lld %lld %lld", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(long long i = 0; i< q; i++)
    {
        long long x;
        scanf("%lld", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    long long now = 0;
    long long ans = 0;
    for(long long i = 0; i< q; i++)
    {
        long long cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            long long a = findd(vec[now].x);
            long long b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        print[Q[i].x] = ans;
    }
    for(long long i = 0; i< q; i++)
        cout<<print[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    long long x,y,z;
    st(long long p, long long q,  long long r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    long long x,y;
} Q[200005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
long long par[200005];
long long fre[200005];
long long prlong long[200005];

long long findd(long long x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(long long a, long long b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    long long n,q;
    cin>>n>>q;
    for(long long i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        long long x,y,z;
        scanf("%d %d %d", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(long long i = 0; i< q; i++)
    {
        long long x;
        scanf("%d", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    long long now = 0;
    long long ans = 0;
    for(long long i = 0; i< q; i++)
    {
        long long cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            long long a = findd(vec[now].x);
            long long b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        prlong long[Q[i].x] = ans;
    }
    for(long long i = 0; i< q; i++)
        cout<<print[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    int x,y,z;
    st(int p, int q,  int r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    int x,y;
} Q[100005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
int par[100005];
int fre[100005];
int print[100005];

int findd(int x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(int a, int b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    int n,q;
    cin>>n>>q;
    for(int i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        int x,y,z;
        scanf("%d %d %d", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(int i = 0; i< q; i++)
    {
        int x;
        scanf("%d", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    int now = 0;
    long long ans = 0;
    for(int i = 0; i< q; i++)
    {
        int cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            int a = findd(vec[now].x);
            int b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        print[Q[i].x] = ans;
    }
    for(int i = 0; i< q; i++)
        cout<<print[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    long long x,y,z;
    st(long long p, long long q,  long long r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    long long x,y;
} Q[200005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
long long par[200005];
long long fre[200005];
long long print[200005];

long long findd(long long x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(long long a, long long b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    long long n,q;
    cin>>n>>q;
    for(long long i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        long long x,y,z;
        scanf("%d %d %d", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(long long i = 0; i< q; i++)
    {
        long long x;
        scanf("%d", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    long long now = 0;
    long long ans = 0;
    for(long long i = 0; i< q; i++)
    {
        long long cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            long long a = findd(vec[now].x);
            long long b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        prlong long[Q[i].x] = ans;
    }
    for(long long i = 0; i< q; i++)
        cout<<print[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    long long x,y,z;
    st(long long p, long long q,  long long r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    long long x,y;
} Q[200005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
long long par[200005];
long long fre[200005];
long long prlong long[200005];

long long findd(long long x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(long long a, long long b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    long long n,q;
    cin>>n>>q;
    for(long long i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        long long x,y,z;
        scanf("%d %d %d", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(long long i = 0; i< q; i++)
    {
        long long x;
        scanf("%d", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    long long now = 0;
    long long ans = 0;
    for(long long i = 0; i< q; i++)
    {
        long long cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            long long a = findd(vec[now].x);
            long long b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        prlong long[Q[i].x] = ans;
    }
    for(long long i = 0; i< q; i++)
        cout<<prlong long[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct st
{
    int x,y,z;
    st(int p, int q,  int r)
    {
        x = p,y = q, z = r;
    }
    bool operator <(const st&p) const
    {
        return z<p.z;
    }
};
struct qq
{
    int x,y;
} Q[200005];
bool comp1(qq x, qq y)
{
    return x.y<y.y;
}

vector<st>vec;
int par[200005];
int fre[200005];
int print[200005];

int findd(int x)
{
    if(par[x] == x)
        return x;
    return par[x] = findd(par[x]);
}

void Union(int a, int b)
{
    par[findd(b)] = findd(a);
}

int main()
{
    int n,q;
    cin>>n>>q;
    for(int i = 0; i <= n; i++)
        par[i] = i,fre[i]++;

    n--;
    while(n--)
    {
        int x,y,z;
        scanf("%d %d %d", &x, &y, &z);
        vec.push_back(st(x,y,z));

    }
    sort(vec.begin(),vec.end());
    vec.push_back(st(-1,-1,9999999));
    for(int i = 0; i< q; i++)
    {
        int x;
        scanf("%d", &x);
        Q[i].x = i;
        Q[i].y = x;
    }
    sort(Q, Q+q,comp1);
    int now = 0;
    long long ans = 0;
    for(int i = 0; i< q; i++)
    {
        int cost = Q[i].y;
        //cout<<"check cost = "<<cost<<endl;
        while(vec[now].z<=cost)
        {
            int a = findd(vec[now].x);
            int b = findd(vec[now].y);

            if(a!=b)
            {
                long long f = fre[a];
                long long s = fre[b];
                fre[a]-=f;
                fre[b]-=s;

                ans-=((f * (f-1))/2);
                ans-=((s * (s-1))/2);
                par[b] = a;
                fre[a]+=(f+s);
                f+=s;
                ans+=((f * (f-1))/2);

                // cout<<"edge_cost "<<vec[now].z<<endl;
                //cout<<"ans = "<<ans<<endl;
            }

            now++;
        }
        print[Q[i].x] = ans;
    }
    for(int i = 0; i< q; i++)
        cout<<print[i]<<" ";
    return 0;
}