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 java.math.BigInteger;
import java.util.*;
import java.io.*;
import java.text.*;
public class Main{
    //SOLUTION BEGIN
    //Into the Hardware Mode
    void pre() throws Exception{}
    void solve(int TC)throws Exception {
        int n = ni();
        int[] p = new int[n], inv = new int[n];
        for(int i = 0; i< n; i++){
            p[i] = ni()-1;
            inv[p[i]] = i;
        }
        long[] a = new long[n];
        for(int i = 0; i< n; i++)a[i] = nl();
        LazySegTree t = new LazySegTree(n);
        long tot = 0;
        for(int i = 0; i< n; i++){
            tot += a[inv[i]];
            t.u(i, i, tot);
        }
        long ans = IINF;
        tot = 0;
        for(int i = 0; i< n-1; i++){
            int u = p[i];
            tot += a[i];
            t.u(0, u-1, a[i]);
            t.u(u, n-1, -a[i]);
            ans = Math.min(ans, t.min(0, n-1));
            ans = Math.min(ans, tot);
        }
        pn(ans);
    }

    class LazySegTree{
        int m = 1;
        long[] sum, lazy;
        public LazySegTree(int n){
            while(m<n)m<<=1;
            sum = new long[m<<1];
            lazy = new long[m<<1|1];
        }
        void push(int ll, int rr, int i){
            if(lazy[i] != 0){
                sum[i] += lazy[i];
                if(ll != rr){
                    lazy[i<<1]+= lazy[i];
                    lazy[i<<1|1]+= lazy[i];
                }
                lazy[i] = 0;
            }
        }
        void u(int l, int r, int ll, int rr, int i, long x){
            push(ll, rr, i);
            if(l == ll && r == rr){
                lazy[i] += x;
                push(ll, rr, i);
                return;
            }
            int mid = (ll+rr)>>1;
            if(r <= mid){
                u(l, r, ll, mid, i<<1, x);
                push(mid+1, rr, i<<1|1);
            }else if(l > mid){
                push(ll, mid, i<<1);
                u(l, r, mid+1, rr, i<<1|1, x);
            }else{
                u(l, mid, ll, mid, i<<1, x);
                u(mid+1, r, mid+1 ,rr, i<<1|1, x);

            }
            sum[i] = Math.min(sum[i<<1], sum[i<<1|1]);
        }
        void u(int l, int r, long x){if(l > r)return;u(l, r, 0, m-1, 1, x);}
        long min(int l, int r){return min(l, r, 0, m-1, 1);}
        long min(int l, int r, int ll, int rr, int i){
            push(ll, rr, i);
            if(l == ll && r == rr)return sum[i];
            int mid = (ll+rr)>>1;
            if(r <= mid)return min(l, r, ll, mid, i<<1);
            else if(l > mid)return min(l, r, mid+1, rr, i<<1|1);
            else return Math.min(min(l, mid, ll, mid, i<<1), min(mid+1, r, mid+1, rr, i<<1|1));
        }
    }
    //SOLUTION END
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    void exit(boolean b){if(!b)System.exit(0);}
    long IINF = (long)1e15;
    final int INF = (int)1e9+2, MX = (int)2e6+5;
    DecimalFormat df = new DecimalFormat("0.00000000000");
    double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-7;
    static boolean multipleTC = false, memory = true, fileIO = false;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        if(fileIO){
            in = new FastReader("");
            out = new PrintWriter("");
        }else {
            in = new FastReader();
            out = new PrintWriter(System.out);
        }
        //Solution Credits: Taranpreet Singh
        int T = (multipleTC)?ni():1;
        pre();
        for(int t = 1; t<= T; t++)solve(t);
        out.flush();
        out.close();
    }
    public static void main(String[] args) throws Exception{
        if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
        else new Main().run();
    }
    int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));}
    int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;}
    long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
    int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n()throws Exception{return in.next();}
    String nln()throws Exception{return in.nextLine();}
    int ni()throws Exception{return Integer.parseInt(in.next());}
    long nl()throws Exception{return Long.parseLong(in.next());}
    double nd()throws Exception{return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next() throws Exception{
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    throw new Exception(e.toString());
                }
            }
            return st.nextToken();
        }

        String nextLine() throws Exception{
            String str = "";
            try{
                str = br.readLine();
            }catch (IOException e){
                throw new Exception(e.toString());
            }
            return str;
        }
    }
}