//Jakub Staroń
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>

#ifdef COMPILING_HOME
#define DEBUG_MODE
#endif

using namespace std;

typedef char int8;
typedef unsigned char uint8;
typedef short int int16;
typedef unsigned short int uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

typedef std::pair<int32,int32> int32_pair;
typedef std::pair<uint32, uint32> uint32_pair;
typedef std::pair<int64,int64> int64_pair;
typedef std::pair<uint64,uint64> uint64_pair;

typedef std::vector<bool> bit_vector;

#ifdef DEBUG_MODE
#define debug_print(x) cerr << #x << " = " << x << endl
#define print_line cerr << "Line " << __LINE__ << endl
#include <cassert>
#else
#define debug_print(x)
#define print_line
#define assert(x)
#endif

#define rep(i, x) for(uint32 i = 0 ; i < (x) ; i++)
#define for_range(i, begin, end) for(auto i = (begin) ; i != (end) ; ++i )
#define all(c) (c).begin(),(c).end()
#define sort_all(x) sort( all(x) )
#define divide(a, b) ( ( (b)%(a) ) == 0 )
#define mp(x, y) make_pair(x,y)
#define pb(x) push_back(x)

#define sig(x) ( (x) == 0 ? 0 : ( (x) < 0 ? -1 : 1 ) )

const double epsilon = 1e-5;

template<class T>
void unique(std::vector<T>& v) {
	sort_all(v);
	v.resize( std::unique(all(v)) - v.begin() );
}

ostream& newline(ostream& str) {
	str.put('\n');
	return str;
}

template<typename T1, typename T2>
istream& operator>>(istream& stream, std::pair<T1, T2>& pair) {
	stream >> pair.first >> pair.second;
	return stream;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& stream, const std::pair<T1, T2>& pair) {
#ifdef DEBUG_MODE
	stream << "(" << pair.first << ", " << pair.second << ")";
#else
	stream << pair.first << ' ' << pair.second;
#endif
	return stream;
}

template<class T>
inline pair<T,T> operator+(const pair<T,T>& a, const pair<T,T>& b) {
	return pair<T,T>(a.first + b.first, a.second + b.second);
}

template<class T>
inline pair<T,T> operator-(const pair<T,T>& a, const pair<T,T>& b) {
	return pair<T,T>(a.first - b.first, a.second - b.second);
}

template<class T>
ostream& operator<<(ostream& str, const vector<T>& v) {
	if(!v.empty())	{
		for(int32 i = 0 ; i + 1 < v.size() ; i++)		
			str << v[i] << ' ';		
		str << v.back();
	}
	return str;
}

class Graf {
public:
	typedef vector<uint32> list_type;

	void resize(uint32 k) {
		graf.resize(k);
	}

	void add(uint32 a, uint32 b) {
		graf[a].push_back(b);
		graf[b].push_back(a);
	}

	uint32 deg(uint32 k) {
		return graf[k].size();
	}

	list_type& obok(uint32 k) {
		return graf[k];
	}

	uint32 size() {
		return graf.size();
	}

private:
	vector<list_type> graf;
};

class Application {
public:
	Application() {
    result = 0;
	}

	void Run() {
		WczytajDane();

    if (M == N) {
      rep (v, N) {
        for (uint32 u : graf.obok(v)) {
          result += abs(rozstaw[v].first - rozstaw[u].first);
        }
      }
      result /= 2;
    }
    else {
      // N - 1 jest naszą stacją i możemy ukorzenić
      // wyznaczamy kolejnosc przetwarzania
      DFS(N - 1);

      for (uint32 v : kolejnosc) {
        if (v < M)
          continue;

        vector<int64_pair> synowie;
        for (uint32 u : graf.obok(v)) {
          if (rozstaw[u] != int64_pair(-1, -1))
            synowie.push_back(rozstaw[u]);
        }
        rozstaw[v] = SolveRanges(synowie);
      }
    }
    cout << result << newline;
	}

  void DFS(uint32 start) {
    bit_vector odwiedzone(N, false);
    odwiedzone[start] = true;
    vector<uint32> Q;
    Q.push_back(start);

    while (!Q.empty()) {
      uint32 v = Q.back();

      Q.pop_back();
      kolejnosc.push_back(v);

      for (uint32 u : graf.obok(v)) {
        if (!odwiedzone[u]) {
          odwiedzone[u] = true;
          Q.push_back(u);
        }
      }
    }
    reverse(all(kolejnosc));
  }

	int64_pair SolveRanges(const vector<int64_pair>& ranges) {
		vector<int64> begins, ends;
		vector<int64> significant;
		for (const int64_pair& pair : ranges) {
			significant.push_back(pair.first);
			significant.push_back(pair.second);
			begins.push_back(pair.first);
			ends.push_back(pair.second);
		}
		unique(significant);
		sort(all(begins), greater<int64>());
		sort(all(ends), greater<int64>());

		vector<int64> results(significant.size(), 0);
		for (const int64_pair& pair : ranges)
			results[0] += min(abs(pair.first - significant[0]), abs(pair.second - significant[0]));

    while (!begins.empty() && begins.back() <= significant[0])
      begins.pop_back();

    // niezjedzone początki
    int64 right_strings = begins.size();
    // końce za mną
    int64 left_strings = 0;

    for_range (i, 1, significant.size()) {
      int64 last = significant[i-1];
      int64 that = significant[i];
      int64 delta = that - last;

      while (!ends.empty() && ends.back() <= last) {
        ends.pop_back();
        left_strings++;
      }

      results[i] = results[i - 1];
      results[i] -= right_strings * delta;
      results[i] += left_strings * delta;

      while (!begins.empty() && begins.back() <= that) {
        begins.pop_back();
        right_strings--;
      }
    }

    int64 minimum = *min_element(all(results));
    //debug_print(minimum);

    result += minimum;

    uint32 first_index, last_index;
    rep (i, results.size())
      if (results[i] == minimum) {
        first_index = i;
        break;
      }

    rep (i, results.size())
      if (results[i] == minimum)
        last_index = i;

    return int64_pair(significant[first_index], significant[last_index]);
	}

	void WczytajDane() {
    cin >> N >> M;
    graf.resize(N);
    rep(i, N - 1) {
      uint32 a, b;
      cin >> a >> b;
      a--,b--;
      graf.add(a, b);
    }

    rozstaw.assign(N, int64_pair(-1, -1));
    rep (i, M) {
      int64 r;
      cin >> r;
      rozstaw[i] = int64_pair(r, r);
    }
  }

  Graf graf;
  uint32 N, M;
  vector<int64_pair> rozstaw;
  vector<uint32> kolejnosc;
	int64 result;
};


int main() {
	ios::sync_with_stdio(0);
	Application application;
	application.Run();
	return 0;
}