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.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.StringTokenizer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
 
public class Main {
    final static int maxn = (int)(2e5 + 5);
    final static long linf = 0x3f3f3f3f3f3f3f3fL;
    static class SegTree {
    	int lf, rt;
    	long mi, lazy;
    	SegTree(int l, int r) {
    		lf = l; rt = r;
    		mi = lazy = 0;
    	}
    }
    static SegTree st[] = new SegTree[maxn << 2];
    static int p[] = new int[maxn];
    static long a[] = new long[maxn];
    static void PushUp(int x) {
    	SegTree now = st[x];
    	SegTree lc = st[x << 1];
    	SegTree rc = st[x << 1 | 1];
    	now.mi = Math.min(lc.mi, rc.mi);
    }
    static void Build(int x, int lf, int rt) {
    	st[x] = new SegTree(lf, rt);
    	SegTree now = st[x];
    	if(lf == rt) {
    		now.mi = 0;
    		return ;
    	}
    	int mid = (lf + rt) >> 1;
    	Build(x << 1, lf, mid);
    	Build(x << 1 | 1, mid + 1, rt);
    	PushUp(x);
    }
    static void PushDown(int x) {
    	SegTree now = st[x];
    	if(now.lazy == 0) {
    		return ;
    	}
    	SegTree lc = st[x << 1];
    	SegTree rc = st[x << 1 | 1];
    	lc.mi += now.lazy;
    	rc.mi += now.lazy;
    	lc.lazy += now.lazy;
    	rc.lazy += now.lazy;
    	now.lazy = 0;
    }
    static void Update(int x, int lf, int rt, long v) {
    	SegTree now = st[x];
    	if(lf <= now.lf && rt >= now.rt) {
    		now.mi += v;
    		now.lazy += v;
    		return ;
    	}
    	PushDown(x);
    	int mid = (now.lf + now.rt) >> 1;
    	if(mid >= lf) {
    		Update(x << 1, lf, rt, v);
    	}
    	if(mid < rt) {
    		Update(x << 1 | 1, lf, rt, v);
    	}
    	PushUp(x);
    }
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(System.out);
//        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = 1; i <= n; ++i) {
        	p[i] = in.nextInt();
        }
        for(int i = 1; i <= n; ++i) {
        	a[i] = in.nextLong();
        }
        Build(1, 0, n);
        for(int i = 1; i <= n; ++i) {
        	Update(1, p[i], n, a[i]);
        }
        long ans = linf;
        for(int i = 1; i < n; ++i) {
        	Update(1, p[i], n, -a[i]);
        	Update(1, 0, p[i] - 1, a[i]);
        	ans = Math.min(ans, st[1].mi);
//        	out.println(i + " " + st[1].mi);
        }
        out.println(ans);
        out.flush();
//        in.close();
    }
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while(tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}

/*

*/
  				 		     	 	  		 							