Kahoo Company
Difficulty: Hard

Baba'ee dream finally came true and he bought the Kahoo Company. The structure of the company is like a rooted Tree. There is the 
general manager as the root, and any worker except general manager has a direct boss. General Manager is represented by 1, all other employees are demonstrated with integers in range [2, n] both inclusive. <br>
The salary given to the person i follows this formula: <br>

    Salary[i] = extra[i] + SUM( Salary[j] ), for any j that i is direct boss of j.

Each employee has a value related to him/her as the extra salary he/she gets, which is represented as extra[i] for person i.<br><br>
As the owner of the company, Baba'ee can swap any two employees in the structure, but he can't change the structure of the company.<br>
If Baba'ee makes the swaps optimally, what is the minimum total salary Baba'ee has to pay to the employees?

__Input Format__ <br>
The first line contains **T**, the number of testcases.  
__T__ testcases follow. Each testcase consists of 3 lines.  
The first line contains **n**, and the second line contains __n-1__ space separated integers, the i-th number is the boss of the (i+1)-th person.
The third line of each test case contains __n__ space separated integers, i-th number in this line is extra[i].
**Use Fast IO.**

__Output Format__ <br>
For each test case, output the minimum total salary Baba'ee has to pay to the employees?

__Constraints__ <br>
1 ≤ T ≤ 20 <br>
1 ≤ n ≤ 10<sup>5</sup> <br>
1 ≤ extra[i] ≤ 10<sup>6</sup> <br>

__Sample Input__ <br>

    1
    3
    1 1
    7 12 13

__Sample Output__ <br>

	51
   
__Explanation__

	In the sample test 1 is boss of 2 and boss of 3.
    It is optimal to swap person 1 and person 3.

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include <iostream>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #include <string.h>
    using namespace std;

    typedef long long ll;
    const ll N = 100000+10;

    ll extra[N];
    ll depth[N];
    vector <int> adj[N];

    queue <int> q;
    void bfs()
    {
        depth[0] = 1;
        q.push( 0 );

        int node;
        while(!q.empty())
        {
            node = q.front();
            q.pop();
            for(int i = 0; i < adj[node].size(); i ++)
            {
                depth[ adj[node][i] ] = depth[node] + 1;
                q.push( adj[node][i] );
            }
        }
    }

    int main()
    {
        int test;
        int n, par;

        cin >> test;
        while(test --)
        {
            cin >> n;
            for(int i = 0; i < n; i ++)
                adj[i].clear();

            for(int i = 1; i < n; i ++)
            {
                cin >> par;
                adj[ par-1 ].push_back( i );
            }
            for(int i = 0; i < n; i ++)
                cin >> extra[i];

            bfs();

            sort(extra, extra+n, greater<int>());
            sort(depth, depth+n);
            ll res = 0;
            for(int i = 0; i < n; i ++)
                res += depth[i] * extra[i];
            cout << res << endl;
        }

        return 0;
    }