// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

#include <bits/stdc++.h>
 
#define int long long 
#define pb push_back
#define ui unsigned int
#define ld long double
#define pl pair<long long int,  long long int>
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ff first    
#define ss second
#define sz size()
#define all(x) x.begin(), x.end()
 
using namespace std;
 
const int maxn = 2e5 + 11;
const int inf = 1e18 + 1;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1 , 0, -1, 1, -1, 1};
const double eps = 1e-10;

int h[maxn];
int c[maxn];
pair<int, int> t[maxn * 4];
pair<int, int> tt[maxn * 4];

void build(int v, int tl, int tr) {
    if(tl == tr) {
        t[ v ].ff = h[ tl ];
        t[ v ].ss = tl;
        return;
    }
    int tm = tl + tr >> 1;

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

    if(t[v + v].ff <= t[v + v + 1].ff) {
        t[ v ] = t[v + v + 1];
    }
    else {
        t[ v ] = t[v + v];
    }
}
void buildd(int v, int tl, int tr) {
    if(tl == tr) {
        tt[ v ].ff = h[ tl ];
        tt[ v ].ss = tl;
        return;
    }
    int tm = tl + tr >> 1;

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

    if(tt[v + v].ff < tt[v + v + 1].ff) {
        tt[ v ] = tt[v + v + 1];
    }
    else {
        tt[ v ] = tt[v + v];
    }
}

pair<int, int> get(int v, int tl, int tr, int l, int r) {
    if(tl > r || tr < l) {
        return {0, 0};
    }
    if(tl >= l && tr <= r) {
        return t[ v ];
    }
    int tm = tl + tr >> 1;

    pair<int, int> a = get(v + v, tl, tm, l, r);
    pair<int, int> b = get(v + v + 1, tm + 1, tr, l, r);
    if(a.ff <= b.ff) {
        return b;
    }
    else {
        return a;
    }
}
pair<int, int> gett(int v, int tl, int tr, int l, int r) {
    if(tl > r || tr < l) {
        return {0, 0};
    }
    if(tl >= l && tr <= r) {
        return tt[ v ];
    }
    int tm = tl + tr >> 1;

    pair<int, int> a = gett(v + v, tl, tm, l, r);
    pair<int, int> b = gett(v + v + 1, tm + 1, tr, l, r);
    if(a.ff < b.ff) {
        return b;
    }
    else {
        return a;
    }
}

void solve() {
    int n;

    cin >> n;

    for(int i = 1; i <= n; i++) {
        cin >> h[ i ];
    }
    for(int i = 1; i <= n; i++) {
        cin >> c[ i ];
    }
    build(1, 1, n + 1);

    buildd(1, 1, n);

    int i = 1;
    int mn = inf;
    int cnt = 0;

    while(i <= n) {
        pair<int, int> pos = get(1, 1, n, i, n);
        if(h[ i ] == pos.ff) {
            cnt += c[ i ];
            i = n + 1;
            continue;
        }
        cnt += c[ pos.ss ];
        i = pos.ss + 1;
    }
    mn = min(mn, cnt);
    i = n;
    cnt = 0;

    while(i >= 1) {
        pair<int, int> pos = gett(1, 1, n, 1, i);
        // cout << pos.ss << ' ';
        if(h[ i ] == pos.ff) {
            cnt += c[ i ];
            i = 0;
            continue;
        }
        cnt += c[ pos.ss ];
        i = pos.ss - 1;
    }
    mn = min(mn, cnt);

    cout << mn;
}
    
signed main() {
    // freopen("sum.in", "r", stdin); 
    // freopen("sum.out", "w", stdout);
 
    boost;      

    int tt = 1;

    // cin >> tt;

    for(int i = 1; i <= tt; i++) {
        // cout << "Case " << i << ":\n";
        solve();    
    }           
}