/////////////////////////////////////////////////////////////////////
//                                                                 //
//                         Code by:                                //
//                      NazhmudinovTRLI                            //
//                                                                 //
/////////////////////////////////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define pi pair<ll,ll>
#define ll long long
#define ld long double
#define bg begin()
#define nd end()
#define sz size()
#define ers erase
#define ff first
#define ss second
#define NeedForSpeed                  \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);\
    cout.tie(nullptr);

ll binpow(ll a, ll x) {
    if (!x)
        return 1 ;
    if (x & 1)
        return a * binpow(a, x - 1) ;
    ll p = binpow(a, x / 2) ;
        return (p * p) ;
}

ll d[200005][30], p[200005] ;
vector<ll> a(200005), c(200005) ;

ll sparse_tabel(ll l, ll r) {
    ll k = (r - l + 1) ;
    k = __lg(k);
    return max(d[l][k], d[r - (1 << k) + 1][k]) ;
}

ll rec(ll l, ll r) {
    if (l == r)
        return c[l] ;
    if(l > r) 
        return 0;
    ll m = find(a.begin(), a.end(), sparse_tabel(l, r)) - a.begin() ;
    //cout << sparse_tabel(l, r) << " %\n";
    //exit(0);
    return min(rec(l, m - 1), rec(m + 1, r)) + c[m];
}

void solve() {

    ll n, x = 0 ;
    cin >> n ;
    for (ll i = 0; i < n; ++i)
        cin >> a[i] ;
    for (ll i = 0; i < n; ++i)
        cin >> c[i] ;
    for (ll i = 0; i < n; ++i)
        d[i][0] = a[i] ;
    for (ll j = 1; j < 30; ++j)
        for (ll i = 0; i <= n - (1 << j) + 1; ++i)
            d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]) ;
    cout << rec(0, n - 1) ;
}

int main() {
//freopen("input.in", "r", stdin) ;
//freopen("output.out", "w", stdout) ;
    NeedForSpeed ll TESTS = 1 ;
    //cin >> TESTS ;
    while (TESTS--) {
        solve() ;
        cout << '\n' ;
    }
}