#include <bits/stdc++.h>

using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 5e5 + 9 , mod = 1e9 + 7;
ll   a[N] = {}, b[N] , c[N] , d[N] , dp[N] , tree[N * 2]  , tree1[N * 2];




void change(int i , int  l , int r , int k , int x){
    int m = (l + r) / 2;
    if(l > k || r < k)
        return;
    if(l == k && r == k){
        tree[i] = x;
        return;
    }
    change(2 * i ,  l , m , k , x);
    change(2 * i  + 1,  m + 1 , r , k , x);
    tree[i] = max(tree[2 * i] , tree[2 * i + 1]);
}
void change1(int i , int  l , int r , int k , int x){
    int m = (l + r) / 2;
    if(l > k || r < k)
        return;
    if(l == k && r == k){
        tree1[i] = x;
        return;
    }
    change1(2 * i ,  l , m , k , x);
    change1(2 * i  + 1,  m + 1 , r , k , x);
    tree1[i] = min(tree1[2 * i] , tree1[2 * i + 1]);
}

ll ans = 0 , ans1 = 1e18;
void get(int i , int l, int r , int x , int y){
    if(l > y || r < x)
        return ;
    int m = (l + r) / 2;
    if(l >= x && r <= y){
        ans = max(ans , tree[i]);
        return;
    }
    get(2 * i , l , m , x , y);
    get(2 * i + 1 , m + 1 , r , x , y);
}
void get1(int i , int l, int r , int x , int y){
    if(l > y || r < x)
        return ;
    int m = (l + r) / 2;
    if(l >= x && r <= y){
        ans1 = min(ans1 , tree1[i]);
        return;
    }
    get1(2 * i , l , m , x , y);
    get1(2 * i + 1 , m + 1 , r , x , y);
}

void solve()
{
    ll q , i , j , m ,n, z ,s = 0 , f  = 0, l , r , k , x , y , mn  = 1e18 , mx = 0;
    cin>>n;
    set<int>st1;
    map<int,int>mp;
    for(i = 1; i <= n; i++){
        cin>>a[i];
        st1.insert(a[i]);
    }
    for(auto it : st1)
        mp[it] = ++s;
    for(i = 1; i <= n; i++)
        a[i] = mp[a[i]];
    for(i = 1; i <= n; i++){
        cin>>b[i];
    }
    multiset<pair<ll,ll>>st;
    multiset<ll>st2;
    for(i = 1; i <= n; i++){
        ans = 0;
    //   cout<<a[i]<<" ";
        if(i != 1)
        get(1 , 1 , n + 2 , a[i] + 1 , n + 1);
        while(st.size() && st.begin()->fi <= a[i]){
            auto it = st.begin();
            st2.erase(st2.find(it->se));
            st.erase(it);
        }
        if(ans == 0){
            dp[i] = b[i];
            if(st2.size())
                dp[i] = min(dp[i] , *st2.begin());
        }else {
            ans1 = 1e16;
            get1(1 , 1 , n + 2 , ans , i - 1);
            dp[i] = dp[i - 1] + b[i];
            dp[i] =  min(dp[i], ans1 + b[i]);
            if(st2.size())
                dp[i] = min(dp[i] , *st2.begin());
        }
        st.insert({a[i] , dp[i - 1] + b[i]});
        st2.insert(dp[i - 1] + b[i]);

        change(1 , 1 , n + 2, a[i] , i);
        change1(1 , 1 , n + 2, i , dp[i]);
    }
    cout<<dp[n];
}

int main(){

int t = 1;
//cin>>t;
while(t--)
     {
     solve();
     }
}
// Author : حسن
