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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cstdio>
#include <iostream>
#include <algorithm>
//#include <string>
#include <vector>
//#include <complex>
//#include <iterator>
//#include <set>
//#include <bitset>
//#include <map>
//#include <stack>
//#include <list>
#include <queue>
//#include <deque>
using namespace std;
typedef vector<int> VI;
typedef long long LL;
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
//#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(i=(c).begin(); i !=(c).end(); ++i)
//#define FORE(i, c) for(VAR(i, (c).begin()), KS=(c).end(); i !=KS; ++i)
#define PB push_back
#define ST first
#define ND second
const int INF = 1000000000;
const int MAX=1000001;
const LL INFL=(LL)INF*INF;
const double EPS = 10e-9;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<double> VD;
//typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
VI::iterator iti;
#define PF push_front
#define MP make_pair
queue<int> qu;
VI tn,tx;
bool bb;
template <class V, class E> struct Graph {
  struct Ed : E {
    int v;
    Ed(E p=E(), int w=0) : E(p), v(w) { }
  };
  struct Ve : V, vector<Ed> { };
  vector<Ve> g;
  int m;
  Graph(int n = 0) : g(n) { }
  void EdgeD(int b, int e, E d = E()) {
    g[b].PB(Ed(d, e));
  }
  void EdgeU(int b, int e, E d = E()) {
    Ed eg(d, e);
    g[b].PB(eg);
    eg.v = b;
    g[e].PB(eg);
  }
/*  void Write() {
    REP(x, SIZE(g)) {
      cout << x+1 << ":";
      FOREACH(ite, g[x]) cout << " " << ite->v+1;
      cout << endl;
    }
  }*/
  typename vector<Ed>::iterator ite;
  void popraw(int i)
  {
    int n,x;
    qu.push(i);
    while(qu.size())
    {
      i=qu.front();
      tn.clear();
      tx.clear();
      qu.pop();
      g[i].c=0;
      FOREACH(ite,g[i])
      {
        tn.PB(g[(ite->v)].n);
        tx.PB(g[(ite->v)].x);
      }
      nth_element(tn.begin(),tn.begin()+(tn.end()-tn.begin())/2,tn.end());
      nth_element(tx.begin(),tx.begin()+(tx.end()-tx.begin()-1)/2,tx.end());
      n=*(tn.begin()+(tn.end()-tn.begin())/2);
      x=*(tx.begin()+(tx.end()-tx.begin()-1)/2);
      if(g[i].n<n || g[i].x>x || bb)
      {
   //     if(bb)
   //       printf("bb%d\n",i);
        bb=0;
        g[i].n=max(n,g[i].n);
        g[i].x=min(x,g[i].x);
        FOREACH(ite,g[i])
          if(g[(ite->v)].c &&ite->v>=m)
          {
            qu.push(ite->v);
          }
      }
      g[i].c=1;
    }
  }
  typename vector<Ve>::iterator itve;
/*  void pisz()
  {
    itve=g.begin();
    FOREACH(itve,g)
      printf("%d %d\n",itve->n,itve->x);
  }*/
  void licz()
  {
    LL w=0;
    FOREACH(itve,g)
      FOREACH(ite,*itve)
        w+=abs(g[ite->v].x-itve->x);
    printf("%lld\n",w/2);
  }
};
struct vv
{
  int n,x;
  bool c;
  vv()
  {
    c=1;
    n=0;
    x=INF;
  }
};
struct ee
{
};
int main()
{
  int n,m,i,b1,b2,mn=INF,mx=0;
  scanf("%d%d",&n,&m);
  Graph<vv,ee> gr(n);
  gr.m=m;
  for(i=1;i<n;++i)
  {
    scanf("%d%d",&b1,&b2);
    gr.EdgeU(b1-1,b2-1);
  }
  for(i=0;i<m;++i)
  {
    scanf("%d",&b1);
    if(b1>mx)
      mx=b1;
    if(b1<mn)
      mn=b1;
    gr.g[i].n=gr.g[i].x=b1;
  }
  bb=false;
  for(i=m;i<n;++i)
  {
    gr.popraw(i);
  }
  for(i=m;i<n;++i)
  {
    if(gr.g[i].n<gr.g[i].x)
    {
      bb=true;
      gr.g[i].n=gr.g[i].x;
      gr.popraw(i);
    }
  }
//  gr.pisz();
  gr.licz();
//  gr.Write();
}