#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define lli long long
#define mod 1000000007
#define mod2 998244353
#define si size()
#define dou double
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void init_code(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
const int mx=1e5+5;
ll int color[mx];
map<pair<ll int,ll int>,ll int> dp;
vector<int> adj[mx];
ll int in[mx];
ll inf=1e11;
long long dfs(ll int node,ll int col,ll int par){
ll ans=0;
if(dp[{node,col}]!=0){
return dp[{node,col}];
}
for(int next:adj[node]){
if(next!=par){
ll minim=inf;
if(color[next]!=-1){
if(color[next]!=col){
minim=min(dfs(next,color[next],node),minim);
}
}else{
for(int i=1;i<=in[next]+1;i++){
if(i!=col){
minim=min(dfs(next,i,node),minim);
}
}
}
ans+=minim;
//cout<<minim<<" "<<next<<"\n";
}
}
ans+=col;
return dp[{node,col}]=ans;
}
int main(){
fast();
init_code();
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
in[a]++;
in[b]++;
}
for(ll int i=1;i<=n;i++){
cin>>color[i];
}
ll an=inf;
if(color[1]!=-1){
an=min(dfs(1,color[1],-1),an);
}else{
for(int i=1;i<=in[i]+1;i++){
an=min(dfs(1,i,-1),an);
}
}
cout<<an<<"\n";
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBzeih4KSAoaW50KXguc2l6ZSgpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgbGwgbG9uZyBsb25nIAojZGVmaW5lIGxs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gICAgICBpZihuZXh0IT1wYXIpewogICAgICAgICAgICAKICAgICAgICAgICAgbGwgbWluaW09aW5mOwogICAgICAgIGlmKGNvbG9yW25leHRdIT0tMSl7CiAgICAgICAgICAgIGlmKGNvbG9yW25leHRdIT1jb2wpewogICAgICAgICAgbWluaW09bWluKGRmcyhuZXh0LGNvbG9yW25leHRdLG5vZGUpLG1pbmltKTsKICAgICAgICAgICAgfQogICAgICAgIH1lbHNlewogICAgICAgICAgICBmb3IoaW50IGk9MTtpPD1pbltuZXh0XSsxO2krKyl7CiAgICAgICAgICAgICAgICBpZihpIT1jb2wpewogICAgICAgICAgICAgICAgbWluaW09bWluKGRmcyhuZXh0LGksbm9kZSksbWluaW0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGFucys9bWluaW07CiAgICAgICAgLy9jb3V0PDxtaW5pbTw8IiAiPDxuZXh0PDwiXG4iOwogICAgICAgIH0KICAgICAgICAKICAgIH0KICAgIGFucys9Y29sOwogICAgCiAgICByZXR1cm4gZHBbe25vZGUsY29sfV09YW5zOwp9CgogCiAKaW50IG1haW4oKXsKICBmYXN0KCk7CiAgaW5pdF9jb2RlKCk7CiAgaW50IG47CiAgY2luPj5uOwogIGZvcihpbnQgaT0wO2k8bi0xO2krKyl7CiAgICBpbnQgYSxiOwogICAgY2luPj5hPj5iOwogICAgYWRqW2FdLnBiKGIpOwogICAgYWRqW2JdLnBiKGEpOwogICAgaW5bYV0rKzsKICAgIGluW2JdKys7CiAgfQogIGZvcihsbCBpbnQgaT0xO2k8PW47aSsrKXsKICAgIGNpbj4+Y29sb3JbaV07CiAgfQogIAogIGxsIGFuPWluZjsKICBpZihjb2xvclsxXSE9LTEpewogICAgYW49bWluKGRmcygxLGNvbG9yWzFdLC0xKSxhbik7CiAgfWVsc2V7CiAgICBmb3IoaW50IGk9MTtpPD1pbltpXSsxO2krKyl7CiAgICAgICAgYW49bWluKGRmcygxLGksLTEpLGFuKTsKICAgIH0KICB9CiAgY291dDw8YW48PCJcbiI7CgogIAogIAogCiAKIAogCiAKIAogCiAKIAp9