#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <bitset>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <vector>
#define fir first
#define sec second
using ll = long long;

template<typename T>
bool rMin(T& a, const T& b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool rMax(T& a, const T& b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}


using namespace std;

int segt[600000];
vector<int> arr;

int make(int v, int l, int r){
    if(r - l == 1){
        return segt[v] = arr[l];
    }
    return segt[v] = max(make((v << 1) + 1, l, (l + r) >> 1), make((v + 1) << 1, (l + r) >> 1, r));
}
int find(int v, int l, int r, int le, int re){
    if(l == r || le == re) return 0;
    if(l == le && r == re) return segt[v];
    int m = (l + r) >> 1, vl = (v << 1) + 1, vr = ((v + 1) << 1);
    if(le >= m){
        return find(vr, m, r, le, re);
    }
    if(re <= m){
        return find(vl, l, m, le, re);
    }
    return max(find(vl, l, m, le, m), find(vr, m, r, m, re));
}

map<int, int> where;
int pr[200200];

ll ans(int l, int r){
    if(l == r) return 0;
    int nind = where[find(0, 0, arr.size(), l, r)];
    return min(ans(l, nind), ans(nind + 1, r)) + pr[nind];
}

void solve(){

    int qc;
    cin >> qc;
    arr.assign(qc, 0);
    for(int i = 0; i < qc; ++i){
        cin >> arr[i];
        where[arr[i]] = i;
    }
    for(int i = 0; i < qc; ++i){
        cin >> pr[i];
    }
    make(0, 0, qc);
    cout << ans(0, qc) << '\n';

}

int main(){
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int t = 1;

    //cin >> t;

    for(int i = 0; i < t; ++i){
        solve();
    }
}