#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()

using namespace std;

const long long mod = 1000000007, mod3 = 998244353, inf = (ll)1e15;

struct T{
    ll x, y;
};

ll n;

ll k, a[1000005], b[1000005], t[400005 * 4];

map <ll, ll> pos;

ll cnt[500005];

T p[500005];

void build(ll v = 1, ll tl = 1, ll tr = (n + 1) / 2){
    if (tl == tr){
        t[v] = a[tl];

        return;
    }

    ll tm = (tl + tr) >> 1;

    build(v + v, tl, tm);
    build(v + v + 1, tm + 1, tr);

    t[v] = max(t[v + v], t[v + v + 1]);
}

ll get(ll l, ll r, ll v = 1, ll tl = 1, ll tr = n){
    if (tl > r || l > tr) return 0;

    if (tl >= l && r >= tr){
        return t[v];
    }

    ll tm = (tl + tr) >> 1;

    return max(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
}

ll ans(ll tl, ll tr){
    if (tl == tr) return b[tl];

    ll cur = get(tl, tr);

    cur = pos[cur];

    return min(ans(tl, cur - 1), ans(cur + 1, tr)) + b[cur];
}

void query(){
    cin >> n;

    for (ll i = 1; i <= n; i++){
        cin >> a[i];

        pos[a[i]] = i;
    }

    for (ll i = 1; i <= n; i++){
        cin >> b[i];
    }

    build();

    cout << ans(1ll, n);
}
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    ll TT = 1;

    //cin >> TT;

    while(TT--){
        query();
    }

    return 0;
}

/**

*/
