You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i.

At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n.

After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i.

Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met.

For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1).

Calculate the minimum number of dollars you have to spend.

Input

The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation.

The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once.

The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9).

Output

Print one integer — the minimum number of dollars you have to spend.

Examples

Input


3
3 1 2
7 1 4


Output


4


Input


4
2 4 1 3
5 9 8 3


Output


3


Input


6
3 5 1 6 2 4
9 1 9 9 1 9


Output


2
import org.omg.CORBA.INTERNAL;
import sun.nio.cs.ext.MacHebrew;
import sun.reflect.generics.tree.Tree;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;


public class Main {

    public static void main(String[] args) {
    ScanReader scanner = new ScanReader(System.in);
//        Scanner scanner = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        solve(scanner, out);
        out.close();
    }

    final static int MAXN = 2 * 100000 + 5;
    final static int MAXK = 505;
    static int N = 0;
    static int M = 0;
    static int K = 0;
    static int W = 0;
    static int H = 0;
//    static int[][] A;
    static long[][] dp;
    static int[][] pre;
    static int curAns = Integer.MAX_VALUE;
    static long[] HASH;
    static long MOD = (1L << 50) + 7;
    static int[] WC;

    static void solve(ScanReader scanner, PrintWriter out) {
        N = scanner.nextInt();
        int[] P = new int[N];
        int[] A = new int[N];
        long[] B = new long[N-1];
        int[] RP = new int[N+1];
        for (int i = 0; i < N; i++) {
            P[i] = scanner.nextInt();
        }
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        B[0] = -A[0];
        for (int i = 1; i < N-1; i++) {
            B[i] = B[i-1] - A[i];
        }

        for (int i = 0; i < N; i++) {
            RP[P[i]] = i;
        }

        SegmentMaxTree s = new SegmentMaxTree();
        s.build(B);

        long ans = s.getMax();
        for (int i = 1; i <= N; i++) {
            int rp = RP[i];
            s.add(rp, A[rp]);
            s.add(0, rp-1, -A[rp]);
            ans = Math.max(ans, s.getMax());
        }

        out.println(-ans);
    }


    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int nextInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}

class UnioinSet {
    private Map<Long, Long> parents = new HashMap<>();

    public Long find(Long u) {
        if (parents.getOrDefault(u, u).equals(u)) {
            return u;
        }

        return find(parents.get(u));
    }

    public void union(Long u, Long v) {
        Long pu = find(u);
        Long pv = find(v);

        parents.put(pu, pv);
    }
}

class Pair implements Comparable<Pair> {
    public Integer x;
    public Integer y;

    public Pair(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

//    public int toIndex(int base) {
//        return this.x * base + this.y;
//    }

    public List<Pair> neighbors() {
        List<Pair> ans = new ArrayList<>();
        ans.add(new Pair(this.x + 1, this.y));
        ans.add(new Pair(this.x - 1, this.y));
        ans.add(new Pair(this.x, this.y + 1));
        ans.add(new Pair(this.x, this.y - 1));

        return ans;
    }

    @Override
    public int compareTo(Pair o) {
        if (o.x != this.x) {
            return (int) (this.x - o.x);
        } else {
            return (int) (this.y - o.y);
        }
    }

    @Override
    public int hashCode() {
        Integer v = this.x * 100000 + this.y;
        return Integer.hashCode(v);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof Pair) {
            Pair op = (Pair) obj;
            return op.x.equals(this.x) && op.y.equals(this.y);
        }

        return false;
    }
}


class SegmentMaxTree {
    private int size;
    private int dsize;
    private long[] data;
    private long[] lazy;

    public void build(long[] data) {
        if (data == null || data.length <= 0) {
            return;
        }

        this.size = data.length;
        this.dsize = this.size * 4;
        this.data = new long[this.dsize];
        this.lazy = new long[this.dsize];
        this.build(1, 0, this.size-1, 0, this.size-1, data);
    }

    public void add(int fromIndex, long val) {
        this.add(1, 0, this.size-1, fromIndex, this.size-1, val);
    }

    public void add(int fromIndex, int toIndexInclusive, long val) {
        this.add(1, 0, this.size-1, fromIndex, toIndexInclusive, val);
    }

    public long getMax() {
        return this.getMax(1, 0, this.size-1, 0, this.size-1);
    }

    private void push(int index) {
        if (index < this.dsize && this.lazy[index] != 0) {
            long v = this.lazy[index];
            this.data[index] += v;
            this.lazy[index] = 0;
            int[] child = new int[] {index * 2, index * 2 + 1};
            for (int ci : child) {
                if (ci < this.dsize) {
                    this.lazy[ci] += v;
                }
            }
        }
    }

    private long build(int index, int nodel, int noder, int targetl, int targetr, long[] data) {
        if (nodel > targetr || noder < targetl || targetr < targetl) {
            return this.data[index];
        }

        if (targetl == targetr) {
            this.data[index] = data[targetl];
            return this.data[index];
        } else {
            int m = (nodel + noder) / 2;
            long a = this.build(index * 2, nodel, m, targetl, Math.min(m, targetr), data);
            long b = this.build(index * 2 + 1, m + 1, noder, Math.max(targetl, m+1), targetr, data);
            long c = Math.max(a, b);
            this.data[index] = c;

            return c;
        }
    }

    private long add(int index, int nodel, int noder, int targetl, int targetr, long val) {
        this.push(index);
        if (nodel > targetr || noder < targetl || targetl > targetr) {
            return this.data[index];
        }

        if (nodel == targetl && noder == targetr) {
            this.lazy[index] += val;
            return this.data[index] + this.lazy[index];
        } else {
            int m = (nodel + noder) / 2;
            long a = this.add(index * 2, nodel, m, targetl, Math.min(targetr, m), val);
            long b = this.add(index * 2 + 1, m + 1, noder, Math.max(targetl, m+1), targetr, val);
            long c = Math.max(a, b);
            this.data[index] = c;
            return c;
        }
    }

    private long getMax(int index, int nodel, int noder, int targetl, int targetr) {
        this.push(index);

        if (nodel > targetr || noder < targetl || targetr < targetl) {
            return this.data[index];
        } else if (nodel == targetl && noder == targetr) {
            return this.data[index];
        } else {
            int m = (nodel + noder) / 2;
            long a = this.getMax(index * 2, nodel, m, targetl, Math.min(m, targetr));
            long b = this.getMax(index * 2 + 1, m + 1, noder, Math.max(targetl, m+1), targetr);
            return Math.max(a, b);
        }
    }
}