#include<bits/stdc++.h>

#define ll long long
#define pb push_back
#define mp make_pair
#define vec vector
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

using namespace std;

void solve(){
    int n;
    cin >> n;
    int a[n], c[n];
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n; ++i)
        cin >> c[i];
    vec < int > st;
    vec < int > l(n, -1);
    vec < int > p(n, -1);
    map < int, int > last;
    for (int i = 0; i < n; ++i)
        last[a[i]] = -1;
    for (int i = 0; i < n; ++i) {
        while (sz(st) && a[st.back()] <= a[i])
            st.pop_back();
        if (sz(st))
            l[i] = st.back();
        st.push_back(i);
        p[i] = last[a[i]];
        last[a[i]] = i;
    }
    vec < int > dp(n);
    dp[0] = c[0];
    for (int i = 0; i < n; ++i) {
        int pos = l[i];
        if (pos == -1) {
            if (p[i] == -1) {
                dp[i] = c[i];
                continue;
            }
            dp[i] = min(c[i], (p[i] ? dp[p[i] - 1] : 0) + c[p[i]]);
            continue;
        }
        if (!pos) {
            dp[i] = min(dp[0] + c[i], c[0]);
            continue;
        }
        dp[i] = min(dp[pos - 1] + c[pos], dp[pos] + c[i]);
    }
    cout << dp.back();
}

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