//package contest;

import static java.lang.Math.*;

import java.io.*;
import java.lang.reflect.*;

public class B {

	final int MOD = (int)1e9 + 7;
	final double eps = 1e-12;
	final int INF = (int)2e9;

	public B () {
		int N = sc.nextInt();
		int [][] E = new int [N-1][];
		for (int i = 0; i < N-1; ++i)
			E[i] = new int [] { sc.nextInt() - 1, sc.nextInt() - 1 };

		V = sc.nextLongs();

		int [] C = new int [N];
		for (int [] e : E) {
			++C[e[0]]; ++C[e[1]];
		}

		G = new int [N][];
		for (int i = 0; i < N; ++i) {
			G[i] = new int[C[i]];
			C[i] = 0;
		}

		for(int [] e : E) {
			int x = e[0], y = e[1];
			G[x][C[x]++] = y; G[y][C[y]++] = x;
		}

		X = new long [N]; Y = new long [N];

		dfs(0, -1);
		long res = X[0] + Y[0];
		exit(res);
	}

	void dfs(int p, int w) {
		for (int q : G[p])
			if (q != w) {
				dfs(q, p);
				X[p] = max(X[p], X[q]);
				Y[p] = max(Y[p], Y[q]);
			}

		long U = V[p] - Y[p] - X[p];
		if (U > 0)
			X[p] += U;
		else
			Y[p] -= U;
	}

	long [] X, Y; Long [] V;
	int [][] G;

	////////////////////////////////////////////////////////////////////////////////////

	/* Dear hacker, don't bother reading below this line, unless you want to help me debug my I/O routines :-) */

	static MyScanner sc = new MyScanner();

	static class MyScanner {
		public String next() {
			newLine();
			return line[index++];
		}

		public char nextChar() {
			return next().charAt(0);
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String nextLine() {
			line = null;
			return readLine();
		}

		public String [] nextStrings() {
			line = null;
			return readLine().split(" ");
		}

		public char [] nextChars() {
			return next().toCharArray();
		}

		public Integer [] nextInts() {
			String [] L = nextStrings();
			Integer [] res = new Integer [L.length];
			for (int i = 0; i < L.length; ++i)
				res[i] = Integer.parseInt(L[i]);
			return res;
		}

		public Long [] nextLongs() {
			String [] L = nextStrings();
			Long [] res = new Long [L.length];
			for (int i = 0; i < L.length; ++i)
				res[i] = Long.parseLong(L[i]);
			return res;
		}

		public Double [] nextDoubles() {
			String [] L = nextStrings();
			Double [] res = new Double [L.length];
			for (int i = 0; i < L.length; ++i)
				res[i] = Double.parseDouble(L[i]);
			return res;
		}

		public String [] next (int N) {
			String [] res = new String [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.next();
			return res;
		}

		public Integer [] nextInt (int N) {
			Integer [] res = new Integer [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextInt();
			return res;
		}

		public Long [] nextLong (int N) {
			Long [] res = new Long [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextLong();
			return res;
		}

		public Double [] nextDouble (int N) {
			Double [] res = new Double [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextDouble();
			return res;
		}

		public String [][] nextStrings (int N) {
			String [][] res = new String [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextStrings();
			return res;
		}

		public Integer [][] nextInts (int N) {
			Integer [][] res = new Integer [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextInts();
			return res;
		}

		public Long [][] nextLongs (int N) {
			Long [][] res = new Long [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextLongs();
			return res;
		}

		public Double [][] nextDoubles (int N) {
			Double [][] res = new Double [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextDoubles();
			return res;
		}

		//////////////////////////////////////////////

		private boolean eol() {
			return index == line.length;
		}

		private String readLine() {
			try {
				return r.readLine();
			} catch (Exception e) {
				throw new Error(e);
			}
		}
		private final BufferedReader r;

		MyScanner () {
			this(new BufferedReader(new InputStreamReader(System.in)));
		}

		MyScanner(BufferedReader r) {
			try {
				this.r = r;
				while (!r.ready())
					Thread.sleep(1);
				start();
			} catch (Exception e) {
				throw new Error(e);
			}
		}

		private String [] line;
		private int index;

		private void newLine() {
			if (line == null || eol()) {
				line = readLine().split(" ");
				index = 0;
			}
		}
	}

	static void print(Object o, Object... a) {
		printDelim(" ", o, a);
	}

	static void cprint(Object o, Object... a) {
		printDelim("", o, a);
	}

	static void printDelim (String delim, Object o, Object... a) {
		pw.println(build(delim, o, a));
	}

	static void exit (Object o, Object... a) {
		print(o, a);
		exit();
	}

	static void exit () {
		pw.close();
		System.out.flush();
		System.err.println("------------------");
		System.err.println("Time: " + ((millis() - t) / 1000.0));
		System.exit(0);
	}

	void NO() {
		throw new Error("NO!");
	}

	////////////////////////////////////////////////////////////////////////////////////

	static String build(String delim, Object o, Object... a) {
		StringBuilder b = new StringBuilder();
		append(b, o, delim);
		for (Object p : a)
			append(b, p, delim);
		return b.toString().trim();
	}

	static void append(StringBuilder b, Object o, String delim) {
		if (o.getClass().isArray()) {
			int L = Array.getLength(o);
			for (int i = 0; i < L; ++i)
				append(b, Array.get(o, i), delim);
		} else if (o instanceof Iterable<?>) {
			for (Object p : (Iterable<?>)o)
				append(b, p, delim);
		} else
			b.append(delim).append(o);
	}

	////////////////////////////////////////////////////////////////////////////////////

	public static void main(String[] args) {
		new B();
		exit();
	}

	static void start() {
		t = millis();
	}

	static PrintWriter pw = new PrintWriter(System.out);

	static long t;

	static long millis() {
		return System.currentTimeMillis();
	}
}


//package contest;

import static java.lang.Math.*;

import java.io.*;
import java.lang.reflect.*;

public class B {

	final int MOD = (int)1e9 + 7;
	final double eps = 1e-12;
	final int INF = (int)2e9;

	public B () {
		int N = sc.nextInt();
		int [][] E = new int [N-1][];
		for (int i = 0; i < N-1; ++i)
			E[i] = new int [] { sc.nextInt() - 1, sc.nextInt() - 1 };

		V = sc.nextLongs();

		int [] C = new int [N];
		for (int [] e : E) {
			++C[e[0]]; ++C[e[1]];
		}

		G = new int [N][];
		for (int i = 0; i < N; ++i) {
			G[i] = new int[C[i]];
			C[i] = 0;
		}

		for(int [] e : E) {
			int x = e[0], y = e[1];
			G[x][C[x]++] = y; G[y][C[y]++] = x;
		}

		X = new long [N]; Y = new long [N];

		dfs(0, -1);
		long res = X[0] + Y[0];
		exit(res);
	}

	void dfs(int p, int w) {
		for (int q : G[p])
			if (q != w) {
				dfs(q, p);
				X[p] = max(X[p], X[q]);
				Y[p] = max(Y[p], Y[q]);
			}

		long U = V[p] + Y[p] - X[p];
		if (U > 0)
			X[p] += U;
		else
			Y[p] -= U;
	}

	long [] X, Y; Long [] V;
	int [][] G;

	////////////////////////////////////////////////////////////////////////////////////

	/* Dear hacker, don't bother reading below this line, unless you want to help me debug my I/O routines :-) */

	static MyScanner sc = new MyScanner();

	static class MyScanner {
		public String next() {
			newLine();
			return line[index++];
		}

		public char nextChar() {
			return next().charAt(0);
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String nextLine() {
			line = null;
			return readLine();
		}

		public String [] nextStrings() {
			line = null;
			return readLine().split(" ");
		}

		public char [] nextChars() {
			return next().toCharArray();
		}

		public Integer [] nextInts() {
			String [] L = nextStrings();
			Integer [] res = new Integer [L.length];
			for (int i = 0; i < L.length; ++i)
				res[i] = Integer.parseInt(L[i]);
			return res;
		}

		public Long [] nextLongs() {
			String [] L = nextStrings();
			Long [] res = new Long [L.length];
			for (int i = 0; i < L.length; ++i)
				res[i] = Long.parseLong(L[i]);
			return res;
		}

		public Double [] nextDoubles() {
			String [] L = nextStrings();
			Double [] res = new Double [L.length];
			for (int i = 0; i < L.length; ++i)
				res[i] = Double.parseDouble(L[i]);
			return res;
		}

		public String [] next (int N) {
			String [] res = new String [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.next();
			return res;
		}

		public Integer [] nextInt (int N) {
			Integer [] res = new Integer [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextInt();
			return res;
		}

		public Long [] nextLong (int N) {
			Long [] res = new Long [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextLong();
			return res;
		}

		public Double [] nextDouble (int N) {
			Double [] res = new Double [N];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextDouble();
			return res;
		}

		public String [][] nextStrings (int N) {
			String [][] res = new String [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextStrings();
			return res;
		}

		public Integer [][] nextInts (int N) {
			Integer [][] res = new Integer [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextInts();
			return res;
		}

		public Long [][] nextLongs (int N) {
			Long [][] res = new Long [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextLongs();
			return res;
		}

		public Double [][] nextDoubles (int N) {
			Double [][] res = new Double [N][];
			for (int i = 0; i < N; ++i)
				res[i] = sc.nextDoubles();
			return res;
		}

		//////////////////////////////////////////////

		private boolean eol() {
			return index == line.length;
		}

		private String readLine() {
			try {
				return r.readLine();
			} catch (Exception e) {
				throw new Error(e);
			}
		}
		private final BufferedReader r;

		MyScanner () {
			this(new BufferedReader(new InputStreamReader(System.in)));
		}

		MyScanner(BufferedReader r) {
			try {
				this.r = r;
				while (!r.ready())
					Thread.sleep(1);
				start();
			} catch (Exception e) {
				throw new Error(e);
			}
		}

		private String [] line;
		private int index;

		private void newLine() {
			if (line == null || eol()) {
				line = readLine().split(" ");
				index = 0;
			}
		}
	}

	static void print(Object o, Object... a) {
		printDelim(" ", o, a);
	}

	static void cprint(Object o, Object... a) {
		printDelim("", o, a);
	}

	static void printDelim (String delim, Object o, Object... a) {
		pw.println(build(delim, o, a));
	}

	static void exit (Object o, Object... a) {
		print(o, a);
		exit();
	}

	static void exit () {
		pw.close();
		System.out.flush();
		System.err.println("------------------");
		System.err.println("Time: " + ((millis() - t) / 1000.0));
		System.exit(0);
	}

	void NO() {
		throw new Error("NO!");
	}

	////////////////////////////////////////////////////////////////////////////////////

	static String build(String delim, Object o, Object... a) {
		StringBuilder b = new StringBuilder();
		append(b, o, delim);
		for (Object p : a)
			append(b, p, delim);
		return b.toString().trim();
	}

	static void append(StringBuilder b, Object o, String delim) {
		if (o.getClass().isArray()) {
			int L = Array.getLength(o);
			for (int i = 0; i < L; ++i)
				append(b, Array.get(o, i), delim);
		} else if (o instanceof Iterable<?>) {
			for (Object p : (Iterable<?>)o)
				append(b, p, delim);
		} else
			b.append(delim).append(o);
	}

	////////////////////////////////////////////////////////////////////////////////////

	public static void main(String[] args) {
		new B();
		exit();
	}

	static void start() {
		t = millis();
	}

	static PrintWriter pw = new PrintWriter(System.out);

	static long t;

	static long millis() {
		return System.currentTimeMillis();
	}
}


// @author Sanzhar
import java.io.*;
import java.util.*;
import java.awt.Point;

public class Template {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(in.readLine());
            } catch (Exception e) {
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    public void run() throws Exception {
        //in = new BufferedReader(new FileReader("input.txt"));
        //out = new PrintWriter(new FileWriter("output.txt"));
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
        out.close();
        in.close();
    }

    public void solve() throws Exception {
        int n = nextInt();
        ArrayList<Integer>[] a = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            a[i] = new ArrayList<Integer>();
        }
        int[] k = new int[n];
        for (int i = 1; i < n; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            a[x].add(y);
            a[y].add(x);
            k[x]++;
            k[y]++;
        }
        long[] v = new long[n];
        for (int i = 0; i < n; i++) {
            v[i] = nextLong();
        }
        ArrayList<Integer>[] p = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            p[i] = new ArrayList<Integer>();
            p[i].add(i);
        }
        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
        q.add(0);
        boolean[] used = new boolean[n];
        used[0] = true;
        int x, y;
        int[] last = new int[n];
        while (!q.isEmpty()) {
            x = q.poll();
            for (int i = 0; i < a[x].size(); i++) {
                y = a[x].get(i);
                if (!used[y]) {
                    q.add(y);
                    used[y] = true;
                    p[y].addAll(p[x]);
                    last[y] = x;
                }
            }
        }
        for (int i = 1; i < n; i++) {
            if (k[i] == 1) {
                q.add(i);
            }
        }
        long ans = 0;
        while (!q.isEmpty()) {
            x = q.poll();
            if (v[x] != 0) {
                long z = v[x];
                for (int i = 0; i < p[x].size(); i++) {
                    y = p[x].get(i);
                    v[y] -= z;
                }
                ans += Math.abs(z);
            }
            y = last[x];
            k[y]--;
            if (y >= 1 && k[y] == 1) {
                q.add(y);
            }
        }
        ans += Math.abs(v[0]);
        out.println(ans);
    }

    public static void main(String[] args) throws Exception {
        new Template().run();
    }
}


// @author Sanzhar
import java.io.*;
import java.util.*;
import java.awt.Point;

public class Template {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(in.readLine());
            } catch (Exception e) {
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    public void run() throws Exception {
        //in = new BufferedReader(new FileReader("input.txt"));
        //out = new PrintWriter(new FileWriter("output.txt"));
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
        out.close();
        in.close();
    }

    public void solve() throws Exception {
        int n = nextInt();
        ArrayList<Integer>[] a = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            a[i] = new ArrayList<Integer>();
        }
        int[] k = new int[n];
        for (int i = 1; i < n; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            a[x].add(y);
            a[y].add(x);
            k[x]++;
            k[y]++;
        }
        long[] add = new long[n];
        long[] sub = new long[n];
        for (int i = 0; i < n; i++) {
            long v = nextLong();
            if (v > 0) {
                sub[i] = v;
            } else {
                add[i] = -v;
            }
        }
        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
        q.add(0);
        boolean[] used = new boolean[n];
        used[0] = true;
        int x, y;
        int[] last = new int[n];
        while (!q.isEmpty()) {
            x = q.poll();
            for (int i = 0; i < a[x].size(); i++) {
                y = a[x].get(i);
                if (!used[y]) {
                    q.add(y);
                    used[y] = true;
                    last[y] = x;
                }
            }
        }
        for (int i = 1; i < n; i++) {
            if (k[i] == 1) {
                q.add(i);
            }
        }
        while (!q.isEmpty()) {
            x = q.poll();
            y = last[x];
            long z = Math.max(add[x] - add[y], sub[x] - sub[y]);
            z = Math.max(z, 0);
            add[y] += z;
            sub[y] += z;
            k[y]--;
            if (y >= 1 && k[y] == 1) {
                q.add(y);
            }
        }
        out.println(add[0] + sub[0]);
    }

    public static void main(String[] args) throws Exception {
        new Template().run();
    }
}


// @author Sanzhar
import java.io.*;
import java.util.*;
import java.awt.Point;

public class Template {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(in.readLine());
            } catch (Exception e) {
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    public void run() throws Exception {
        //in = new BufferedReader(new FileReader("input.txt"));
        //out = new PrintWriter(new FileWriter("output.txt"));
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
        out.close();
        in.close();
    }

    public void solve() throws Exception {
        int n = nextInt();
        ArrayList<Integer>[] a = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            a[i] = new ArrayList<Integer>();
        }
        int[] k = new int[n];
        for (int i = 1; i < n; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            a[x].add(y);
            a[y].add(x);
            k[x]++;
            k[y]++;
        }
        long[] add = new long[n];
        long[] sub = new long[n];
        for (int i = 0; i < n; i++) {
            long v = nextLong();
            if (v > 0) {
                sub[i] = v;
            } else {
                add[i] = -v;
            }
        }
        ArrayList<Integer>[] p = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            p[i] = new ArrayList<Integer>();
            p[i].add(i);
        }
        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
        q.add(0);
        boolean[] used = new boolean[n];
        used[0] = true;
        int x, y;
        int[] last = new int[n];
        while (!q.isEmpty()) {
            x = q.poll();
            for (int i = 0; i < a[x].size(); i++) {
                y = a[x].get(i);
                if (!used[y]) {
                    q.add(y);
                    used[y] = true;
                    p[y].addAll(p[x]);
                    last[y] = x;
                }
            }
        }
        for (int i = 1; i < n; i++) {
            if (k[i] == 1) {
                q.add(i);
            }
        }
        while (!q.isEmpty()) {
            x = q.poll();
            y = last[x];
            long z = Math.max(add[x] - add[y], sub[x] - sub[y]);
            z = Math.max(z, 0);
            add[y] += z;
            sub[y] += z;
            k[y]--;
            if (y >= 1 && k[y] == 1) {
                q.add(y);
            }
        }
        out.println(add[0] + sub[0]);
    }

    public static void main(String[] args) throws Exception {
        new Template().run();
    }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class cf274b {
  static int n;
  static long[] v;
  static ArrayList<Integer>[] g;
  public static void main(String[] args) {
    FastIO in = new FastIO(), out = in;
    n = in.nextInt();
    v = new long[n];
    g = new ArrayList[n];
    for(int i=0; i<n; i++) g[i] = new ArrayList<Integer>();
    for(int i=1; i<n; i++) {
      int a = in.nextInt()-1;
      int b = in.nextInt()-1;
      g[a].add(b);
      g[b].add(a);
    }
    for(int i=0; i<n; i++) v[i] = in.nextLong();
    long[] ans = go(0,0);
    out.println(ans[1]-ans[0]);
    out.close();
  }
  static long[] go(int pos, int par) {
    long[] ans = new long[2];
    for(int x : g[pos]) {
      if(x == par) continue;
      long[] tmp = go(x, pos);
      ans[0] = Math.min(ans[0],tmp[0]);
      ans[1] = Math.max(ans[1],tmp[1]);
    }
    long left = v[pos]+ans[0]+ans[1];
    if(left > 0) ans[0] -= left;
    else ans[1] -= left;
    return ans;
  }
  static class FastIO extends PrintWriter {
    BufferedReader br;
    StringTokenizer st;

    public FastIO() {
      this(System.in,System.out);
    }
    public FastIO(InputStream in, OutputStream out) {
      super(new BufferedWriter(new OutputStreamWriter(out)));
      br = new BufferedReader(new InputStreamReader(in));
      scanLine();
    }
    public void scanLine() {
      try {
        st = new StringTokenizer(br.readLine().trim());
      } catch(Exception e) {
        throw new RuntimeException(e.getMessage());
      }
    }
    public int numTokens() {
      if(!st.hasMoreTokens()) {
        scanLine();
        return numTokens();
      }
      return st.countTokens();
    }
    public String next() {
      if(!st.hasMoreTokens()) {
        scanLine();
        return next();
      }
      return st.nextToken();
    }
    public double nextDouble() {
      return Double.parseDouble(next());
    }
    public long nextLong() {
      return Long.parseLong(next());
    }
    public int nextInt() {
      return Integer.parseInt(next());
    }
  }
}


import java.io.*;
import java.math.*;
import static java.lang.Math.*;
import java.security.SecureRandom;
import static java.util.Arrays.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sun.misc.Regexp;

public class Main {

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
    StreamTokenizer in;
    PrintWriter out;
//deb////////////////////////////////////////////////

    public static void deb(String n, Object n1) {
        System.out.println(n + " is : " + n1);
    }

    public static void deb(int[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }
    public static void deb(boolean[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }
    public static void deb(long[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }

    public static void deb(String[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }

    public static void deb(int[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }
    public static void deb(double[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }

    public static void deb(long[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }

    public static void deb(String[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }
    /////////////////////////////////////////////////////////////

    int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    long nextLong() throws IOException {
        in.nextToken();
        return (long) in.nval;
    }

    void run() throws IOException {
//          in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
  // out = new PrintWriter(new FileWriter("output.txt"));
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    out = new PrintWriter(new OutputStreamWriter(System.out));
        solve();
        out.flush();
    }

static TreeSet<Integer>[] child;
  static int[] netex;
    @SuppressWarnings("unchecked")
    void solve() throws IOException  {
   //   BufferedReader re= new BufferedReader(new FileReader("input.txt"));
  //     BufferedReader re= new BufferedReader(new InputStreamReader(System.in));
 int n=nextInt();
 TreeSet<Integer>[] gen= new TreeSet[n+1];
 child= new TreeSet[n+1];
 netex= new int[n+1];
 boolean[] vis=new boolean[n+1];
        for (int i = 1; i < n+1; i++) {
            gen[i]=new TreeSet<Integer>();
            child[i]=new TreeSet<Integer>();
        }

 for (int i = 0; i < n-1; i++) {
         int a=nextInt();
         int b=nextInt();
 gen[a].add(b);gen[b].add(a);

 }
        for (int i = 1; i < n+1; i++) {
         netex[i]=nextInt();
        }
  TreeSet<Integer> ts= new TreeSet<Integer>();
  TreeSet<Integer> tmp= new TreeSet<Integer>();
  ts.add(1);
  vis[1]=true;
  while(!ts.isEmpty()){
  tmp.clear();
      for (Integer integer : ts) {
          for (Integer integer1 : gen[integer]) {
          if(!vis[integer1]){
          child[integer].add(integer1);
          vis[integer1]=true;
          tmp.add(integer1);
          }

          }

      }
  ts=       (TreeSet<Integer>) tmp.clone();

  }
  long[] ans=get(1);
  long hh= abs(ans[0])+abs(ans[1]);
//  deb(ans);
      System.out.println(hh);
       // System.out.println(ans);
    }

    private long[] get(int i) {
    long[] ret= new long[2];
     long[] tmp= new long[2];

        for (Integer chi : child[i]) {
            tmp=get(chi);
            if(tmp[0]>ret[0])ret[0]=tmp[0];
            if(tmp[1]<ret[1])ret[1]=tmp[1];
        }
        long val=ret[1]+ret[0]+netex[i];
     if(val>0)ret[1]-=val;
     if(val<0)ret[0]-=val;
      //  System.out.println("ret "+i);
      //  deb(ret);
    return ret;

    }

}


import java.io.*;
import java.math.*;
import static java.lang.Math.*;
import java.security.SecureRandom;
import static java.util.Arrays.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sun.misc.Regexp;

public class Main {

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
    StreamTokenizer in;
    PrintWriter out;
//deb////////////////////////////////////////////////

    public static void deb(String n, Object n1) {
        System.out.println(n + " is : " + n1);
    }

    public static void deb(int[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }
    public static void deb(boolean[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }
    public static void deb(long[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }

    public static void deb(String[] A) {

        for (Object oo : A) {
            System.out.print(oo + " ");
        }
        System.out.println("");
    }

    public static void deb(int[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }
    public static void deb(double[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }

    public static void deb(long[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }

    public static void deb(String[][] A) {
        for (int i = 0; i < A.length; i++) {
            for (Object oo : A[i]) {
                System.out.print(oo + " ");
            }
            System.out.println("");
        }

    }
    /////////////////////////////////////////////////////////////

    int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    long nextLong() throws IOException {
        in.nextToken();
        return (long) in.nval;
    }

    void run() throws IOException {
//          in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
  // out = new PrintWriter(new FileWriter("output.txt"));
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    out = new PrintWriter(new OutputStreamWriter(System.out));
        solve();
        out.flush();
    }

    static TreeSet<Integer>[] child;
  static int[] netex;
    @SuppressWarnings("unchecked")
    void solve() throws IOException  {
   //   BufferedReader re= new BufferedReader(new FileReader("input.txt"));
  //     BufferedReader re= new BufferedReader(new InputStreamReader(System.in));
 int n=nextInt();
 TreeSet<Integer>[] gen= new TreeSet[n+1];
 child= new TreeSet[n+1];
 netex= new int[n+1];
 boolean[] vis=new boolean[n+1];
        for (int i = 1; i < n+1; i++) {
            gen[i]=new TreeSet<Integer>();
            child[i]=new TreeSet<Integer>();
        }

 for (int i = 0; i < n-1; i++) {
         int a=nextInt();
         int b=nextInt();
 gen[a].add(b);gen[b].add(a);

 }
        for (int i = 1; i < n+1; i++) {
         netex[i]=nextInt();
        }
  TreeSet<Integer> ts= new TreeSet<Integer>();
  TreeSet<Integer> tmp= new TreeSet<Integer>();
  ts.add(1);
  vis[1]=true;
  while(!ts.isEmpty()){
  tmp.clear();
      for (Integer integer : ts) {
          for (Integer integer1 : gen[integer]) {
          if(!vis[integer1]){
          child[integer].add(integer1);
          vis[integer1]=true;
          tmp.add(integer1);
          }

          }

      }
  ts=       (TreeSet<Integer>) tmp.clone();

  }
  get(1);
      System.out.println(ans);
       // System.out.println(ans);
    }
static long ans=0;
    private long get(int i) {
    long ret=0;
        for (Integer chi : child[i]) {
            ret+=get(chi);
        }
    long val=ret+netex[i];
    ans+=abs(val);
    return -val+ret;
    }
}


import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st = new StringTokenizer("");

	final int maxn = 100010;

	int n;
	int head[] = new int[maxn];
	int next[] = new int[2 * maxn];
	int to[] = new int[2 * maxn];
	int cnt;
	int v[] = new int[maxn];

	void addEdge(int u, int v) {
		to[cnt] = v;
		next[cnt] = head[u];
		head[u] = cnt++;
	}

	Pair dfs(int u, int p) {
		int add = 0;
		int sub = 0;
		if (head[u] == -1) {
			if (v[u] <= 0)
				add = abs(v[u]);
			else
				sub = abs(v[u]);
		} else {
			//ArrayList<Pair> array = new ArrayList<Pair>();
			for (int i = head[u]; i != -1; i = next[i])
				if (to[i] != p) {
					Pair temp = dfs(to[i], u);
					add = max(add, temp.add);
					sub = max(sub, temp.sub);
				}
//			for (int i = 0; i < array.size(); i++) {
//				add = max(array.get(i).add, add);
//				sub = max(array.get(i).add, sub);
//			}
			v[u] = v[u] + add - sub;
			if (v[u] <= 0)
				add += abs(v[u]);
			else
				sub += v[u];
		}
		//System.err.println("" + (u + 1) + " abb = " + add + " sub = " + sub);
		return new Pair(add, sub);
	}

	private void run() throws IOException {
//		in = new BufferedReader(new FileReader("input.txt"));
//		out = new PrintWriter("output.txt");
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		fill(head, -1);
		cnt = 0;
		n = nextInt();
		for (int i = 0; i < n - 1; i++) {
			int from = nextInt();
			int to = nextInt();
			addEdge(from - 1, to - 1);
			addEdge(to - 1, from - 1);
		}
		for (int i = 0; i < n; i++)
			v[i] = nextInt();

		Pair ans = dfs(0, -1);
		out.println(ans.add + ans.sub);

		in.close();
		out.close();
	}

	public class Pair {
		public int add;
		int sub;

		public Pair(int add, int sub) {
			this.add = add;
			this.sub = sub;
		}
	}

	void chk(boolean b) {
		if (b)
			return;
		System.out.println(new Error().getStackTrace()[1]);
		exit(999);
	}
	void deb(String fmt, Object... args) {
		System.out.printf(Locale.US, fmt + "%n", args);
	}
	String nextToken() throws IOException {
		while (!st.hasMoreTokens())
			st = new StringTokenizer(in.readLine());
		return st.nextToken();
	}
	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}
	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}
	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
	String nextLine() throws IOException {
		st = new StringTokenizer("");
		return in.readLine();
	}
	boolean EOF() throws IOException {
		while (!st.hasMoreTokens()) {
			String s = in.readLine();
			if (s == null)
				return true;
			st = new StringTokenizer(s);
		}
		return false;
	}
}


import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st = new StringTokenizer("");

	final int maxn = 100010;

	int n;
	int head[] = new int[maxn];
	int next[] = new int[2 * maxn];
	int to[] = new int[2 * maxn];
	int cnt;
	long v[] = new long[maxn];

	void addEdge(int u, int v) {
		to[cnt] = v;
		next[cnt] = head[u];
		head[u] = cnt++;
	}

	Pair dfs(int u, int p) {
		long add = 0;
		long sub = 0;
		if (head[u] == -1) {
			if (v[u] <= 0)
				add = abs(v[u]);
			else
				sub = abs(v[u]);
		} else {
			//ArrayList<Pair> array = new ArrayList<Pair>();
			for (int i = head[u]; i != -1; i = next[i])
				if (to[i] != p) {
					Pair temp = dfs(to[i], u);
					add = max(add, temp.add);
					sub = max(sub, temp.sub);
				}
//			for (int i = 0; i < array.size(); i++) {
//				add = max(array.get(i).add, add);
//				sub = max(array.get(i).add, sub);
//			}
			v[u] = v[u] + add - sub;
			if (v[u] <= 0)
				add += abs(v[u]);
			else
				sub += v[u];
		}
		//System.err.println("" + (u + 1) + " abb = " + add + " sub = " + sub);
		return new Pair(add, sub);
	}

	private void run() throws IOException {
//		in = new BufferedReader(new FileReader("input.txt"));
//		out = new PrintWriter("output.txt");
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		fill(head, -1);
		cnt = 0;
		n = nextInt();
		for (int i = 0; i < n - 1; i++) {
			int from = nextInt();
			int to = nextInt();
			addEdge(from - 1, to - 1);
			addEdge(to - 1, from - 1);
		}
		for (int i = 0; i < n; i++)
			v[i] = nextInt();

		Pair ans = dfs(0, -1);
		out.println(ans.add + ans.sub);

		in.close();
		out.close();
	}

	public class Pair {
		public long add;
		long sub;

		public Pair(long add, long sub) {
			this.add = add;
			this.sub = sub;
		}
	}

	void chk(boolean b) {
		if (b)
			return;
		System.out.println(new Error().getStackTrace()[1]);
		exit(999);
	}
	void deb(String fmt, Object... args) {
		System.out.printf(Locale.US, fmt + "%n", args);
	}
	String nextToken() throws IOException {
		while (!st.hasMoreTokens())
			st = new StringTokenizer(in.readLine());
		return st.nextToken();
	}
	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}
	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}
	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
	String nextLine() throws IOException {
		st = new StringTokenizer("");
		return in.readLine();
	}
	boolean EOF() throws IOException {
		while (!st.hasMoreTokens()) {
			String s = in.readLine();
			if (s == null)
				return true;
			st = new StringTokenizer(s);
		}
		return false;
	}
}


import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
        int n = Integer.parseInt(tokenizer.nextToken());

        ArrayList<Integer>[] neighs = new ArrayList[n];
        for(int i = 0;i < n;i++)
        {
            neighs[i] = new ArrayList<Integer>();
        }

        for(int i = 0;i+1 < n;i++)
        {
            tokenizer = new StringTokenizer(reader.readLine());
            int a = Integer.parseInt(tokenizer.nextToken())-1;
            int b = Integer.parseInt(tokenizer.nextToken())-1;
            neighs[a].add(b);
            neighs[b].add(a);
        }

        tokenizer = new StringTokenizer(reader.readLine());
        long[] number = new long[n];

        for(int i = 0;i < n;i++)
            number[i] = Integer.parseInt(tokenizer.nextToken());

        ArrayList<Integer> queue = new ArrayList<Integer>();
        queue.add(0);
        boolean[] visited = new boolean[n];
        visited[0] = true;

        for(int i = 0;i < queue.size();i++)
            for(int j : neighs[queue.get(i)])
                if(!visited[j])
                {
                    visited[j] = true;
                    queue.add(j);
                }

        long[] increase = new long[n];
        long[] decrease = new long[n];

        for(int abc = n-1;abc >= 0;abc--)
        {
            int i = queue.get(abc);

            long incNeed = 0;
            long decNeed = 0;

            for(int j : neighs[i])
            {
                incNeed = Math.max(incNeed, increase[j]);
                decNeed = Math.max(decNeed, decrease[j]);
            }

            number[i] += incNeed;
            number[i] -= decNeed;

            if(number[i] > 0)
                decNeed += number[i];
            else
                incNeed -= number[i];

            increase[i] = incNeed;
            decrease[i] = decNeed;
        }

        System.out.println(increase[0]+decrease[0]);
    }
}


import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
        int n = Integer.parseInt(tokenizer.nextToken());

        ArrayList<Integer>[] neighs = new ArrayList[n];
        for(int i = 0;i < n;i++)
        {
            neighs[i] = new ArrayList<Integer>();
        }

        for(int i = 0;i+1 < n;i++)
        {
            tokenizer = new StringTokenizer(reader.readLine());
            int a = Integer.parseInt(tokenizer.nextToken())-1;
            int b = Integer.parseInt(tokenizer.nextToken())-1;
            neighs[a].add(b);
            neighs[b].add(a);
        }

        tokenizer = new StringTokenizer(reader.readLine());
        long[] number = new long[n];

        for(int i = 0;i < n;i++)
            number[i] = Integer.parseInt(tokenizer.nextToken());

        long res = 0;
        long add = 0;

        ArrayList<Integer> queue = new ArrayList<Integer>();
        queue.add(0);
        boolean[] visited = new boolean[n];
        visited[0] = true;

        for(int i = 0;i < queue.size();i++)
            for(int j : neighs[i])
                if(!visited[j])
                {
                    visited[j] = true;
                    queue.add(j);
                }

        long[] increase = new long[n];
        long[] decrease = new long[n];

        for(int abc = n-1;abc >= 0;abc--)
        {
            int i = queue.get(abc);

            long incNeed = 0;
            long decNeed = 0;

            for(int j : neighs[i])
            {
                incNeed = Math.max(incNeed, increase[j]);
                decNeed = Math.max(decNeed, decrease[j]);
            }

            number[i] += incNeed;
            number[i] -= decNeed;

            if(number[i] > 0)
                decNeed += number[i];
            else
                incNeed -= number[i];

            increase[i] = incNeed;
            decrease[i] = decNeed;
        }

        System.out.println(increase[0]+decrease[0]);
    }
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {

	ArrayList<Integer> g[];
	int degree[];
	long minV[], maxV[], a[];

	void dfs (int v, int pre) {
		long mini = 0;
		long maxi = 0;
		for (int i = 0; i < g[v].size(); i++) {
			int to = g[v].get(i);
			if (to == pre) continue;
			dfs (to, v);
			mini = min (minV[to], mini);
			maxi = max (maxV[to], maxi);
		}
		minV[v] = mini;
		maxV[v] = maxi;
		long result = maxi + mini + a[v];
		if (result > 0)
			minV[v] -= result;
		else
			maxV[v] -= result;
	}

	public void solve () throws Exception {
		int n = sc.nextInt();
		degree = new int [n];

		minV = new long [n];
		maxV = new long [n];

		g = new ArrayList [n];
		for (int i = 0; i < n; i++)
			g[i] = new ArrayList<>();

		for (int i = 0; i < n - 1; i++) {
			int v1 = sc.nextInt() - 1;
			int v2 = sc.nextInt() - 1;
			g[v1].add(v2);
			g[v2].add(v1);
			degree[v1]++;
			degree[v2]++;
		}

		a = new long [n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextLong();

		dfs (0, -1);
		out.println(maxV[0] - minV[0]);
	}

	BufferedReader in;
	PrintWriter out;
	FastScanner sc;

	static Throwable uncaught;

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			sc = new FastScanner(in);
			solve();
		} catch (Throwable t) {
			Solution.uncaught = t;
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) throws Throwable {
		Thread t = new Thread(null, new Solution(), "", (1 << 26));
		t.start();
		t.join();
		if (uncaught != null) {
			throw uncaught;
		}
	}

}

class FastScanner {

	BufferedReader reader;
	StringTokenizer strTok;

	public FastScanner(BufferedReader reader) {
		this.reader = reader;
	}

	public String nextToken() throws IOException {
		while (strTok == null || !strTok.hasMoreTokens()) {
			strTok = new StringTokenizer(reader.readLine());
		}
		return strTok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

}


import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {

	ArrayList<Integer> g[];
	long d[], a[];

	long result = 0;

	void dfs (int v, int pre) {
		long mini = 0;
		long maxi = 0;

		for (int i = 0; i < g[v].size(); i++) {
			int to = g[v].get(i);
			if (to == pre) continue;
			dfs (to, v);
			mini = min (mini, d[to]);
			maxi = max (maxi, d[to]);
		}

		d[v] = maxi + mini;
		result += abs(a[v] + d[v]);
		d[v] -= a[v];
	}

	public void solve () throws Exception {
		int n = sc.nextInt();
		g = new ArrayList [n];
		for (int i = 0; i < n; i++)
			g[i] = new ArrayList<>();

		for (int i = 0; i < n - 1; i++) {
			int v1 = sc.nextInt() - 1;
			int v2 = sc.nextInt() - 1;
			g[v1].add(v2);
			g[v2].add(v1);
		}

		a = new long [n];
		d = new long [n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextLong();

		dfs (0, -1);
		out.println(result);
	}

	BufferedReader in;
	PrintWriter out;
	FastScanner sc;

	static Throwable uncaught;

	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			sc = new FastScanner(in);
			solve();
		} catch (Throwable t) {
			Solution.uncaught = t;
		} finally {
			out.close();
		}
	}

	public static void main(String[] args) throws Throwable {
		Thread t = new Thread(null, new Solution(), "", (1 << 26));
		t.start();
		t.join();
		if (uncaught != null) {
			throw uncaught;
		}
	}

}

class FastScanner {

	BufferedReader reader;
	StringTokenizer strTok;

	public FastScanner(BufferedReader reader) {
		this.reader = reader;
	}

	public String nextToken() throws IOException {
		while (strTok == null || !strTok.hasMoreTokens()) {
			strTok = new StringTokenizer(reader.readLine());
		}
		return strTok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

}


import java.util.*;
import java.io.*;

public class solve {
	Scanner in;
	PrintWriter out;
	ArrayList<Integer>[] g;
	int[] v;
	boolean[] f;
	int n;

	class Pair {
		long plus, minus;
		public Pair(long plus, long minus) {
			this.plus = plus;
			this.minus = minus;
		}
	}

	Pair dfs(int i) {

		f[i] = true;
		long plus = 0;
		long minus = 0;
		for (int j = 0; j < g[i].size(); j++) {
			int to = g[i].get(j);
			if ( !f[to] ) {
				Pair res = dfs(to);
				if (res.plus > plus) {
					plus = res.plus;
				}
				if (res.minus > minus) {
					minus = res.minus;
				}
			}
		}
		if (( plus - minus + v[i] ) <= 0 ) {
			return new Pair(plus + Math.abs(plus - minus + v[i]), minus);
		} else {
			return new Pair(plus ,minus + Math.abs(plus - minus + v[i]));
		}
	}

	public void solve() throws IOException {
		n = in.nextInt();
		g = new ArrayList[n];
		v = new int[n];
		f = new boolean[n];

		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<Integer>();
		}

		for (int i = 0; i < n - 1; i++) {
			int a = in.nextInt() - 1;
			int b = in.nextInt() - 1;
			g[a].add(b);
			g[b].add(a);
		}
		for (int i = 0; i < n; i++) {
			v[i] = in.nextInt();
		}
		Pair ans = dfs(0);
		out.print(ans.plus + ans.minus);
	}

	public void run() {
		try {
			in = new Scanner(System.in);
			out = new PrintWriter(System.out);

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
	}

	public static void main(String[] arg) {
		new solve().run();
	}
}


import java.util.*;
import java.io.*;

public class solve {
	Scanner in;
	PrintWriter out;
	ArrayList<Integer>[] g;
	int[] v;
	long[] plusv, minusv;
	boolean[] f;
	int n;

	long dfs(int i) {

		f[i] = true;
		long plus = 0;
		long minus = 0;
		for (int j = 0; j < g[i].size(); j++) {
			int to = g[i].get(j);
			if ( !f[to] ) {
				long res = dfs(to);
				plus = Math.max(plus, plusv[to]);
				minus = Math.max(minus, minusv[to]);
			}
		}

		if (( plus - minus + v[i] ) <= 0 ) {
			plusv[i] = plus + Math.abs(plus - minus + v[i]);
			minusv[i] = minus;
		} else {
			plusv[i] = plus;
			minusv[i] = minus + Math.abs(plus - minus + v[i]);
		}
		return Math.abs(plusv[i] +minusv[i]);
	}

	public void solve() throws IOException {
		n = in.nextInt();
		g = new ArrayList[n];
		v = new int[n];
		plusv = new long[n];
		minusv = new long[n];
		f = new boolean[n];

		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<Integer>();
		}

		for (int i = 0; i < n - 1; i++) {
			int a = in.nextInt() - 1;
			int b = in.nextInt() - 1;
			g[a].add(b);
			g[b].add(a);
		}
		for (int i = 0; i < n; i++) {
			v[i] = in.nextInt();
		}
		out.print(dfs(0));
	}

	public void run() {
		try {
			in = new Scanner(System.in);
			out = new PrintWriter(System.out);

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
	}

	public static void main(String[] arg) {
		new solve().run();
	}
}


import java.util.*;
import java.io.*;

public class solve {
	Scanner in;
	PrintWriter out;
	ArrayList<Integer>[] g;
	int[] v;
	long[] plusv, minusv;
	boolean[] f;
	int n;

	long dfs(int i) {

		f[i] = true;
		long plus = 0;
		long minus = 0;
		for (int j = 0; j < g[i].size(); j++) {
			int to = g[i].get(j);
			if ( !f[to] ) {
				long res = dfs(to);
				plus = Math.max(plus, plusv[j]);
				minus = Math.max(minus, minusv[j]);
			}
		}

		if (( plus - minus + v[i] ) <= 0 ) {
			plusv[i] = plus + Math.abs(plus - minus + v[i]);
			minusv[i] = minus;
		} else {
			plusv[i] = plus;
			minusv[i] = minus + Math.abs(plus - minus + v[i]);
		}
		return Math.abs(plusv[i] +minusv[i]);
	}

	public void solve() throws IOException {
		n = in.nextInt();
		g = new ArrayList[n];
		v = new int[n];
		plusv = new long[n];
		minusv = new long[n];
		f = new boolean[n];

		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<Integer>();
		}

		for (int i = 0; i < n - 1; i++) {
			int a = in.nextInt() - 1;
			int b = in.nextInt() - 1;
			g[a].add(b);
			g[b].add(a);
		}
		for (int i = 0; i < n; i++) {
			v[i] = in.nextInt();
		}
		out.print(dfs(0));
	}

	public void run() {
		try {
			in = new Scanner(System.in);
			out = new PrintWriter(System.out);

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
	}

	public static void main(String[] arg) {
		new solve().run();
	}
}


import java.io.*;
import java.util.*;

public class B implements Runnable {

	int[] a;
	long[] minus, plus;
	ArrayList<Integer>[] g;
	boolean[] used;
	long ans;

	private void Solution() throws IOException {
		int n = nextInt();
		g = new ArrayList[n];
		for (int i = 0; i < n; i++)
			g[i] = new ArrayList<Integer>(0);
		for (int i = 0; i < n - 1; i++) {
			int x = nextInt() - 1, y = nextInt() - 1;
			g[x].add(y);
			g[y].add(x);
		}
		a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		used = new boolean[n];
		minus = new long[n];
		plus = new long[n];
		long[] ans = dfs(0);
		System.out.println(ans[0] + ans[1]);
	}

	long[] dfs(int v) {
		used[v] = true;
		long[] res = new long[2];
		for (int to : g[v])
			if (!used[to]) {
				long[] tmp = dfs(to);
				res[0] = Math.max(res[0], tmp[0]);
				res[1] = Math.max(res[1], tmp[1]);
			}
		long total = res[0] - res[1] + a[v];
		if (total < 0)
			res[0] -= total;
		else
			res[1] += total;
		return res;
	}

	public static void main(String[] args) {
		new B().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(261);
		}
	}

	void halt() {
		out.close();
		System.exit(0);
	}

	void print(Object... obj) {
		for (int i = 0; i < obj.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(obj[i]);
		}
	}

	void println(Object... obj) {
		print(obj);
		out.println();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	String nextLine() throws IOException {
		return in.readLine();
	}

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class R168qDZeroTree {

	static int n;
	static int v[];
	static ArrayList<Integer> g[];

	@SuppressWarnings("unchecked")
	public static void main(String args[] ) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    PrintWriter w = new PrintWriter(System.out);

	    n = ip(br.readLine());

	    g = new ArrayList[n];
		for(int i=0;i<n;i++)
			g[i] = new ArrayList<Integer>();
		for(int i=1;i<n;i++){
			StringTokenizer st = new StringTokenizer(br.readLine());
			int u = ip(st.nextToken()) - 1;
			int v = ip(st.nextToken()) - 1;
			g[u].add(v);
			g[v].add(u);
		}

	    StringTokenizer st2 = new StringTokenizer(br.readLine());
	    v = new int[n];
	    for(int i=0;i<n;i++)
	    	v[i] = ip(st2.nextToken());

	    Node ans = dfs(0,-1);
	    w.println(ans.inc + ans.dec);
	    w.close();
	}

	public static Node dfs(int curr,int prev){
		Node ret = new Node(0,0);

		int s = g[curr].size();
		for(int j=0;j<s;j++){
			if(g[curr].get(j) == prev)	continue;
			Node back = dfs(g[curr].get(j),curr);
			ret.inc = Math.max(ret.inc, back.inc);
			ret.dec = Math.max(ret.dec, back.dec);
		}

		v[curr] = v[curr] + ret.inc - ret.dec;
		if(v[curr] > 0)	ret.dec += v[curr];
		if(v[curr] < 0)	ret.inc -= v[curr];

		return ret;
	}

	static public class Node{
		int inc,dec;
		Node(int i,int d){
			inc = i;
			dec = d;
		}
	}

	public static int ip(String s){
		return Integer.parseInt(s);
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class R168qDZeroTree {

	static int n;
	static long v[];
	static ArrayList<Integer> g[];

	@SuppressWarnings("unchecked")
	public static void main(String args[] ) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    PrintWriter w = new PrintWriter(System.out);

	    n = ip(br.readLine());

	    g = new ArrayList[n];
		for(int i=0;i<n;i++)
			g[i] = new ArrayList<Integer>();
		for(int i=1;i<n;i++){
			StringTokenizer st = new StringTokenizer(br.readLine());
			int u = ip(st.nextToken()) - 1;
			int v = ip(st.nextToken()) - 1;
			g[u].add(v);
			g[v].add(u);
		}

	    StringTokenizer st2 = new StringTokenizer(br.readLine());
	    v = new long[n];
	    for(int i=0;i<n;i++)
	    	v[i] = ip(st2.nextToken());

	    Node ans = dfs(0,-1);
	    w.println(ans.inc + ans.dec);
	    w.close();
	}

	public static Node dfs(int curr,int prev){
		Node ret = new Node(0L,0L);

		int s = g[curr].size();
		for(int j=0;j<s;j++){
			if(g[curr].get(j) == prev)	continue;
			Node back = dfs(g[curr].get(j),curr);
			ret.inc = Math.max(ret.inc, back.inc);
			ret.dec = Math.max(ret.dec, back.dec);
		}

		v[curr] = v[curr] + ret.inc - ret.dec;
		if(v[curr] > 0)	ret.dec += v[curr];
		if(v[curr] < 0)	ret.inc -= v[curr];

		return ret;
	}

	static public class Node{
		long inc,dec;
		Node(long i,long d){
			inc = i;
			dec = d;
		}
	}

	public static int ip(String s){
		return Integer.parseInt(s);
	}
}


import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;

/**
 * Actual solution is at the top, in class Solver
 */
final public class Main implements Runnable {
	private static String[] args;

	public static void main(String[] args) {
		Main.args = args;
		new Thread(null, new Main(), "MyRunThread", 1 << 27).start();
	}

	//@Override
	public void run() {
		long time_beg = -1;
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		if (args.length > 0 && args[0].equals("outside")) {
			time_beg = System.currentTimeMillis();
			try {
				inputStream = new FileInputStream("IO/in.txt");
//				outputStream = new FileOutputStream("IO/out.txt");
			} catch (Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		} else {
			try {
//				inputStream = new FileInputStream("IO/in.txt");
//				outputStream = new FileOutputStream("IO/out.txt");
			} catch (Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		}

		Solver s = new Solver();
		s.in = new InputReader(inputStream);
		s.out = new OutputWriter(outputStream);
		if (args.length > 0 && args[0].equals("outside")) {
			s.dout = new DebugWriter(s.out);
		}

		s.solve();

		if (args.length > 0 && args[0].equals("outside")) {
			s.dout.printFormat("*** Total time: %.3f ***\n", (System.currentTimeMillis() - time_beg) / 1000.0);
		}

		s.out.close();
	}
}

final class Solver {
	InputReader in;
	OutputWriter out;
	DebugWriter dout;

	public void solve() {
		int n = in.readInt();
		int[] sz = new int[n];
		int[] A = new int[n - 1];
		int[] B = new int[n - 1];
		for (int i = 0; i < n - 1; ++i) {
			A[i] = in.readInt() - 1;
			B[i] = in.readInt() - 1;
			++sz[A[i]];
		}

		g = new int[n][];
		for (int i = 0; i < n; ++i) {
			g[i] = new int[sz[i]];
			sz[i] = 0;
		}

		for (int i = 0; i < n - 1; ++i) {
			g[A[i]][sz[A[i]]++] = B[i];
		}

		mas = new int[n];
		for (int i = 0; i < n; ++i)
			mas[i] = in.readInt();

		Pair<Long, Long> p = rec(0);
		out.printLine(p.first + p.second);
	}

	int[] mas;
	int[][] g;

	Pair<Long, Long> rec(int v) {
		long add = 0;
		long sub = 0;

		for (int i = 0; i < g[v].length; ++i) {
			Pair<Long, Long> p = rec(g[v][i]);

			add = Math.max(add, p.first);
			sub = Math.max(sub, p.second);
		}

		long val = add - sub + mas[v];

		if (val > 0) sub += val;
		else add += -val;

		return new Pair<Long, Long>(add, sub);
	}
}

final class InputReader {
	private boolean finished = false;

	private InputStream stream;
	private byte[] buf = new byte[1 << 10];
	private int curChar;
	private int numChars;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	private int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int peek() {
		if (numChars == -1)
			return -1;
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				return -1;
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar];
	}

	public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long readLong() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public String readString() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	public static boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private String readLine0() {
		StringBuilder buf = new StringBuilder();
		int c = read();
		while (c != '\n' && c != -1) {
			if (c != '\r')
				buf.appendCodePoint(c);
			c = read();
		}
		return buf.toString();
	}

	public String readLine() {
		String s = readLine0();
		while (s.trim().length() == 0)
			s = readLine0();
		return s;
	}

	public String readLine(boolean ignoreEmptyLines) {
		if (ignoreEmptyLines)
			return readLine();
		else
			return readLine0();
	}

	public BigInteger readBigInteger() {
		try {
			return new BigInteger(readString());
		} catch (NumberFormatException e) {
			throw new InputMismatchException();
		}
	}

	public char readCharacter() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		return (char) c;
	}

	public double readDouble() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		double res = 0;
		while (!isSpaceChar(c) && c != '.') {
			if (c == 'e' || c == 'E')
				return res * Math.pow(10, readInt());
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		}
		if (c == '.') {
			c = read();
			double m = 1;
			while (!isSpaceChar(c)) {
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, readInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				m /= 10;
				res += (c - '0') * m;
				c = read();
			}
		}
		return res * sgn;
	}

	public boolean isExhausted() {
		int value;
		while (isSpaceChar(value = peek()) && value != -1)
			read();
		return value == -1;
	}
}

final class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream), 1 << 10));
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object... objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(Object... objects) {
		print(objects);
		writer.println();
	}

	public void printFormat(String format, Object... objects) {
		writer.printf(format, objects);
	}

	public void print(char[] objects) {
		writer.print(objects);
	}

	public void printLine(char[] objects) {
		writer.println(objects);
	}

	public void printLine(char[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(int[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(int[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(int[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(short[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(short[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(short[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(long[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(long[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(long[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(double[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(double[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(double[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(byte[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(byte[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(byte[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(boolean[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(boolean[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(boolean[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void close() {
		writer.close();
	}

	public void flush() {
		writer.flush();
	}
}

final class DebugWriter {
	private final OutputWriter writer;

	public DebugWriter(OutputWriter writer) {
		this.writer = writer;
	}

	private void printDebugMessage() {
		writer.print("DEBUG:\t");
	}

	public void printLine(Object... objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printFormat(String format, Object... objects) {
		flush();
		printDebugMessage();
		writer.printFormat(format, objects);
		flush();
	}

	public void printLine(char[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(char[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(double[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(double[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(int[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(int[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(short[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(short[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(long[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(long[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(byte[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(byte[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(boolean[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(boolean[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void flush() {
		writer.flush();
	}
}

final class Pair<F, S> {
	public F first;
	public S second;

	public Pair(F first, S second) {
		this.first = first;
		this.second = second;
	}

	@Override
	public int hashCode() {
		int hFirst = first != null ? first.hashCode() : 0;
		int hSecond = second != null ? second.hashCode() : 0;
		return 31 * hFirst + hSecond;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		Pair pair = (Pair) o;

		return (first != null ? first.equals(pair.first) : pair.first == null) &&
				(second != null ? second.equals(pair.second) : pair.second == null);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return new Pair<F, S>(first, second);
	}

	@Override
	public String toString() {
		return "(" + first + ", " + second + ")";
	}
}


import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;

/**
 * Actual solution is at the top, in class Solver
 */
final public class Main implements Runnable {
	private static String[] args;

	public static void main(String[] args) {
		Main.args = args;
		new Thread(null, new Main(), "MyRunThread", 1 << 27).start();
	}

	//@Override
	public void run() {
		long time_beg = -1;
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		if (args.length > 0 && args[0].equals("outside")) {
			time_beg = System.currentTimeMillis();
			try {
				inputStream = new FileInputStream("IO/in.txt");
//				outputStream = new FileOutputStream("IO/out.txt");
			} catch (Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		} else {
			try {
//				inputStream = new FileInputStream("IO/in.txt");
//				outputStream = new FileOutputStream("IO/out.txt");
			} catch (Exception e) {
				System.err.println(e);
				System.exit(1);
			}
		}

		Solver s = new Solver();
		s.in = new InputReader(inputStream);
		s.out = new OutputWriter(outputStream);
		if (args.length > 0 && args[0].equals("outside")) {
			s.dout = new DebugWriter(s.out);
		}

		s.solve();

		if (args.length > 0 && args[0].equals("outside")) {
			s.dout.printFormat("*** Total time: %.3f ***\n", (System.currentTimeMillis() - time_beg) / 1000.0);
		}

		s.out.close();
	}
}

final class Solver {
	InputReader in;
	OutputWriter out;
	DebugWriter dout;

	public void solve() {
		int n = in.readInt();
		int[] sz = new int[n];
		int[] A = new int[n - 1];
		int[] B = new int[n - 1];
		for (int i = 0; i < n - 1; ++i) {
			A[i] = in.readInt() - 1;
			B[i] = in.readInt() - 1;
			++sz[A[i]];
			++sz[B[i]];
		}

		g = new int[n][];
		for (int i = 0; i < n; ++i) {
			g[i] = new int[sz[i]];
			sz[i] = 0;
		}

		for (int i = 0; i < n - 1; ++i) {
			g[A[i]][sz[A[i]]++] = B[i];
			g[B[i]][sz[B[i]]++] = A[i];
		}

		mas = new int[n];
		for (int i = 0; i < n; ++i)
			mas[i] = in.readInt();

		Pair<Long, Long> p = rec(0, -1);
		out.printLine(p.first + p.second);
	}

	int[] mas;
	int[][] g;

	Pair<Long, Long> rec(int v, int parent) {
		long add = 0;
		long sub = 0;

		for (int i = 0; i < g[v].length; ++i)
			if (parent != g[v][i]) {
				Pair<Long, Long> p = rec(g[v][i], v);

				add = Math.max(add, p.first);
				sub = Math.max(sub, p.second);
			}

		long val = add - sub + mas[v];

		if (val > 0) sub += val;
		else add += -val;

		return new Pair<Long, Long>(add, sub);
	}
}

final class InputReader {
	private boolean finished = false;

	private InputStream stream;
	private byte[] buf = new byte[1 << 10];
	private int curChar;
	private int numChars;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	private int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int peek() {
		if (numChars == -1)
			return -1;
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				return -1;
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar];
	}

	public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long readLong() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public String readString() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	public static boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private String readLine0() {
		StringBuilder buf = new StringBuilder();
		int c = read();
		while (c != '\n' && c != -1) {
			if (c != '\r')
				buf.appendCodePoint(c);
			c = read();
		}
		return buf.toString();
	}

	public String readLine() {
		String s = readLine0();
		while (s.trim().length() == 0)
			s = readLine0();
		return s;
	}

	public String readLine(boolean ignoreEmptyLines) {
		if (ignoreEmptyLines)
			return readLine();
		else
			return readLine0();
	}

	public BigInteger readBigInteger() {
		try {
			return new BigInteger(readString());
		} catch (NumberFormatException e) {
			throw new InputMismatchException();
		}
	}

	public char readCharacter() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		return (char) c;
	}

	public double readDouble() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		double res = 0;
		while (!isSpaceChar(c) && c != '.') {
			if (c == 'e' || c == 'E')
				return res * Math.pow(10, readInt());
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		}
		if (c == '.') {
			c = read();
			double m = 1;
			while (!isSpaceChar(c)) {
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, readInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				m /= 10;
				res += (c - '0') * m;
				c = read();
			}
		}
		return res * sgn;
	}

	public boolean isExhausted() {
		int value;
		while (isSpaceChar(value = peek()) && value != -1)
			read();
		return value == -1;
	}
}

final class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream), 1 << 10));
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object... objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(Object... objects) {
		print(objects);
		writer.println();
	}

	public void printFormat(String format, Object... objects) {
		writer.printf(format, objects);
	}

	public void print(char[] objects) {
		writer.print(objects);
	}

	public void printLine(char[] objects) {
		writer.println(objects);
	}

	public void printLine(char[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(int[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(int[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(int[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(short[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(short[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(short[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(long[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(long[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(long[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(double[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(double[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(double[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(byte[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(byte[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(byte[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void print(boolean[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

	public void printLine(boolean[] objects) {
		print(objects);
		writer.println();
	}

	public void printLine(boolean[][] objects) {
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
	}

	public void close() {
		writer.close();
	}

	public void flush() {
		writer.flush();
	}
}

final class DebugWriter {
	private final OutputWriter writer;

	public DebugWriter(OutputWriter writer) {
		this.writer = writer;
	}

	private void printDebugMessage() {
		writer.print("DEBUG:\t");
	}

	public void printLine(Object... objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printFormat(String format, Object... objects) {
		flush();
		printDebugMessage();
		writer.printFormat(format, objects);
		flush();
	}

	public void printLine(char[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(char[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(double[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(double[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(int[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(int[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(short[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(short[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(long[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(long[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(byte[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(byte[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void printLine(boolean[] objects) {
		flush();
		printDebugMessage();
		writer.printLine(objects);
		flush();
	}

	public void printLine(boolean[][] objects) {
		flush();
		for (int i = 0; i < objects.length; ++i)
			printLine(objects[i]);
		flush();
	}

	public void flush() {
		writer.flush();
	}
}

final class Pair<F, S> {
	public F first;
	public S second;

	public Pair(F first, S second) {
		this.first = first;
		this.second = second;
	}

	@Override
	public int hashCode() {
		int hFirst = first != null ? first.hashCode() : 0;
		int hSecond = second != null ? second.hashCode() : 0;
		return 31 * hFirst + hSecond;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		Pair pair = (Pair) o;

		return (first != null ? first.equals(pair.first) : pair.first == null) &&
				(second != null ? second.equals(pair.second) : pair.second == null);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return new Pair<F, S>(first, second);
	}

	@Override
	public String toString() {
		return "(" + first + ", " + second + ")";
	}
}


import java.util.*;
import java.io.*;
import java.lang.*;

public class tree {

	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(in, out);
		out.close();
	}
}

class TaskB {
	public ArrayList<Integer>[] vc;
	public boolean[] visit;
	public int[] a;

	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt();
		vc = new ArrayList[n+5];
		visit = new boolean[n+5];
		a = new int[n+5];
		int u, v;

		for (int i=1; i<=n; ++i)
			vc[i] = new ArrayList<Integer>();
		Arrays.fill(visit, false);

		for (int i=1; i<n; ++i) {
			u = in.nextInt();
			v = in.nextInt();
			vc[u].add(v);
			vc[v].add(u);
		}

		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}

		pair p = dfs(1);
		long ans = p.fir - p.sec;
		out.println(ans);
	}

	public pair dfs(int u) {
		pair ret = new pair(), p;
		long tmp;

		visit[u] = true;
		for (int v : vc[u]) {
			if (!visit[v]) {
				p = dfs(v);
				ret.fir = Math.max(p.fir, ret.fir);
				ret.sec = Math.min(p.sec, ret.sec);
			}
		}

		if (ret.fir == Long.MIN_VALUE) {
			tmp = a[u];
			if (tmp >= 0) {
				ret.sec = -tmp;
				ret.fir = 0;
			} else {
				ret.fir = -tmp;
				ret.sec = 0;
			}
		} else {
			tmp = a[u] + ret.fir + ret.sec;
			if (tmp >= 0) {
				ret.sec -= tmp;
			} else {
				ret.fir -= tmp;
			}
		}

		return ret;
	}
}

class pair {
	long fir, sec;
	public pair() {
		fir = Long.MIN_VALUE;
		sec = Long.MAX_VALUE;
	}
	public pair(long fir, long sec) {
		this.fir = fir;
		this.sec = sec;
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}
}


#include<iostream>
#include<string>
using namespace std;
bool check(string inp,int k)
{
     int *a=new int[k+1];
     for(int i=0;i<=k;i++)
     a[i]=0;
     for(int i=0;i<inp.length();i++)
     {
          a[int(inp[i])-int('0')]=1;
     }

     for(int i=0;i<=k;i++)
     {
       if(a[i]!=1)
       return false;
     }

     return true;
}

int main()
{
    int n,k;
    string inp;
    cin>>n>>k;
    string *arr=new string[n];
    for(int i=0;i<n;i++)
    {
        cin>>inp;
        arr[i]=inp;
    }
    int count=0;
    for(int i=0;i<n;i++)
    {
        if(arr[i].length()<=k)
        continue;
        if(check(arr[i],k))
        count++;
    }
    cout<<count<<endl;
    //system("pause");
    return 0;
}


#include<iostream>
#include<string>
using namespace std;
bool check(string inp,int k,int *a)
{

     for(int i=0;i<k+1;i++)
     a[i]=0;

     for(int i=0;i<inp.length();i++)
     {
          a[int(inp[i])-int('0')]=1;
     }

     for(int i=0;i<=k;i++)
     {
       if(a[i]!=1)
       {
       // cout<<inp<<endl;
        return false;
       }

     }

     return true;
}

int main()
{
    int n,k;
    string inp;
    cin>>n>>k;
    string *arr=new string[n];

     int* a=new int[k+1];
     for(int i=0;i<=k;i++)
     a[i]=0;

    for(int i=0;i<n;i++)
    {
        cin>>inp;
        arr[i]=inp;
    }
    int count=0;
    for(int i=0;i<n;i++)
    {
        //if(arr[i].length()<=k)
        //continue;
        if(check(arr[i],k,a))
        count++;
    }
    cout<<count<<endl;
    //system("pause");
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <fstream>
#include <string>
#include <memory>
#include <utility>
#include <ctype.h>
#include <cstdlib>
#include <cassert>
#include <cstring>
using namespace std;
#define MIN(a,b)  (((a)<(b)) ? a : b)
#define MAX(a,b)   (((a)>(b)) ? a : b)
#define SUM(a,b,c)  (a+b+c)
#define MU(a,b) a*b
#define Di(a,b) a/b
#define FD(i,n) for (i=0;i<(int)n;i++)
#define FA(i,a,b)		for(int i=(a);i<=(b);i++)
#define FS(i,s)    for (int i=0;i<s.size();i++)
#define Fi(i,n)   for (int i=0;i<(int)n ;i++)
//#define FS1(i,s)    for (int i=0;i<s.size();i++)
#define FV(i,v)   for (int i=0;i<v.size();i++)
#define wh(a)   while (a--)
#define DA(n)     cin>>n;
#define DA2(n,m)    cin>>n>>m;
#define DA3(n,m,k)     cin>>n>>m>>k;
#define cout(a,b,c)    cout<<a<<" "<<b<<" "<<c;
#define cout(a,b)  cout<<a<<" "<<b<<endl;
#define pb push_back
#define c(a,b)  a%=b;
#define d(a,b)  b%=a;
#define GCD(a,b)
#define SORT(v)  sort(v.begin(),v.end())
#define SZ(X) (int)(X).size()
#define N  101
typedef long long ll;
typedef vector<int> w;
typedef vector<char> h;
typedef vector<string> W;
int i;
//string f="hello";
string s,s1="";
//int a=0;
//string arr[]={"NO","YES"};
//hello
//int k,periodc,one=0,two=0,elanswer=0;
//int n;
//int a;

//vector<int>v;
void solve()
{
    int n,k;
    cin>>n>>k;
    int x;
    int f=0;
    Fi(i,n)
    {
        DA(x);
        set<int>s;

        while(x){s.insert(x%10); x=x/10;}
        int b=0;
        for (int j=0;j<=k;j++){
            if (s.find(j)==s.end())b=1;}

        if (!b)f++;

    }
    cout<<f<<endl;
}

int  main ()
{
    solve();

   return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k,c=0;
    vector <int>l;
    cin>>a>>k;
    int f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }
//for (int i=0;i<l.size();i++)cout<<l[i]<<endl;

    for (int i=0;i<l.size();i++)
    {
        int p=0;
        string s="";
       stringstream ll;
       ll<<l[i];
       ll>>s;
       //cout<<s<<"ll"<<endl;
       int m=s.size();
      // cout<<m;
        if (m-1==k){c++;}

    }
    cout<<c<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <fstream>
#include <string>
#include <memory>
#include <utility>
#include <ctype.h>
#include <cstdlib>
#include <cassert>
#include <cstring>
using namespace std;
#define MIN(a,b)  (((a)<(b)) ? a : b)
#define MAX(a,b)   (((a)>(b)) ? a : b)
#define SUM(a,b,c)  (a+b+c)
#define MU(a,b) a*b
#define Di(a,b) a/b
#define FD(i,n) for (i=0;i<(int)n;i++)
#define FA(i,a,b)		for(int i=(a);i<=(b);i++)
#define FS(i,s)    for (int i=0;i<s.size();i++)
#define Fi(i,n)   for (int i=0;i<(int)n ;i++)
//#define FS1(i,s)    for (int i=0;i<s.size();i++)
#define FV(i,v)   for (int i=0;i<v.size();i++)
#define wh(a)   while (a--)
#define DA(n)     cin>>n;
#define DA2(n,m)    cin>>n>>m;
#define DA3(n,m,k)     cin>>n>>m>>k;
#define cout(a,b,c)    cout<<a<<" "<<b<<" "<<c;
#define cout(a,b)  cout<<a<<" "<<b<<endl;
#define pb push_back
#define c(a,b)  a%=b;
#define d(a,b)  b%=a;
#define GCD(a,b)
#define SORT(v)  sort(v.begin(),v.end())
#define SZ(X) (int)(X).size()
#define N  101
typedef long long ll;
typedef vector<int> w;
typedef vector<char> h;
typedef vector<string> W;
int i;
//string f="hello";
string s,s1="";
//int a=0;
//string arr[]={"NO","YES"};
//hello
int k,periodc,one=0,two=0,elanswer=0;
int n;
//int a;
int f=0;
//vector<int>v;
int  main ()
{
  cin>>n>>k;

 Fi(i,n)
 {
    cin>>s;

   FS(i,s)
   {
      if (s.size()-1==k){f++;}
   }

 }
 cout<<f/(k+1)<<endl;
   return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k,c=0;
    vector <int>l;
    cin>>a>>k;
    int f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }
//for (int i=0;i<l.size();i++)cout<<l[i]<<endl;

    for (int i=0;i<l.size();i++)
    {
        int p=0;
       stringstream ll;
       string s="";
       ll<<l[i];
       ll>>s;
       //cout<<s<<"ll"<<endl;
       for (int o=0;o<k;o++)
        {
            p++;
        }
       // cout<<p;
        if (p==k){c++;}

    }
    cout<<c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k,c=0;
    vector <int>l;
    cin>>a>>k;
    int f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }
//for (int i=0;i<l.size();i++)cout<<l[i]<<endl;

    for (int i=0;i<l.size();i++)
    {
        int p=0;
       stringstream ll;
       string s="";
       ll<<l[i];
       ll>>s;
       //cout<<s<<"ll"<<endl;
       for (int o=0;o<s.size();o++)
        {
            p++;
        }
       // cout<<p;
        if (p==k+1){c++;}

    }
    cout<<c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k,c=0;
    vector <int>l;
    cin>>a>>k;
    int f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }
//for (int i=0;i<l.size();i++)cout<<l[i]<<endl;

    for (int i=0;i<l.size();i++)
    {
        int p=0;
        string s="";
       stringstream ll;
       ll<<l[i];
       ll>>s;
       //cout<<s<<"ll"<<endl;
       int m=s.size();
      // cout<<m;
        if (m-1==k){c++;}

    }
    if (k==0)cout<<a<<endl;
    else
    cout<<c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k;
    vector <int>l;
    cin>>a>>k;
    int f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }

    int p=0;
    for (int i=0;i<l.size();i++)
    {
       stringstream ll;
       string s="";
       ll<<l[i];
       ll>>s;
       if (s.size()==k){p++;}

    }
    cout<<p<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k,c=0;
    vector <int>l;
    cin>>a>>k;
    long long  f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }
//for (int i=0;i<l.size();i++)cout<<l[i]<<endl;

    for (int i=0;i<l.size();i++)
    {
        int p=0;
        string s="";
       stringstream ll;
       ll<<l[i];
       ll>>s;
       //cout<<s<<"ll"<<endl;
       int m=s.size();
      // cout<<m;
        if (m-1==k){c++;}

    }
    if (k==0)cout<<a<<endl;
    else
    cout<<c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{

    int a,k,p=0;
    vector <int>l;
    cin>>a>>k;
    int f[a];
    for (int i=0;i<a;i++)
    {
        cin>>f[i];
        l.push_back(f[i]);

    }
//for (int i=0;i<l.size();i++)cout<<l[i]<<endl;

    for (int i=0;i<l.size();i++)
    {
       stringstream ll;
       string s="";
       ll<<l[i];
       ll>>s;
       //cout<<s<<"ll"<<endl;
       if (s.size()-1==k){p++;}

    }
    cout<<p<<endl;
    return 0;
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[12]; scanf("%s", a);
        set<int> t;
        REP(j, strlen(a)) t.insert(a[j]-'0');
        int f=0;
        if (t.size()!=k+1)
            continue;
        int kk=0;
        for (set<int>::iterator it=t.begin(); it!=t.end(); it++, kk++)
            if (*it!=kk) break;
        if (kk==k+1)
            ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[12]; scanf("%s", a);
        set<int> t;
        REP(j, strlen(a)) t.insert(a[j]-'0');
        if (t.size()==k+1) ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[12]; scanf("%s", a);
        set<int> t;
        REP(j, strlen(a))
            t.insert(a[j]-'0');
        int kk=0;
        for (typeof(t.begin()) it=t.begin(); it!=t.end(); it++, kk++)
            if (*it!=kk) break;
        if (kk>k)
            ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[10]; scanf("%s", &a);
        set<int> t;
        REP(j, strlen(a)) t.insert(a[j]-'0');
        if (t.size()==k+1) ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <algorithm>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[10]; scanf("%s", &a);
        int f=(1<<(k+1))-1;
        int t=0;
        REP(j, strlen(a))
            t|=1<<(a[j]-'0');
        if (t==f)
            ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[12]; scanf("%s", a);
        set<int> t;
        REP(j, strlen(a))
            t.insert(a[j]-'0');
        if (t.size()<k+1)
            continue;
        int kk=0;
        for (typeof(t.begin()) it=t.begin(); it!=t.end(); it++, kk++)
            if (*it!=kk) break;
        if (kk==t.size())
            ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[12]; scanf("%s", a);
        set<int> t;
        REP(j, strlen(a))
            t.insert(a[j]-'0');
        int kk=0;
        for (typeof(t.begin()) it=t.begin(); it!=t.end(); it++, kk++)
            if (*it!=kk) break;
        if (kk==t.size())
            ans++;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <algorithm>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[10]; scanf("%s", &a);
        int l=strlen(a);
        sort(a, a+strlen(a));
        REP(j, l)
            if ((a[j]-'0')>j) goto next;
        ans++;
next:
        ;
    }
    printf("%d\n", ans);
}


using namespace std;

#include <cstdio>
#include <cstring>
#include <algorithm>
#define REP(i, n) for (int i=0; i<(n); i++)
#define FOR(i, a, b) for (int i=(a); i<=(b); i++)
#define FORD(i, a, b) for (int i=(a); i>=(b); i--)

int main(void) {
    int n, k; scanf("%d %d", &n, &k);
    int ans=0;
    REP(i, n) {
        char a[10]; scanf("%s", &a);
        int f=(1<<(k+1))-1;
        int t=0;
        int d=0;
        REP(j, strlen(a)) {
            if ((t&(1<<(a[j]-'0')))==0)
                t|=1<<(a[j]-'0');
            else {
                d=1; break;
            }
        }
        if (t==f && !d)
            ans++;
    }
    printf("%d\n", ans);
}