// #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];
int a[maxn];
int t[maxn * 4];

void build(int v, int tl, int tr) {
    if(tl == tr) {
        t[ v ] = h[ tl ];
        return;
    }
    int 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]);
}
int get(int v, int tl, int tr, int l, int r) {
    if(tl > r || tr < l) {
        return 0;
    }
    if(tl >= l && tr <= r) {
        return t[ v ];
    }
    int tm = tl + tr >> 1;

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

void solve() {
    int n;

    cin >> n;

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

    int szz = 1;
    map<int, int> mp;
    for(int i = 1; i <= n; i++) {
        if(!mp[a[ i ]]) {
            mp[a[ i ]] = szz;
            szz++;
        }
    }
    for(int i = 1; i <= n; i++) {
        h[ i ] = mp[h[ i ]];
    }
    vector<int> g[n + 1];

    for(int i = 1; i <= n; i++) {
        g[h[ i ]].pb( i );
    }
    build(1, 1, n);

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

    while(i <= n) {
        int val = get(1, 1, n, i, n);
        if(h[ i ] == val) {
            int l = 0, r = g[ val ].sz - 1;
            int ans = -1;

            while(l <= r) {
                int mid = l + r >> 1;

                if(g[ val ][ mid ] > i) {
                    ans = mid;
                    r = mid - 1;
                }
                else {
                    l = mid + 1;
                }
            }
            cnt += c[ i ];
            if(ans == -1) {
                i = n + 1;
            }
            else {
                i = g[ val ][ ans ];
            }
            continue;
        }
        int l = 0, r = g[ val ].sz - 1;
        int ans = -1;

        while(l <= r) {
            int mid = l + r >> 1;

            if(g[ val ][ mid ] >= i) {
                ans = mid;
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        cnt += c[ g[ val ][ ans ] ];
        i = g[ val ][ ans ] + 1;
    }
    mn = min(mn, cnt);
    i = n;
    cnt = 0;

    while(i >= 1) {
        int val = get(1, 1, n, 1, i);
        // cout << pos.ss << ' ';
        if(h[ i ] == val) {
            int l = 0, r = g[ val ].sz - 1;
            int ans = -1;

            while(l <= r) {
                int mid = l + r >> 1;

                if(g[ val ][ mid ] < i) {
                    ans = mid;
                    l = mid + 1;
                }
                else {
                    r = mid - 1;
                }
            }
            cnt += c[ i ];
            if(ans == -1) {
                i = 0;
            }
            else {
                i = g[ val ][ ans ];
            }
            continue;
        }
        int l = 0, r = g[ val ].sz - 1;
        int ans = -1;

        while(l <= r) {
            int mid = l + r >> 1;

            if(g[ val ][ mid ] <= i) {
                ans = mid;
                l = mid + 1;
            }
            else {
                r = mid - 1;
            }
        }
        cnt += c[ g[ val ][ ans ] ];
        i = g[ val ][ ans ] - 1;
    }
    // cout << cnt << "\n";
    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();    
    }           
}