#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <bitset>
#include <vector>

template<typename T>
bool rMin(T& a, const T& b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool rMax(T& a, const T& b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

using namespace std;

typedef long long ll;

const int INF = 1e9 + 19;

void solve(){
    int n;
    cin >> n;
    vector<int> h(n + 1, INF), c(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> h[i];
    for (int i = 1; i <= n; ++i)
        cin >> c[i];
    vector<int> l(n + 1);
    vector<int> st;
    st.push_back(0);
    for (int i = 1; i <= n; ++i) {
        while (h[i] > h[st.back()])
            st.pop_back();
        l[i] = st.back();
        st.push_back(i);
    }
    vector<ll> dp(n + 1 , 0);
    for (int i = 1; i <= n; i++) {
        dp[i] = min(dp[i - 1] + c[i], dp[l[i]] + c[i]);
    }
    cout << dp[n] << endl;
}

int main(){
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int t = 1;

    //cin >> t;

    for(int i = 0; i < t; ++i){
        solve();
    }
}