#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cstdio>
#include <random>
#include <chrono>

using namespace std;

using ll = long long;
using ld = long double;

const ll inf = ll(2e18) + 26;

mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

//#define int long long
#define all(x) (x).begin(), (x).end()
vector<ll> t;

void update(ll pos, ll d, ll v, ll l, ll r) {
    if (r - l < 2) {
        t[v] = d;
        return;
    }
    ll m = (l + r) / 2;
    if (pos < m) {
        update(pos, d, v * 2 + 1, l, m);
    } else {
        update(pos, d, v * 2 + 2, m, r);
    }
    t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}

ll get(ll ql, ll qr, ll v, ll l, ll r) {
    if (qr <= l || r <= ql) {
        return inf;
    }
    if (ql <= l && r <= qr) {
        return t[v];
    }
    ll m = (l + r) / 2;
    return get(ql, qr, v * 2 + 1, l, m) + get(ql, qr, v * 2 + 2, m, r);
}

void solve() {
    ll n;
    cin >> n;
    vector<ll> h(n), c(n);
    for (auto &it : h) {
        cin >> it;
    }
    for (auto& it : c) {
        cin >> it;
    }
    vector<ll> srt = h;
    sort(all(srt));
    for (auto &it : h) {
        it = lower_bound(all(srt), it) - srt.begin();
    }
    vector<ll> minS(n, inf);
    vector<ll> d(n, inf);
    stack<ll> st;
    t.resize(n * 4, 0);
    for (ll i = 0; i < n; i++) {
        while (!st.empty() && h[st.top()] < h[i]) {
            st.pop();
        }
        if (!st.empty()) d[i] = minS[ll(st.size()) - 1];
        if (!st.empty()) {
            ll j = st.top() + 1;
            d[i] = min(d[i], get(j, i, 0, 0, n));
        }
        else {
            d[i] = min(d[i], c[i]);
        }
        minS[st.size()] = min(st.empty() ? inf : minS[ll(st.size()) - 1], (i ? d[i - 1] : 0) + c[i]);
        st.push(i);
        update(h[i], d[i], 0, 0, n);
    }
    cout << d[n - 1];
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll timer = 1;
    // cin >> timer;
    while (timer--) {
        solve();
    }
    return 0;
}
