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<bits/stdc++.h>

    using namespace std;

    int main()
    {
        int t, n, temp;
        cin >> t;
        map<int, int> parent;
        map<int, int> height;
        map<int, int> height_count;
        vector<int> extra;
        assert(1 <= t and t <= 20);
        while(t--)
        {
            cin >> n;
            parent.clear();
            height.clear();
            height_count.clear();
            extra.resize(n);
            height_count[0] = 0;
            height_count[1] = 1;
            parent[1] = 0;
            height[0] = 0;
            height[1] = 1;
            for(int i = 0; i < n - 1; i++)
            {
                cin >> temp;
                parent[i + 2] = temp;
                height[i + 2] = height[temp] + 1;
                height_count[height[i + 2]] += 1;
            }

            for(int i = 0; i < n; i++)
                cin >> extra[i];

            sort(extra.begin(), extra.end());

            int h = 1, height_iter;
            long long answer = 0;
            for(int i = 1; i < height_count.size(); i++)
            {
                height_iter = height_count[i];
                while(height_iter--)
                {
                    answer = answer + (long long)h * extra.back();
                    extra.pop_back();
                }
                h += 1;
            }
            cout << answer << endl;
        }
        return 0;
    }