For this Question: You are given an undirected tree with N vertices and our task is to colour the vertices of the tree. For simplicity, the colours are represented by integers from 1 to N. Some of the vertices have already been assigned a colour, and the remaining are uncoloured.

Let's represent the colour of vertex k by A<sub>k</sub>. If A<sub>k</sub> is -1, this means that the vertex k is initially uncoloured. Otherwise, the vertex k is initially coloured with colour A<sub>k</sub>. 

We call a colouring to be valid if no two adjacent vertices have the same colour, i.e, if two vertices have an edge in between them, then the colours assigned to them must be different. You are not allowed to change the colour of vertices whose colour is given in the input. But you must colour the initially uncoloured vertices using any of the N colours, so that all the vertices get coloured.

There could be multiple ways to do so, hence to simplify our task, we define the cost of the colouring as the sum of A<sub>k</sub> for all k from 1 to N after all the vertices have been coloured (you need to consider all i's even if it's coloured initially). Find the minimum cost for a valid colouring of the tree.

It is guaranteed that the test cases are designed in such a way that at least one valid colouring exists.The first line has a single integer N — the number of vertices of the tree. 
The next N-1 lines contain two integers each, u and v (u ≠ v and 1 ≤ u, v ≤ N), denoting an edge between vertices u and v.
The last line contains N integers A<sub>1</sub>, A<sub>2</sub>,... A<sub>N</sub>, denoting the colours of the vertices. If A<sub>k</sub> = -1, the k<sup>th</sup> vertex is uncoloured.

Furthermore, it is guaranteed that there exists a colouring such that no two adjacent vertices have the same colour.

<b>Constraints:</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ A<sub>k</sub> ≤ N or A<sub>k</sub> = -1
Output a single integer — the minimum cost to colour the tree.Sample Input 1:
3
3 2
3 1
2 2 -1

Sample Output 1:
5

Sample Explanation 1:
3 is the only uncoloured vertex. Since vertex 3 is connected to vertex 1 and vertex 2, both of them having colour 2, we cannot colour vertex 3 with colour 2. The optimal way will be to colour vertex 3 with colour 1. Hence, our total cost becomes 2 + 2 + 1 = 5.

Sample Input 2:
4
2 1
3 4
4 1
1 -1 2 -1

Sample Output 2:
8

Sample Explanation 2:
An optimal solution is to colour vertex 2 with colour 2, and vertex 4 with colour 3. Thus, total cost is 1 + 2 + 2 + 3 = 8., I have written this Solution Code: 
//#define ASC
//#define DBG_LOCAL

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

#define int long long
// #define int __int128

#define all(X) (X).begin(), (X).end()

#define pb push_back
#define endl '\n'
#define fi first
#define se second

// const int mod = 1e9 + 7;
const int mod=998'244'353;


const long long INF = 2e18 + 10;
// const int INF=1e9+10;

#define readv(x, n)   \
    vector<int> x(n); \
    for (auto &i : x) \
        cin >> i;



template <typename T>
    using v = vector<T>;

template <typename T>
    using vv = vector<vector<T>>;

template <typename T>
    using vvv = vector<vector<vector<T>>>;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<vector<double>> vvd;

typedef pair<int, int> pii;



int multiply(int a, int b, int in_mod) { return (int)(1LL * a * b % in_mod); }
int mult_identity(int a) { return 1; }



const double PI = acosl(-1);


auto power(auto a, auto b, const int in_mod)
{

    auto prod = mult_identity(a);
    auto mult = a % 2;
    while (b != 0)
    {
        if (b % 2)
        {
            prod = multiply(prod, mult, in_mod);
        }
        if(b/2)
        mult = multiply(mult, mult, in_mod);
        b /= 2;
    }
    return prod;
}
auto mod_inv(auto q, const int in_mod)
{
    return power(q, in_mod - 2, in_mod);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define stp cout << fixed << setprecision(20);



vvi adj;

void solv()
{
    int n;
    cin>>n;

    adj.resize(n);


    for(int i = 0;i<n-1;i++)
    {
        int a, b;
        cin>>a>>b;

        a--;
        b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }


    readv(a,n);

    const int mx_used = 25;

    v<vector<int>> sm(n, vector<int>(mx_used));
    vector<int> b(n);
    function<void(int, int)> mn_cost = [&](int root, int parent)
    {
        if(a[root] == -1)
        {
            for(int i = 1;i<mx_used;i++)
                sm[root][i] = i;


            for(auto child :adj[root])
            {
                if(child == parent)
                    continue;
                mn_cost(child, root);

                if(a[child] == -1)
                {

                    for(int i = 1;i<mx_used;i++)
                    {
                        int mn = INF;
                        for(int j = 1;j<mx_used;j++)
                        {
                            if(i == j)
                                continue;

                            mn = min(mn, sm[child][j]);

                        }
                        if(sm[root][i] != INF && mn != INF)
                            sm[root][i] += mn;
                        else
                            sm[root][i] = INF;
                    }

                }
                else{

                    for(int i = 1;i<mx_used;i++)
                    {
                        int mn = INF;

                        if(i != a[child])
                            mn = min(mn, b[child]);


                        if(sm[root][i] != INF && mn != INF)
                            sm[root][i] += mn;
                        else
                            sm[root][i] = INF;


                    }

                }



            }



        }
        else{

            b[root] = a[root];


            for(auto child:adj[root])
            {
                if(child == parent)
                    continue;

                mn_cost(child, root);

                int mn = INF;

                if(a[child] != -1)
                {
                    assert(a[child] != a[root]);
                    b[root] += b[child];
                    assert(b[child] != INF);

                }
                else{

                    int mn = INF;
                    for(int i = 1;i<mx_used;i++)
                    {
                        if(i != a[root])
                        {
                            mn = min(mn, sm[child][i]);

                        }
                    }
                    if(b[root] != INF && mn != INF)
                        b[root] += mn;
                    else
                        b[root] = INF;
                }

            }
        }


    };


    mn_cost(0,0);
    int mn = INF;

    if(a[0] == -1)
        mn = *min_element(1 + all(sm[0]));
    else
        mn = b[0];

    cout<<mn<<endl;




}



void solve()
{

    int t = 1;
    // cin>>t;
    for(int i = 1;i<=t;i++)
    {
        // cout<<"Case #"<<i<<": ";
        solv();
    }

}




signed main()
{



    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cerr.tie(NULL);

#ifndef ONLINE_JUDGE
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);

#else
    #ifdef ASC
    namespace fs = std::filesystem;
    std::string path = "./";
    string filename;
    for (const auto & entry : fs::directory_iterator(path)){
        if( entry.path().extension().string() == ".in"){
            filename = entry.path().filename().stem().string();
        }
    }
    if(filename != ""){
        string input_file = filename +".in";
        string output_file = filename +".out";
        if (fopen(input_file.c_str(), "r"))
        {
            freopen(input_file.c_str(), "r", stdin);
            freopen(output_file.c_str(), "w", stdout);
        }
    }
    #endif
#endif
    // auto clk = clock();
    // -------------------------------------Code starts here---------------------------------------------------------------------

    signed t = 1;
    // cin >> t;

    for (signed test = 1; test <= t; test++)
    {
        // cout<<"Case #"<<test<<": ";
        // cout<<endl;

        solve();
    }

    // -------------------------------------Code ends here------------------------------------------------------------------

    // clk = clock() - clk;

    #ifndef ONLINE_JUDGE
    // cerr << fixed << setprecision(6) << "\nTime: " << ((float)clk) / CLOCKS_PER_SEC << "\n";
    #endif
    return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not?