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.util.*;
import java.io.*;
import java.math.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        //Scanner sc = new Scanner();
        Reader in = new Reader();
        Main solver = new Main();
        solver.solve(out, in);
        out.flush();
        out.close();
 
    }
 
    static int INF = (int)1e9;
    static int maxn = 2*(int)1e5+5;
    static int mod= 1000000321 ;
    static int n,m,k,t,q,d,p;
    
    static long[] tree, lazy;
    
    void solve(PrintWriter out, Reader in) throws IOException{
        n = in.nextInt();
        
        tree = new long[maxn*4];
        lazy = new long[maxn*4];
        
        int tmp;
        long[] sum = new long[n+1];
        int[] p = new int[n+1];
        long[] a = new long[n+1];
        int[] pos = new int[n+1];
        for (int i = 1; i <= n; i++) {
            tmp = in.nextInt();
            p[i] = tmp;
            pos[tmp] = i;
        }
        for (int i = 1; i <= n; i++) {
            a[i] = in.nextInt();
        }
        sum[1] = a[pos[1]];
        for (int i = 2; i <= n; i++) sum[i] = sum[i-1] + a[pos[i]];
        
        for (int i = 1; i < n; i++) {
            if (p[i] == 1) update(1, 1, maxn, i, maxn, -a[i]);
            else           update(1, 1, maxn, i, maxn,  a[i]);
        }
        
        long ans = tree[1]+sum[1];
        ans = Math.min(ans, Math.min(a[n],a[1]));
        
        for (int i = 2; i < n; i++) {
            if (pos[i] == n) continue;
            update(1, 1, maxn, pos[i], maxn, -2L*a[pos[i]]);
            ans = Math.min(ans, sum[i]+tree[1]);
        }
        
        out.println(ans);
    }
    
    //<>
    
    
    
    static void update(int idx, int s, int e, int l, int r, long v) {
        if (lazy[idx] != 0) relax(idx, s, e);
        
        if (e < l || r < s) return;
        if (l <= s && e <= r) {
            lazy[idx] += v;
            relax(idx, s, e);
            return;
        }
        
        int mid = (s+e)/2;
        update(idx*2, s, mid, l, r, v);
        update(idx*2+1, mid+1, e, l, r, v);
        tree[idx] = Math.min(tree[idx*2], tree[idx*2+1]);
    }
    
    static void relax(int idx, int s, int e) {
        tree[idx] += lazy[idx];
        if (s != e) {
            lazy[idx*2] += lazy[idx];
            lazy[idx*2+1] += lazy[idx];
        }
        lazy[idx] = 0;
    }
    
    static class Reader {
 
    private InputStream mIs;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
 
    public Reader() {
        this(System.in);
    }
 
    public Reader(InputStream is) {
        mIs = is;
    }
 
    public int read() {
        if (numChars == -1) {
            throw new InputMismatchException();
 
    }
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = mIs.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0) {
                return -1;
            }
        }
        return buf[curChar++];
    }
 
    public String nextLine() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }
 
    public String next() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }
 
    double nextDouble()
    {
        return Double.parseDouble(next());
    }
 
    public long nextLong() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9') {
                throw new InputMismatchException();
            }
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
 
    public int nextInt() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9') {
                throw new InputMismatchException();
            }
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
 
    public boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }
 
    public boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
 
    }
}