Problem: Snuke has a rooted tree with N vertices, numbered 1 through N. Vertex 1 is the root of the tree, and the parent of Vertex i ( 2\leq i \leq N ) is Vertex P_i ( P_i < i ). There is a number, 0 or 1, written on each vertex. The number written on Vertex i is V_i.

Snuke would like to arrange the vertices of this tree in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex.

After arranging the vertices, let X be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement. Snuke would like to minimize the inversion number of X. Find the minimum possible inversion number of X.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq P_i < i ( 2 \leq i \leq N )
* 0 \leq V_i \leq 1 ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
P_2 P_3 ... P_N
V_1 V_2 ... V_N


Output

Print the minimum possible inversion number of X.

Examples

Input

6
1 1 2 3 3
0 1 1 0 0 0


Output

4


Input

1

0


Output

0


Input

15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0


Output

31

Solution (in JAVA):

Solution:
import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    class Frac implements Comparable<Frac>{
        long x, y;

        public Frac(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(Frac o) {
            return Long.compare(x * o.y, y * o.x);
        }

        @Override
        public String toString() {
            return "Frac{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }

    int s;


    class Vertex implements Comparable<Vertex>{
        int cntZero;
        int cntOnes;
        List<Vertex> ch;
        Frac res;
        Vertex parent;
        int id = s++;
        boolean alive = true;

        Vertex getParent() {
            return parent.alive ? parent : (parent = parent.getParent());
        }

        Vertex(Vertex p) {
            this.parent = p;
            ch = new ArrayList<>();
        }

        void count() {
            res = new Frac(cntZero, 1);
        }

        @Override
        public int compareTo(Vertex o) {
            int f = -res.compareTo(o.res);
            if (f != 0) {
                return f;
            }
            return Integer.compare(id, o.id);
        }
    }

    long res;

    void go(Vertex parent, int v) {
        if (val[v] == 1) {
            Vertex nv = new Vertex(parent);
            nv.cntOnes++;
            for (int to : g[v]) {
                go(nv, to);
            }
            nv.count();
            res += nv.cntZero;
            pq.add(nv);
        } else {
            parent.cntZero++;
            for (int to : g[v]) {
                go(parent, to);
            }
        }
    }

    int[] val;
    ArrayList<Integer>[] g;



    TreeSet<Vertex> pq = new TreeSet<>();


    void solve() {
        int n = in.nextInt();
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        int[] p = new int[n];
        for (int i = 1; i < n; i++) {
            p[i] = in.nextInt() - 1;
            g[p[i]].add(i);
        }
        int cntZero = 0;
        val = new int[n];
        for (int i = 0; i < n; i++) {
            val[i] = in.nextInt();
            if (val[i] == 0) {
                cntZero++;
            }
        }
        Vertex fake = new Vertex(null);
        go(fake, 0);
        cntZero -= fake.cntZero;
        for (Vertex to : fake.ch) {
            pq.add(to);
        }
        while (pq.size() > 0) {
            Vertex v = pq.pollFirst();
            v.alive = false;
            Vertex par = v.getParent();
            if (par != fake) {
                pq.remove(par);
            }
            res += par.cntOnes * 1L* v.cntZero;
            par.cntZero += v.cntZero;
            par.cntOnes += v.cntOnes;
            par.res = new Frac(par.cntZero, par.cntOnes);
            if (par != fake) {
                pq.add(par);
            }
        }
        out.println(res);
    }

    void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

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

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

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

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

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

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

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

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

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

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