///In the name of Allah, the most Gracious and the most Merciful.
#include<bits/stdc++.h>
#define fr first
#define sc second
#define ll long long
#define pb push_back
#define no cout<<"NO"<<endl
#define yes cout<<"YES"<<endl
#define IShowSpeed ios_base::sync_with_stdio(0) ; cin.tie(0) ;
using namespace std ;

ll n , m , a , b , k , x , y , ans , cnt , sum , mid;
vector < ll > v , v1 , vv ;
map < ll , ll > mp ;
string s , s1 , ss ;
deque < ll > d ;

struct mercurii {
    vector<pair<ll, ll>> tree;
    ll sz;
    void init(ll n) {
        sz = 1;
        for(; sz < n; sz *= 2);
        tree.assign(sz * 2 - 1, {LLONG_MIN, -1});
    }
    void build(vector<ll> &a, ll x, ll lx, ll rx) {
        if (rx - lx == 1) {
            tree[x].second = lx;
            if (lx < a.size()) {
                tree[x].first = a[lx];
            }
        } else {
            ll m = (lx + rx) / 2;
            build(a, x * 2 + 1, lx, m);
            build(a, x * 2 + 2, m, rx);
            tree[x] = yakKun(tree[x * 2 + 1], tree[x * 2 + 2]);
        }
    }

    void build(vector<ll> &a) {
        init(a.size());
        build(a, 0, 0, sz);
    }

    pair<ll, ll> yakKun(pair<ll, ll> a, pair<ll, ll> b) {
        if (a.first > b.first) {
            return a;
        } else {
            return b;
        }
    }

    pair<ll, ll> MIN(ll l, ll r, ll x, ll lx, ll rx) {
        if (lx >= r or l >= rx) {
            return make_pair(LLONG_MIN, -1);
        }
        if (lx >= l and rx <= r) {
            return tree[x];
        }
        ll m = (lx + rx) / 2;
        auto s1 = MIN(l, r, x * 2 + 1, lx, m);
        auto s2 = MIN(l, r, x * 2 + 2, m, rx);
        return yakKun(s1, s2);
    }

    ll MIN(ll l, ll r) {
        return MIN(l, r, 0, 0, sz).second;
    }
};

ll base;
vector<ll> price;
vector<ll> val;
mercurii TT;

ll halKun(ll l, ll r) {
    if (r - l == 1) {
        return price[l];
    } else if (r - l == 0) {
        return 0;
    }
    ll poz = TT.MIN(l, r);
    ll chapQiymat = halKun(l, poz);
    ll rastQiymat = halKun(poz + 1, r);
    return min(chapQiymat, rastQiymat) + price[poz];
}

void halMasala() {
    cin >> base;
    TT = mercurii();
    price.assign(base, 0);
    val.assign(base, 0);

    for (ll i = 0; i < base; i++) {
        cin >> val[i];
    }
    for (ll i = 0; i < base; i++) {
        cin >> price[i];
    }

    TT.build(val);
    cout << halKun(0, base);
}
/**
    So remember me , I will remember you [ Quran 2:152 ]
**/
int main()
{
    IShowSpeed
    ll t = 1 ;
    ///cin >> t ;
    while ( t -- )
    {
        halMasala() ;
    }
}
