
#include <bits/stdc++.h>

#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

#define fi first
//#define second secondcond
#define pb push_back
#define fr(i, l, r) for (int i = (l); i <= (r); ++i)
#define rf(i, l, r) for (int i = (l); i >= (r); --i)

using namespace std;

typedef long long ll;
typedef long double ld;


#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;


const int N = 2e5+100;
const ll inf = 1e18;

ll h[N], c[N], dp[N];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    for (int i = 1; i <= n; i++) {
        dp[i] = inf;
    }
    dp[1] = c[1];
    set <pair <int, ll>> st;
    st.insert({1, h[1]});
    oset <pair <ll, ll>> ost;
    ost.insert({h[1], dp[1]});
    for (int i = 2; i <= n; i++) {
        dp[i] = min(dp[i], dp[i - 1] + c[i]);
        pair <int, ll> p = *--st.end();
        if (p.second > h[i]) {
            dp[i] = min(dp[i], dp[p.first] + c[p.first]);
        }

        int l = 0, r = i - 1;
        while (r - l > 13) {
            int mid = (l + r) >> 1;
            pair <ll ,ll> g = *ost.find_by_order(mid);
            if (g.first < h[i]) l = mid + 1;
            else r = mid;
        }
        for (int j = l; j <= r; j++) {
             pair <ll ,ll> g = *ost.find_by_order(j);
             if (g.first > h[i]) {
                dp[i] = min(dp[i], g.second);
                // break;
             }
        }

        ost.insert({h[i], dp[i]});
        st.insert({i, h[i]});


    }
    cout << dp[n];
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}

