1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <cstdio>
#include <vector>
#include <algorithm>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

using namespace std;

typedef long long int LLI;
typedef pair < int , int > PII;

typedef vector < int > VI;
typedef vector < PII > VP;
typedef vector < LLI > VL;
typedef vector < VI > VVI;

const LLI INF = 1000000000000000001LL;

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

VVI G;
VP T;
LLI acc;

void DFSv(int v, int par)
{
    if (SIZE(G[v]) <= 1)
        return ;

    for (int u : G[v]) if (u != par)
        DFSv(u, v);

    VL L, R;
    VI all;

    for (int u : G[v]) if (u != par)
    {
        L.PB(T[u].ST);
        R.PB(T[u].ND);

        all.PB(T[u].ST);
        all.PB(T[u].ND);
    }

    int d = SIZE(L);

    sort(L.begin(), L.end());
    sort(R.begin(), R.end());

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

    VL sumL = L, sumR = R;

    FOR(i,1,d-1)
        sumL[i] += sumL[i-1];

    FOR(i,1,d-1)
        sumR[i] += sumR[i-1];

    int itL = 0, itR = 0;

    VL score;

    LLI minScore = INF;

    for (int val : all)
    {
        while (itL < d && L[itL] <= val)
            itL++;

        while (itR < d && R[itR] <= val)
            itR++;

        LLI sumLeft = ( itR ? sumR[itR - 1] : 0 ),
            sumRight = sumL.back() - ( itL ? sumL[itL - 1] : 0 );

        LLI cntLeft = itR,
            cntRight = d - itL;

        LLI myScore = sumRight - cntRight * val + cntLeft * val - sumLeft;
        score.PB(myScore);

        minScore = min(minScore, myScore);
    }

    FORE(i,score) if (score[i] == minScore)
    {
        T[v].ST = all[i];
        break;
    }

    FORDE(i,score) if (score[i] == minScore)
    {
        T[v].ND = all[i];
        break;
    }

    acc += minScore;
}

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

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);

    G.resize(n);

    FOR(i,1,n-1)
    {
        int u, v;
        scanf("%d %d", &u, &v); u--; v--;

        G[u].PB(v);
        G[v].PB(u);
    }

    T.resize(n);

    FOR(i,0,m-1)
    {
        int r;
        scanf("%d", &r);

        T[i] = MP(r,r);
    }

    acc = 0LL;

    if (n > 2)
        DFSv(n-1, -1);
    else
        acc = abs(T[0].ST - T[1].ST);

    printf("%lld", acc);

    return 0;
}

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