#include<bits/stdc++.h>
using namespace std;
// #pragma GCC optimize("Ofast") //~ only for that moment
#define all(x) (x).begin(), (x).end()
#define ImOnASugarCrash() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sz(x) int((x).size())
template<typename... Args>
void read(Args&... args) {
    ((std::cin >> args), ...);
}
using ll = long long;
using ld = long double;
//!

const ll MAXN = 200005;
const ll LOG = 30;
ll table[MAXN][LOG];
vector<ll> a(MAXN), b(MAXN);

ll query(ll L, ll R) {
    ll k = R - L + 1;
    k = __lg(k);
    return max(table[L][k], table[R - (1 << k) + 1][k]);
}


ll solve(ll L, ll R) {
    if (L > R)
        return 0;
    if (L == R)
        return b[L];
    ll m = find(a.begin() + L, a.begin() + R, query(L, R)) - a.begin();
    return min(solve(L, m - 1), solve(m + 1, R)) + b[m];
}

signed main() {
    ImOnASugarCrash();    
    ll n;
    read(n);
    a.resize(n); b.resize(n);
    for (ll i = 0; i < n; i ++) {
        cin >> a[i];
        table[i][0] = a[i];
    }
    for (ll i = 0; i < n; i ++) {
        cin >> b[i];
    }
    for (ll j = 1; j < LOG; j ++) {
        for (ll i = 0; i <= n - (1 << j) + 1; i ++) {
            table[i][j] = max(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]);
        }
    }
    cout << solve(0, n - 1);
}