#include <bits/stdc++.h>
using namespace std;
#define ll long long

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_table(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() + l, a.begin() + r, sparse_table(l, r)) - a.begin();
    return min(rec(l, m - 1), rec(m + 1, r)) + c[m];
}

void solve() {
    ll n;
    cin >> n;
    a.resize(n);
    c.resize(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() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll TESTS = 1;
    while (TESTS--) {
        solve();
        cout << '\n';
    }
}
