This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include "dango3.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
int pr[501],gs[501];
int find(int x){
    if(x==pr[x])return x;
    return pr[x] = find(pr[x]);
}
bool mergegroup(int a,int b){
    a = find(a);b = find(b);
    if(a==b)return 0;
    if(gs[a]<gs[b])swap(a,b);
    pr[b] = a;
    gs[a]+=gs[b];
    return 1;
}
signed main(){
    //freopen("input.txt","r",stdin);
    //freopen("outout.txt","w",stdout);
    int n,m;
    cin>>n>>m;
    vector<int> v[n];
    int cool[n];
    long long smaller =0 , greater = 0;
    long long cntsm = 0 , cntlr = 0;
    vector<array<int,3>> ed;
    for(int i = 0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        ed.push_back({a,b,c});
        v[a].push_back(c);
    }
    vector<pair<int,int>> pts;
    for(int i = 1;i<n;i++){
        sort(v[i].begin(),v[i].end());
        pts.push_back({0,i});
        for(int j = 0;j<v[i].size();j++){
            pts.push_back({v[i][j]+1,i});
            pts.push_back({v[i][j],i});
        }
        for(int j = 1;j<v[i].size();j++){
            pts.push_back({(v[i][j]+v[i][j-1])/2+1,i});
            pts.push_back({(v[i][j]+v[i][j-1])/2,i});
        }
        cool[i] = 0;
    }
    sort(pts.begin(),pts.end());
    int q;
    cin>>q;
    if(q<=10){
        while(q--){
        int c;
        cin>>c;
        vector<array<int,3>> lol;
        for(int i = 1;i<=n;i++){
            pr[i] = i , gs[i] = 1;
        }
        for(int i = 0;i<m;i++){
            lol.push_back({abs(ed[i][2]-c),ed[i][0],ed[i][1]});
        }
        sort(lol.begin(),lol.end());
        long long all = 0;
        for(auto i:lol){
            if(mergegroup(i[1],i[2])){
                all+=i[0];
            }
        }
        cout<<all<<endl;
    }
    return 0;
    }
    long long ans[q];
    vector<pair<int,int>> qu;
    for(int i = 0;i<q;i++){
        int c;cin>>c;
        qu.push_back({c,i});
    }
    sort(qu.begin(),qu.end());
    int ind = 0;
    for(auto i:qu){
        while(ind<pts.size()&&pts[ind].first<=i.first){
            int an = pts[ind].second;
            if(cool[an]!=0){
                if(cool[an]>0){
                    greater-=cool[an];
                    cntlr--;
                }else{
                    smaller+=cool[an];
                    cntsm--;
                }
            }
            int it = lower_bound(v[an].begin(),v[an].end(),i.first)-v[an].begin();
            if(it==v[an].size()){
                it--;
                cool[an] = -v[an][it];
                smaller+=v[an][it];
                cntsm++;
            }else if(it==0){
                cool[an] = v[an][it];
                greater+=v[an][it];
                cntlr++;
            }else{
                if(abs(v[an][it]-i.first)<=abs(v[an][it-1]-i.first)){
                    cool[an] = v[an][it];
                    greater+=v[an][it];
                    cntlr++;
                }else{
                    it--;
                    cool[an] = -v[an][it];
                    smaller+=v[an][it];
                    cntsm++;
                }
            }
            ind++;
        }
        ans[i.second] = greater-smaller-i.first*cntlr+i.first*cntsm;
    }
    for(int i = 0;i<q;i++){
        cout<<ans[i]<<endl;
    }
}
Compilation message (stderr)
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:39:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for(int j = 0;j<v[i].size();j++){
      |                       ~^~~~~~~~~~~~
reconstruction.cpp:43:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for(int j = 1;j<v[i].size();j++){
      |                       ~^~~~~~~~~~~~
reconstruction.cpp:83:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         while(ind<pts.size()&&pts[ind].first<=i.first){
      |               ~~~^~~~~~~~~~~
reconstruction.cpp:95:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |             if(it==v[an].size()){
      |                ~~^~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |